Skip to content

Test Runner fails in Play Mode #367

Closed

Description

@bazzboyy1

There seems to be anm issue with the UnityMCP - run_tests (MCP)(mode: "play") command.

Basically if I try have claude code run the test runner, the test runner throws exceptions. When I run the tests manually via the GUI, it works fine.

It's pretty important that this works for iteration purposes.

I sincerely apologise for this horrendous console dump:

UnityMCP - read_console (MCP)(action: "get", count: 50, format: "detailed")
⎿ {
"success": true,
"message": "Retrieved 9 log entries.",
"data": [
{
"type": "Log",
"message": "<color=#2EA3FF>MCP-FOR-UNITY: MCPForUnityBridge started on port 6400.
(OS=WindowsEditor, server=6.3.0)",
"file": "./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038be/Editor/Helpers/McpLog.cs",
"line": 20,
"stackTrace": "UnityEngine.Debug:Log (object)\nMCPForUnity.Editor.Helpers.McpLog:Info (string,bool) (at
./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038be/Editor/Helpers/McpLog.cs:20)\nMCPForUnity.Editor.MC
PForUnityBridge:Start () (at ./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038be/Editor/MCPForUnityBrid
ge.cs:395)\nMCPForUnity.Editor.MCPForUnityBridge:EnsureStartedOnEditorIdle () (at
./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038be/Editor/MCPForUnityBridge.cs:267)\nUnityEditor.Edito
rApplication:Internal_CallUpdateFunctions ()"
},
{
"type": "Log",
"message": "<color=#2EA3FF>MCP-FOR-UNITY: Auto-discovered 10 tools and 3 resources (13
total handlers)",
"file": "./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038be/Editor/Helpers/McpLog.cs",
"line": 20,
"stackTrace": "UnityEngine.Debug:Log (object)\nMCPForUnity.Editor.Helpers.McpLog:Info (string,bool) (at
./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038be/Editor/Helpers/McpLog.cs:20)\nMCPForUnity.Editor.To
ols.CommandRegistry:AutoDiscoverCommands () (at ./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038be/Edi
tor/Tools/CommandRegistry.cs:101)\nMCPForUnity.Editor.Tools.CommandRegistry:Initialize () (at
./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038be/Editor/Tools/CommandRegistry.cs:50)\nMCPForUnity.Ed
itor.MCPForUnityBridge:Start () (at ./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038be/Editor/MCPForUn
ityBridge.cs:399)\nMCPForUnity.Editor.MCPForUnityBridge:EnsureStartedOnEditorIdle () (at
./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038be/Editor/MCPForUnityBridge.cs:267)\nUnityEditor.Edito
rApplication:Internal_CallUpdateFunctions ()"
},
{
"type": "Log",
"message": "<color=#2EA3FF>MCP-FOR-UNITY: No PythonToolsAsset found. Create one via Assets
> Create > MCP For Unity > Python Tools",
"file": "./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038be/Editor/Helpers/McpLog.cs",
"line": 20,
"stackTrace": "UnityEngine.Debug:Log (object)\nMCPForUnity.Editor.Helpers.McpLog:Info (string,bool) (at
./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038be/Editor/Helpers/McpLog.cs:20)\nMCPForUnity.Editor.Se
rvices.ToolSyncService:SyncProjectTools (string) (at ./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038b
e/Editor/Services/ToolSyncService.cs:32)\nMCPForUnity.Editor.Helpers.PythonToolSyncProcessor:SyncAllTools ()
(at ./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038be/Editor/Helpers/PythonToolSyncProcessor.cs:96)\n
MCPForUnity.Editor.Helpers.PythonToolSyncProcessor/<>c:<.cctor>b__2_0 () (at
./Library/PackageCache/com.coplaydev.unity-mcp@2ab49ec038be/Editor/Helpers/PythonToolSyncProcessor.cs:29)\nUnit
yEditor.EditorApplication:Internal_CallDelayFunctions ()"
},
{
"type": "Exception",
"message": "InvalidOperationException: This cannot be used during play mode.",
"file": "./Library/PackageCache/com.unity.test-framework@5ac417e07314/UnityEditor.TestRunner/TestRun/Task
s/Scene/SaveModifiedSceneTask.cs",
"line": 13,
"stackTrace": "UnityEditor.SceneManagement.EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo
(System.String message) (at <08b6666d108d4c9796450fb85df4d5b2>:0)\nUnityEditor.SceneManagement.EditorSceneManag
er.SaveCurrentModifiedScenesIfUserWantsTo () (at <08b6666d108d4c9796450fb85df4d5b2>:0)\nUnityEditor.TestTools.T
estRunner.TestRun.Tasks.Scene.SaveModifiedSceneTask+d__1.MoveNext () (at
./Library/PackageCache/com.unity.test-framework@5ac417e07314/UnityEditor.TestRunner/TestRun/Tasks/Scene/SaveMod
ifiedSceneTask.cs:13)\nUnityEditor.TestTools.TestRunner.TestRun.TestJobRunner.ExecuteStep () (at
./Library/PackageCache/com.unity.test-framework@5ac417e07314/UnityEditor.TestRunner/TestRun/TestJobRunner.cs:17
3)\nUnityEditor.EditorApplication:Internal_CallUpdateFunctions()"
},
{
"type": "Log",
"message": "An unexpected error happened while running tests.",
"file": "./Library/PackageCache/com.unity.test-framework@5ac417e07314/UnityEditor.TestRunner/Api/Callback
sDelegator.cs",
"line": 67,
"stackTrace": "UnityEditor.EditorApplication:Internal_CallUpdateFunctions ()"
},
{
"type": "Log",
"message": "Too many instant steps in test execution mode: Error. Current task ExitPlayModeTask.",
"file":
"./Library/PackageCache/com.unity.test-framework@5ac417e07314/UnityEditor.TestRunner/TestRun/TestJobRunner.cs",
"line": 119,
"stackTrace": "UnityEditor.EditorApplication:Internal_CallUpdateFunctions ()"
},
{
"type": "Exception",
"message": "Exception: Playmode tests were aborted because the player was stopped.",
"file": "./Library/PackageCache/com.unity.test-framework@5ac417e07314/UnityEditor.TestRunner/TestRun/Task
s/PlayModeRunTask.cs",
"line": 59,
"stackTrace": "UnityEditor.TestTools.TestRunner.TestRun.Tasks.PlayModeRunTask+d__1.MoveNext ()
(at ./Library/PackageCache/com.unity.test-framework@5ac417e07314/UnityEditor.TestRunner/TestRun/Tasks/PlayModeR
unTask.cs:59)\nUnityEditor.TestTools.TestRunner.TestRun.TestJobRunner.ExecuteStep () (at
./Library/PackageCache/com.unity.test-framework@5ac417e07314/UnityEditor.TestRunner/TestRun/TestJobRunner.cs:17
3)\nUnityEditor.EditorApplication:Internal_CallUpdateFunctions()"
},
{
"type": "Log",
"message": "An unexpected error happened while running tests.",
"file": "./Library/PackageCache/com.unity.test-framework@5ac417e07314/UnityEditor.TestRunner/Api/Callback
sDelegator.cs",
"line": 67,
"stackTrace": "UnityEditor.EditorApplication:Internal_CallUpdateFunctions ()"
},
{
"type": "Warning",
"message": "Executing IPostBuildCleanup for: Unity.PerformanceTesting.Editor.TestRunBuilder.",
"file": "./Library/PackageCache/com.unity.test-framework@5ac417e07314/UnityEditor.TestRunner/TestRun/Task
s/BuildActionTaskBase.cs",
"line": 51,
"stackTrace": "UnityEditor.EditorApplication:Internal_CallUpdateFunctions ()"
}
]
}

