{"record":{"id":"f8500fe397778ccf","repo":"CoplayDev/unity-mcp","slug":"cannot-start-a-test-run-while-the-editor-is-in-or","errorCode":null,"errorMessage":"Cannot start a test run while the Editor is in or entering Play Mode. Stop Play Mode and try again.","messagePattern":"Cannot start a test run while the Editor is in or entering Play Mode\\. Stop Play Mode and try again\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MCPForUnity/Editor/Services/TestRunnerService.cs","lineNumber":204,"sourceCode":"        }\n\n        public async Task<TestRunResult> RunTestsAsync(TestMode mode, TestFilterOptions filterOptions = null)\n        {\n            await _operationLock.WaitAsync().ConfigureAwait(true);\n            Task<TestRunResult> runTask;\n            bool adjustedPlayModeOptions = false;\n            bool originalEnterPlayModeOptionsEnabled = false;\n            EnterPlayModeOptions originalEnterPlayModeOptions = EnterPlayModeOptions.None;\n            try\n            {\n                if (_runCompletionSource != null && !_runCompletionSource.Task.IsCompleted)\n                {\n                    throw new InvalidOperationException(\"A Unity test run is already in progress.\");\n                }\n\n                if (EditorApplication.isPlaying || EditorApplication.isPlayingOrWillChangePlaymode)\n                {\n                    throw new InvalidOperationException(\"Cannot start a test run while the Editor is in or entering Play Mode. Stop Play Mode and try again.\");\n                }\n\n                if (mode == TestMode.PlayMode)\n                {\n                    // PlayMode runs transition the editor into play across multiple update ticks. Unity's\n                    // built-in pipeline schedules SaveModifiedSceneTask early, but that task uses\n                    // EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo which throws once play mode is\n                    // active. To minimize that window we pre-save dirty scenes and disable domain reload (so the\n                    // MCP bridge stays alive). We do NOT force runSynchronously here because that can freeze the\n                    // editor in some projects. If the TestRunner still hits the save task after entering play, the\n                    // run can fail; in that case, rerun from a clean Edit Mode state.\n                    adjustedPlayModeOptions = EnsurePlayModeRunsWithoutDomainReload(\n                        out originalEnterPlayModeOptionsEnabled,\n                        out originalEnterPlayModeOptions);\n                }\n\n                _leafResults.Clear();\n                _runCompletionSource = new TaskCompletionSource<TestRunResult>(TaskCreationOptions.RunContinuationsAsynchronously);","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/MCPForUnity/Editor/Services/TestRunnerService.cs#L186-L222","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Stop Play Mode (click Stop, or EditorApplication.ExitPlaymode) and wait for isPlaying to become false before retrying.","Gate run_tests on !EditorApplication.isPlaying on the caller side.","Prefer EditMode tests when play state is uncertain."],"exampleFix":"// before\nawait tests.RunTestsAsync(TestMode.EditMode, opts); // throws if editor in Play Mode\n\n// after\nif (EditorApplication.isPlaying || EditorApplication.isPlayingOrWillChangePlaymode)\n    return Error(\"Stop Play Mode before running tests.\");\nawait tests.RunTestsAsync(TestMode.EditMode, opts);","handlingStrategy":"validation","validationCode":"if (EditorApplication.isPlaying || EditorApplication.isPlayingOrWillChangePlaymode)\n    return ErrorResponse(\"Stop Play Mode before running tests.\");\nawait tests.RunTestsAsync(mode, opts);","typeGuard":"static bool EditorIsIdleForTests()\n    => !EditorApplication.isPlaying && !EditorApplication.isPlayingOrWillChangePlaymode;","tryCatchPattern":"try { await tests.RunTestsAsync(mode, opts); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Play Mode\"))\n{ EditorApplication.ExitPlaymode(); /* then retry once idle */ }","preventionTips":["Gate run_tests on !EditorApplication.isPlaying on the caller side.","Stop Play Mode and wait for isPlaying to become false before retrying.","Prefer EditMode tests when the editor's play state is uncertain."],"tags":["test-runner","play-mode","state","unity-editor"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}