Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Loading Scene twice in play mode tests causes Editor to crash

Discussion in 'Testing & Automation' started by jessfdm_sky, Sep 30, 2019.

  1. jessfdm_sky

    jessfdm_sky

    Joined:
    Nov 19, 2018
    Posts:
    12
    Hi,

    I'm trying to write tests for a scene. Because a lot of my scene is already in place and I need to check positions I wanted to load the scene up at test-time and perform tests on it. The issue I'm having it I use
    SceneManagement.LoadScene([scene name], LoadSceneMode.Single)
    at the moment to load the scene at the start of each test, but doing this causes the Editor itself to crash when I run all the tests. From looking at the logs (putting Debug.Log()s before and after the load) I can tell this happens exactly as I try and load the scene over itself, but it doesn't have any information as to what actually causes the crash.

    If I run the tests individually they're fine, and if I put a check in to see if the scene is already loaded at the start of each test then everything works. But this isn't ideal as I'd like to run the tests on a fresh scene each time. I can't achieve this with Tear downs and set ups either as I can't unload a scene if it's the only one loaded (according to an error message I get in the console if I try to do it).

    What's the best way to run the tests within a scene across multiple files with different test cases in them and reset it between tests?
     
  2. Warnecke

    Warnecke

    Unity Technologies

    Joined:
    Nov 28, 2017
    Posts:
    92
    It might not be the most optimal, but you could register the original scene name before your test runs and then reopen that scene again after your test have run. This would typically be the default test scene.
     
  3. jessfdm_sky

    jessfdm_sky

    Joined:
    Nov 19, 2018
    Posts:
    12
    Where abouts do I register the scene? I've added it as the only scene to the build settings
     
  4. Warnecke

    Warnecke

    Unity Technologies

    Joined:
    Nov 28, 2017
    Posts:
    92
    by that I mean to put the name into a local variable and then use the SceneManagement api to reopen it in the TearDown
     
  5. jessfdm_sky

    jessfdm_sky

    Joined:
    Nov 19, 2018
    Posts:
    12
    So I've been working on this for most of today and still couldn't find out why that solution didn't fix the silent crashing. Finally I've found that there was actually an exception being thrown somewhere on the scene loading over itself and the catch clause it fell to told the entire process to exit (all of this was within our own code, not Unity's), which explains why it was closing and leaving no trace. Turns out to be nothing really to do with the test runner and just be that the exception is thrown when the scene tries to load over itself (which we'd never tried to do before).

    Thanks for you help though :) After fixing the issue I can now just load over the Scene as usual standard play mode so no need for a work around.