Skip to content

Commit 6b011e9

Browse files
Detect play mode tests by checking scene name instead of timing-dependent callbacks
The previous approaches had timing issues - TestRunnerCallbacks.RunStarted is called after RuntimeInitializeOnLoadMethod, and #if !UNITY_INCLUDE_TESTS doesn't work because the assembly always has UNITY_INCLUDE_TESTS defined (it contains test files). This commit uses a simple, reliable approach: Unity Test Framework creates temporary scenes with names like "InitTestScene<timestamp>" when running play mode tests. By checking if the current scene name starts with "InitTestScene", we can reliably detect test runs at runtime without timing dependencies. Benefits: - No timing issues with callbacks - No dependency on preprocessor directives - Works in all scenarios (GUI test runner, command line, etc.) - Simple and maintainable Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 7aa10e2 commit 6b011e9

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

  • UnityProject/Packages/com.jasonxudeveloper.jengine.core/Editor/Misc

UnityProject/Packages/com.jasonxudeveloper.jengine.core/Editor/Misc/ChangeScene.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ namespace JEngine.Core.Editor.Misc
99
/// </summary>
1010
internal static class ChangeScene
1111
{
12-
// Don't auto-change scenes during Play Mode tests
13-
#if !UNITY_INCLUDE_TESTS
1412
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
15-
#endif
1613
private static void DoChange()
1714
{
15+
// Unity Test Framework creates scenes with names like "InitTestScene<timestamp>"
16+
// Skip scene change if we're in a test scene
17+
var currentScene = SceneManager.GetActiveScene();
18+
if (currentScene.name.StartsWith("InitTestScene"))
19+
{
20+
Debug.Log($"[JEngine] Skipping scene change - Play Mode test detected (scene: {currentScene.name})");
21+
return;
22+
}
23+
1824
var jump = Settings.Instance.jumpStartUp;
1925
if (!jump) return;
2026
var scene = SceneManager.GetActiveScene();

0 commit comments

Comments
 (0)