Skip to content

Conversation

@JasonXuDeveloper
Copy link
Owner

Summary

Fixes the issue where play mode tests would jump to the init scene but not actually run the tests. The problem was that RuntimeInitializeOnLoadMethod in ChangeScene.cs was executing during test runs and changing the active scene.

Changes

  1. Scene-based test detection - Detect play mode tests by checking if the current scene name starts with InitTestScene (which Unity Test Framework creates for test runs)
  2. Assembly definition update - Added Unity Test Framework assembly reference to support test-related functionality
  3. Gitignore improvements - Added patterns to exclude Claude-generated files and Unity test framework temporary scenes

Technical Details

The fix went through several iterations to find the right approach:

  1. Initial attempt: Used #if UNITY_INCLUDE_TESTS to wrap the test check - didn't work because of timing issues
  2. Second attempt: Used #if !UNITY_INCLUDE_TESTS on the attribute - didn't work because the assembly always has UNITY_INCLUDE_TESTS defined
  3. Final solution: Check the active scene name at runtime - Unity creates scenes named InitTestScene<timestamp> for play mode tests

The scene name check is reliable, has no timing dependencies, and works in all test scenarios (GUI runner, command line, etc.).

Test Plan

  • Run play mode tests from Unity Test Runner - tests execute correctly without scene jumping
  • Enter play mode normally - init scene jumping still works as expected
  • Verify no build errors or warnings

🤖 Generated with Claude Code

JasonXuDeveloper and others added 4 commits January 22, 2026 15:59
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>
@claude
Copy link

claude bot commented Jan 22, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@JasonXuDeveloper JasonXuDeveloper merged commit f85eaa7 into master Jan 22, 2026
5 checks passed
@JasonXuDeveloper JasonXuDeveloper deleted the fix/play-mode-tests branch January 22, 2026 05:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant