CoplayDev/unity-mcp · error · InvalidOperationException

Cannot start a test run while the Editor is in or entering P

Error message

Cannot start a test run while the Editor is in or entering Play Mode. Stop Play Mode and try again.

What it means

Thrown by TestRunnerService.RunTestsAsync when EditorApplication.isPlaying or isPlayingOrWillChangePlaymode is true. Starting a test run (especially one that itself toggles Play Mode) while the editor is already in or entering Play Mode would conflict with Unity's play-state transitions, so the service refuses up front.

Source

Thrown at MCPForUnity/Editor/Services/TestRunnerService.cs:204

        }

        public async Task<TestRunResult> RunTestsAsync(TestMode mode, TestFilterOptions filterOptions = null)
        {
            await _operationLock.WaitAsync().ConfigureAwait(true);
            Task<TestRunResult> runTask;
            bool adjustedPlayModeOptions = false;
            bool originalEnterPlayModeOptionsEnabled = false;
            EnterPlayModeOptions originalEnterPlayModeOptions = EnterPlayModeOptions.None;
            try
            {
                if (_runCompletionSource != null && !_runCompletionSource.Task.IsCompleted)
                {
                    throw new InvalidOperationException("A Unity test run is already in progress.");
                }

                if (EditorApplication.isPlaying || EditorApplication.isPlayingOrWillChangePlaymode)
                {
                    throw new InvalidOperationException("Cannot start a test run while the Editor is in or entering Play Mode. Stop Play Mode and try again.");
                }

                if (mode == TestMode.PlayMode)
                {
                    // PlayMode runs transition the editor into play across multiple update ticks. Unity's
                    // built-in pipeline schedules SaveModifiedSceneTask early, but that task uses
                    // EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo which throws once play mode is
                    // active. To minimize that window we pre-save dirty scenes and disable domain reload (so the
                    // MCP bridge stays alive). We do NOT force runSynchronously here because that can freeze the
                    // editor in some projects. If the TestRunner still hits the save task after entering play, the
                    // run can fail; in that case, rerun from a clean Edit Mode state.
                    adjustedPlayModeOptions = EnsurePlayModeRunsWithoutDomainReload(
                        out originalEnterPlayModeOptionsEnabled,
                        out originalEnterPlayModeOptions);
                }

                _leafResults.Clear();
                _runCompletionSource = new TaskCompletionSource<TestRunResult>(TaskCreationOptions.RunContinuationsAsynchronously);

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Stop Play Mode (click Stop, or EditorApplication.ExitPlaymode) and wait for isPlaying to become false before retrying.
  2. Gate run_tests on !EditorApplication.isPlaying on the caller side.
  3. Prefer EditMode tests when play state is uncertain.

Example fix

// before
await tests.RunTestsAsync(TestMode.EditMode, opts); // throws if editor in Play Mode

// after
if (EditorApplication.isPlaying || EditorApplication.isPlayingOrWillChangePlaymode)
    return Error("Stop Play Mode before running tests.");
await tests.RunTestsAsync(TestMode.EditMode, opts);
Defensive patterns

Strategy: validation

Validate before calling

if (EditorApplication.isPlaying || EditorApplication.isPlayingOrWillChangePlaymode)
    return ErrorResponse("Stop Play Mode before running tests.");
await tests.RunTestsAsync(mode, opts);

Type guard

static bool EditorIsIdleForTests()
    => !EditorApplication.isPlaying && !EditorApplication.isPlayingOrWillChangePlaymode;

Try / catch

try { await tests.RunTestsAsync(mode, opts); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Play Mode"))
{ EditorApplication.ExitPlaymode(); /* then retry once idle */ }

Prevention

When it happens

Trigger: User manually entered Play Mode then called run_tests; a previous PlayMode test run left the editor in play; an EditMode run requested while the editor is mid-play-transition.

Common situations: Developer clicked the Play button to test something and forgot to stop it; a prior PlayMode run exited tests but left play mode on; automation that doesn't gate on play state.

Related errors


AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13). Data as JSON: /api/errors/f8500fe397778ccf. Report an issue: GitHub.