Skip to content

Commit f85eaa7

Browse files
Fix play mode tests jumping to init scene instead of running (#550)
* Fix play mode test scene jumping issue This commit fixes an issue where play mode tests would jump to the init scene but not run any tests. Changes: - Wrap TestRunnerCallbacks.IsRunningTests check with #if UNITY_INCLUDE_TESTS in ChangeScene.cs This ensures the test runner check is only performed when test framework is available - Add test assembly reference to JEngine.Core.Editor.asmdef (GUID:0acc523941302664db1f4e527237feb3) - Update .gitignore to exclude Claude-generated files The root cause was that ChangeScene.cs was trying to check TestRunnerCallbacks.IsRunningTests without verifying the test framework was available, which could cause the check to fail or not work properly during play mode tests. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * Improve play mode test detection by preventing RuntimeInitializeOnLoadMethod registration The previous fix checked TestRunnerCallbacks.IsRunningTests at runtime, but there was a timing issue - RuntimeInitializeOnLoadMethod executes before TestRunnerCallbacks.RunStarted is called, so the flag was not set yet. This commit uses the cleaner approach recommended by Unity: prevent the method from being registered at all during tests by applying #if !UNITY_INCLUDE_TESTS to the attribute itself. This eliminates the runtime check and TestRunnerCallbacks dependency entirely from ChangeScene.cs, making the code simpler and more reliable. References: - Unity Discussions: RuntimeInitializeOnLoadMethod runs during playmode unit tests https://discussions.unity.com/t/methods-with-runtimeinitializeonloadmethod-attribute-run-during-playmode-unit-tests/949876 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * 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> * Add Unity Test Framework temporary test scenes to .gitignore Unity Test Framework creates temporary scenes like InitTestScene<timestamp>.unity when running play mode tests. These should not be committed to the repository. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent a1f7876 commit f85eaa7

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@ UnityProject/Build/*
1616
/UnityProject/UserSettings/Layouts
1717
/UnityProject/HybridCLRData
1818
/UnityProject/UserSettings
19+
20+
# Unity Test Framework temporary scenes
21+
/UnityProject/Assets/InitTestScene*.unity
22+
/UnityProject/Assets/InitTestScene*.unity.meta
23+
24+
# Auto Claude generated files
25+
.auto-claude/
26+
.auto-claude-security.json
27+
.auto-claude-status
28+
.claude_settings.json
29+
.worktrees/
30+
.security-key
31+
logs/security/

UnityProject/Packages/com.jasonxudeveloper.jengine.core/Editor/JEngine.Core.Editor.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"GUID:f51ebe6a0ceec4240a699833d6309b23",
1212
"GUID:e34a5702dd353724aa315fb8011f08c3",
1313
"GUID:3743e71edcd5bd8499007797ef02cbfb",
14-
"GUID:27619889b8ba8c24980f49ee34dbb44a"
14+
"GUID:27619889b8ba8c24980f49ee34dbb44a",
15+
"GUID:0acc523941302664db1f4e527237feb3"
1516
],
1617
"includePlatforms": [
1718
"Editor"

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ internal static class ChangeScene
1212
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
1313
private static void DoChange()
1414
{
15-
// Skip scene change when running Play Mode tests
16-
if (TestRunnerCallbacks.IsRunningTests)
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"))
1719
{
20+
Debug.Log($"[JEngine] Skipping scene change - Play Mode test detected (scene: {currentScene.name})");
1821
return;
1922
}
2023

0 commit comments

Comments
 (0)