Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Resolved "Run test on a new scene" and "Autosave scene" is missing in Editor Test runner

Discussion in 'Testing & Automation' started by Indie Viking, Jan 5, 2016.

  1. Indie Viking

    Indie Viking

    Joined:
    Sep 12, 2013
    Posts:
    28
    Hi
    I cannot find the options "Run tests on a new scene" and "Autosave scene" in Editor Test runner. The manual says these two options is in the Editor Test runner window menu, but in 5.3.1p1 they are missing.

    Where can I find these two options?

    -Thore
     

    Attached Files:

  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,455
    I think the documentation should be updated. Tests are being run on a new scene by default. Before switching to that scene, it prompts you to save the current scene.
     
  3. Indie Viking

    Indie Viking

    Joined:
    Sep 12, 2013
    Posts:
    28
    I was exploring the testing tools and it looked nice to be able to run tests on the current open scene. But after a second thought, it was a bad idea since my tests then depends on that I have the correct scene open in the Editor when they run. And if I really want that functionality, I found out that I can switch to the current open scene by using the SceneManager.

    It looks like unity prompts you to save only when the scene is unnamed. Named scenes is auto saved without prompt.
     
  4. kevinsbk

    kevinsbk

    Joined:
    Apr 23, 2016
    Posts:
    2
    I'm having the same issue. Tests are not being run on a new scene by default. I used EditorSceneManager.OpenScene() to open a scene in one test class, and I noticed that the same scene is being used when I run other test cases right after. How do we ensure that each test case runs on a new scene?
     
  5. Whatever560

    Whatever560

    Joined:
    Jan 5, 2016
    Posts:
    158
    I'm having the same issue. Tests should run independently, as when you run unit tests under VS. But the test scene does not get cleaned after each test is processed.

    Only workaround for now is to manually cleanup the scene between each test ... brrh
     
  6. Whatever560

    Whatever560

    Joined:
    Jan 5, 2016
    Posts:
    158
    Simply make your tests classes inherit from this base class and you'll ensure a new scene is created each time with a game object named for visual references

    Code (CSharp):
    1.     public abstract class BaseUnityTest
    2.     {
    3.         [SetUp]
    4.         public virtual void Init()
    5.         {
    6. #if UNITY_EDITOR
    7.             var scene = UnityEditor.SceneManagement.EditorSceneManager.NewScene(UnityEditor.SceneManagement.NewSceneSetup.EmptyScene, UnityEditor.SceneManagement.NewSceneMode.Single);
    8.             var go = new GameObject("Test "+TestContext.CurrentContext.Test.Name); //you can remove this line is you simply want an empty scene
    9. #endif
    10.         }
    11.  
    12.     }
     
    JasperCiti and liortal like this.
  7. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,455
    I believe the test runner is already doing this in 2017.x
     
  8. Whatever560

    Whatever560

    Joined:
    Jan 5, 2016
    Posts:
    158
    I guess you've missunderstood the issue. A new scene is only created when you're launching the test suite. Though Unit tests and tests in general needs and are intended to be run independently. This is the way you test things. Unity run all the tests in the same new scene, behavior intended is to run EACH test in a new scene.

    The little workaround works perfectly and is the norm in test frameworks, especially if we consider Unity uses Nunit under the hood.

    Unity 2017.4.1f1
     
    a436t4ataf and JasperCiti like this.
unityunity