Fix play mode tests jumping to init scene instead of running#550
Merged
Conversation
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>
…dMethod 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>
…dent 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>
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>
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the issue where play mode tests would jump to the init scene but not actually run the tests. The problem was that
RuntimeInitializeOnLoadMethodinChangeScene.cswas executing during test runs and changing the active scene.Changes
InitTestScene(which Unity Test Framework creates for test runs)Technical Details
The fix went through several iterations to find the right approach:
#if UNITY_INCLUDE_TESTSto wrap the test check - didn't work because of timing issues#if !UNITY_INCLUDE_TESTSon the attribute - didn't work because the assembly always hasUNITY_INCLUDE_TESTSdefinedInitTestScene<timestamp>for play mode testsThe scene name check is reliable, has no timing dependencies, and works in all test scenarios (GUI runner, command line, etc.).
Test Plan
🤖 Generated with Claude Code