Is it possible to pause a test and explore the scene in the editor?

I can't figure out a way to pause a test while it's running and explore the scene in the editor. If I could do this, I think I could have saved 16 hours of time on this project so far. Is there any way to do this?

Whenever a [UnityTest] gives me an unexpected error, I could save a bunch of time if I could just look at the scene and figure out what's going on. Someone taught me one trick:

while (true) yield return null;

This loop will allow me to see what's happening in the game view. I can put that right above a failing assertion and see what's rendered on the screen. That helps in certain situations, but in other situations, nothing beats being able to explore the hierarchy and look at the inspector. Is there any way to do that?

1 Like

I guess you could run the unit test method from an editor script instead of through the test runner?

1 Like

So, you can kinda do this in Playmode tests today, by calling Debug.Break() in your test. It's fiddly though - the break will only take effect at the next 'yield', and the test needs to not conclude before that happens (either from an assertion failure, or from reaching the end of your method). This test does sort of what you're looking for:

        [UnityTest]
        public IEnumerator DirtyingUnityTest()
        {
            new GameObject("Some scene object");
            yield return null;

            try
            {
                Assert.Fail();
            }
            catch (AssertionException ex)
            {
                LogAssert.ignoreFailingMessages = true;
                Debug.LogException(ex);
                Debug.Break();
            }

            yield return null;
        }

When I run this as a Playmode test inside the Editor, it pauses and I'm able to inspect "Some scene object" in the Inspector. This is clearly pretty messy, though!

For Editmode tests, sadly the same trick doesn't work. Perhaps you could temporarily mark your tests as playmode tests (by just enabling them to run on at least one more platform) and use this trick.

I think it's a good idea, though! We should build 'pause on error' functionality into the framework directly...

6 Likes

[quote=“Baste”, post:2, topic: 734189]
I guess you could run the unit test method from an editor script instead of through the test runner?
[/quote]

That’s an interesting idea. But if I do that, wouldn’t the scene be all messed up from the changes in my test?

[quote=“superpig”, post:3, topic: 734189]
For Editmode tests, sadly the same trick doesn’t work. Perhaps you could temporarily mark your tests as playmode tests (by just enabling them to run on at least one more platform) and use this trick.
[/quote]

I’ve never successfully got a playmode test to work. You’re saying all I have to do is check one more of these?


I think I’d have to uncheck the Editor box, too, but I’m not sure.

I would love that, though all I really would need is Debug.Break(); to work in edit mode tests. Or, if there were some way to say, “don’t reset the scene after the test” that would work, too. I could just put a return statement right before the failing assert. Of course, I’d need a way to clean up the mess I’ve made when I’m done inspecting the situation. I’m happy to create a feature request around this if that helps.

1 Like

[quote=“superpig”, post:3, topic: 734189]
For Editmode tests, sadly the same trick doesn’t work.

I think it’s a good idea, though! We should build ‘pause on error’ functionality into the framework directly…
[/quote]

8 months later … did this make it into a future build (it’s not in 2019.2 - or am I looking in the wrong place?)

Also, incidentally … Unity deleted the TestRunner docs in 2019.2 onwards ( I’m using the 2019.1 archived version here - https://docs.unity3d.com/2019.1/Documentation/Manual/testing-editortestsrunner.html ) although the 2019.1 docs seem to be valid for my experience in 2019.2

I'd also be interested in a feature that allowed inspecting the scene state for failed tests.

I used Superpig's suggestion to debug my test, but it's really fiddly setting this up for edit mode tests. I feel like most of the time it would be enough to inspect the scene after a test has finished if that makes it easier to implement.

[quote=“a436t4ataf”, post:6, topic: 734189]
8 months later … did this make it into a future build (it’s not in 2019.2 - or am I looking in the wrong place?)
[/quote]Nope, we’ve not looked at this internally yet.

[quote]
Also, incidentally … Unity deleted the TestRunner docs in 2019.2 onwards ( I’m using the 2019.1 archived version here - https://docs.unity3d.com/2019.1/Documentation/Manual/testing-editortestsrunner.html ) although the 2019.1 docs seem to be valid for my experience in 2019.2
[/quote]They moved to the package docs here: https://docs.unity3d.com/Packages/com.unity.test-framework@1.1/manual/index.html

1 Like

Well, it's now 18 months since I asked the question :), and I stopped using EditorTests entirely - I don't think anyone else I work with uses them either. Without basic functionality like this, there's not much point to them (yes, they're much faster to startup, but ... as soon as one fails, you then spend more time than you saved in total, by trying to debug the un-debuggable).

I hate having to do this multiple times an hour, but its still quicker to use Superpig's method and change:

[Test]
public void TestAThing()
{
   // do stuff
   // do more stuff
}

to:

[[B]Unity[/B]Test]
public [B]IEnumerator[/B] TestAThing()
{
   // do stuff
   [B]Debug.Break();
   yield return null;[/B]
   // do more stuff
}

(and change it back again after you fix the test ... otherwise your testrunner will pause every. Time. You. Run. Any. Test ...than to waste time using your mental powers to try and guess what has changed/broken in a failing test.

From what I recall ... the current plan is to make Play tests run super fast, implying EditorTests are going to be deleted / obsoleted anyway? I don't mind, so long as we get something that works, and I can stop having to delete/undelete those 3 changes over and over again.

(it's possible to convert everything to UnityTests, and have a "yield break;" at the end of every test - and if I had a large team of first-time junior engineers who'd never learned to unit testing before, I'd probably force them to go that way - but then you have to teach everyone the subtelties that 'everything is now a coroutine' and teach them to stop using the standards for test control ([Setup] etc), otherwise things break in non-obvious ways. ... and you still need to keep manually adding two lines everytime a test fails, and then deleting them as soon as it passes.)

My workaround was to save the scene created during the test into a file, and open it after.
Sounds silly, but it worked for me.

bool saveOK = EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene(), "Assets/test.unity");
4 Likes