Search Unity

Question How to open an empty scene and reopen the last scene when running Editormode tests.

Discussion in 'Testing & Automation' started by kosigaga, Jul 21, 2020.

  1. kosigaga

    kosigaga

    Joined:
    Oct 12, 2019
    Posts:
    4
    What we have is a [SetUpFixture] with a [OneTimeSetUp] and a [OneTimeTearDown] method. We call the EditorSceneManager.OpenScene(EmptyScenePath, OpenSceneMode.Single) on Setup and it doesn't seem to do anything, the tests are still running on the currently opened scene. The Setup function is definitely called because logs are written to the console.

    We want to be able to run Editormode tests without always having to switch to an empty scene by hand. If there is any other suggestion to achieve this, that would also help.
     
  2. kosigaga

    kosigaga

    Joined:
    Oct 12, 2019
    Posts:
    4
  3. MajeureX

    MajeureX

    Joined:
    May 4, 2020
    Posts:
    13
    What version of Unity are you using? Can you provide sample code? Have you tried running the test in PlayMode instead of EditMode?
     
  4. kosigaga

    kosigaga

    Joined:
    Oct 12, 2019
    Posts:
    4
    I'm using 2019 LTS and no I do not want to use PlayMode. The point is to run tests that don't need PlayMode functionality in EditMode. It is way faster and logical, but EditMode automatically uses the selected scene in the editor which sucks in this regard.
     
  5. MajeureX

    MajeureX

    Joined:
    May 4, 2020
    Posts:
    13
    Without some sample code I can't see why the `EditorSceneManager.OpenScene` method call isn't working as expected. If you can't share your production code, are you able to make a bare minimum example that reproduces the issue?

    In terms of alternative approaches, in my recent project I made sure my EditMode tests were all strictly unit tests. By strict definition, a unit test tests a single unit of a system, for example a single function of a class. Where a given unit has dependencies on other units of code, I use a technique called mocking, where by mock objects stand in place of the real object. Much like a stunt double in film, to the code under test the 'test double' looks like and behaves like real object, but internally it's different. I use a mocking library called NSubstitute to create mock objects for me.

    Using this approach, it should be in principle possible to test your project's code without needing a scene. Doing this in practice is easier said than done, though. If you can give some sample code, I can probably give you some pointers if you're interested in this approach.