Activity

  1. coderabbitai commented on Nov 4, 2025

    @coderabbitai
    Contributor

    A summary of the changes CodeRabbit can apply:

    • Update MCPForUnity/Editor/Services/TestRunnerService.cs to set ExecutionSettings.runSynchronously = true when mode == TestMode.PlayMode so PlayMode tests run synchronously, preventing the SaveModifiedSceneTask InvalidOperationException and leaving EditMode behavior unchanged.

    • Update MCPForUnity/Editor/Services/TestRunnerService.cs to replace the direct _testRunnerApi.Execute(...) call with a new ExecutionSettings instance (including a comment/link) and set settings.runSynchronously = true when mode == TestMode.PlayMode before calling _testRunnerApi.Execute(settings) to prevent scene save conflicts during PlayMode test runs.

    • ✅ Create PR with these edits
    • 📋 Get copyable edits
  2. msanatan commented on Nov 5, 2025

    @msanatan
    Member

    @bazzboyy1 you can use markdown tags to help format the dump, like ``` (https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax)

    Thanks for flagging! With the test runner I've mostly used it for editor mode tests, less so for Play mode. It looks like our AI helper might has some ideas but I'm not sure that's the actual fix.

    Why do you have SaveModifiedSceneTask.cs? That file is calling SaveCurrentModifiedScenesIfUserWantsTo(), which is not available in Play mode. It's an error with your code and setup.

    Instead of using that file, you can save the scene manually or instruct the LLM to do so before running tests

  3. msanatan commented on Nov 8, 2025

    @msanatan
    Member

    @bazzboyy1 let me know, I'll close by Monday. Logs indicate it's an error in your project

  4. bazzboyy1 commented on Nov 9, 2025

    @bazzboyy1
    Author

    Hey so I'm not calling this from my code, it's from the Unity Test Runner. It's the one trying to save the scene. I've tried running this in an empty scene and I get the same result.

    InvalidOperationException: This cannot be used during play mode. UnityEditor.SceneManagement.EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo (System.String message) (at <08b6666d108d4c9796450fb85df4d5b2>:0) UnityEditor.SceneManagement.EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo () (at <08b6666d108d4c9796450fb85df4d5b2>:0) UnityEditor.TestTools.TestRunner.TestRun.Tasks.Scene.SaveModifiedSceneTask+<Execute>d__1.MoveNext () (at ./Library/PackageCache/com.unity.test-framework@5ac417e07314/UnityEditor.TestRunner/TestRun/Tasks/Scene/SaveModifiedSceneTask.cs:13) UnityEditor.TestTools.TestRunner.TestRun.TestJobRunner.ExecuteStep () (at ./Library/PackageCache/com.unity.test-framework@5ac417e07314/UnityEditor.TestRunner/TestRun/TestJobRunner.cs:173) UnityEditor.EditorApplication:Internal_CallUpdateFunctions()

    Could this be a bug with the Unity API? I'm running Unity V6.0.4

  5. msanatan commented on Nov 10, 2025

    @msanatan
    Member

    Just to be clear, @bazzboyy1, is your scene saved before running the test?

  6. bazzboyy1 commented on Nov 11, 2025

    @bazzboyy1
    Author

    yep @msanatan, I've tried running it in an empty scene too with the same result. Not an empty project though which I suppose I could try

  7. FriedrichWessel commented on Nov 11, 2025

    @FriedrichWessel

    I see the same issue.
    From a quick scan: The most likely issue is the TestRunnerService:74

    The TestRunnerAPI's internal task pipeline includes the SaveModifiedSceneTask as part of its scene setup/teardown process.
    This calls: EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()
    This is not available during Playmode - so most likely the timing here is off when running via TestRunner instead of running with the UI.
    Since it also happens when the scene is not marked as dirty - saving the scene upfront wont help - but its maybe worth a try:

    var scenes = UnityEngine.SceneManagement.SceneManager.GetAllScenes();
    foreach (var scene in scenes)
    {
        if (scene.isDirty)
        {
            EditorSceneManager.SaveScene(scene);
        }
    }
    

    other option would be to try monkey-path with reflection ( pretty hacky - but could proof my theory )

    using UnityEditor.SceneManagement;
    using System.Reflection;
    
    // Call this before TestRunnerAPI.Execute()
    private static void DisableSaveModifiedScenesCheck()
    {
        var method = typeof(EditorSceneManager).GetMethod(
            "SaveCurrentModifiedScenesIfUserWantsTo",
            BindingFlags.Public | BindingFlags.Static,
            null,
            System.Type.EmptyTypes,
            null
        );
        
        if (method != null)
        {
            // Create a no-op delegate that returns true (proceeds without saving)
            var methodHandle = method.MethodHandle;
            RuntimeHelpers.PrepareMethod(methodHandle);
        }
    }
    

    That should at least tell us if thats the issue.

    I will see if I find some time to test those in the next days - if no one else wants to pick it up before.

  8. changed the title [-]Test Runner exceptions when the GUI does not.[/-] [+]Test Runner fails in Play Mode[/+] on Nov 25, 2025
  9. msanatan commented on Nov 26, 2025

    @msanatan
    Member

    Didn't mean to mark as completed, but it should be fixed in v8.0.1. Can someone else update and confirm?

    @bazzboyy1 @FriedrichWessel @Redhaozzz @tribbloid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions