Search Unity

Number of Scenes Different in Edit Mode Test, and Play Mode Tests

Discussion in 'Testing & Automation' started by smatalonga, May 23, 2021.

  1. smatalonga

    smatalonga

    Joined:
    May 21, 2021
    Posts:
    2
    Hi,
    I am trying to get understand Unity Test framework.
    I have evolved my game from this tutorial: https://www.raywenderlich.com/9454-introduction-to-unity-unit-testing

    I created a second scene called "Scene2", then added it to the Build Settings

    upload_2021-5-23_12-0-3.png

    Initially, I wanted to create a Play Mode Test Class for each scene. With the Set up Changing scenes with this code:

    Code (CSharp):
    1. var scene2 = UnityEditor.SceneManagement.EditorSceneManager.GetSceneByName("RW/Scenes/Scene2");
    Which returns this error:
    Code (CSharp):
    1. SetUp : System.ArgumentException : SceneManager.SetActiveScene failed; invalid scene
    So to understand the error, I created the same test case, In PlayMode and in Edit mode.

    Code for Edit Mode:
    Code (CSharp):
    1. [Test]
    2.         public void AssertTwoScenesPresentinBuild()
    3.         {
    4.             int numScenes = UnityEditor.SceneManagement.EditorSceneManager.sceneCount;
    5.  
    6.             Assert.AreEqual(2, numScenes);
    7.             string output = "";
    8.  
    9.             for (int i = 0; i < numScenes; i++)
    10.             {
    11.                 var temScene = UnityEditor.SceneManagement.EditorSceneManager.GetSceneAt(i);
    12.                 output += temScene.name;
    13.                 output += temScene.isLoaded ? " (Loaded, " : " (Not Loaded, ";
    14.                 output += temScene.isDirty ? "Dirty, " : "Clean, ";
    15.                 output += temScene.buildIndex >= 0 ? " in build)\n" : " NOT in build)\n";
    16.                 Debug.Log("Scene("+i+"):"+ temScene.name);
    17.             }
    18.             Debug.Log(output);
    19.         }
    In Edit Mode, the Assert passes, and the Debug Output is:
    Code (CSharp):
    1. Scene(0):Scene2
    2. Scene(1):
    3. Scene2 (Loaded, Clean,  in build)
    4. (Loaded, Clean,  NOT in build)
    In Play Mode the assert does not pass.
    And the output is:
    Code (CSharp):
    1. AssertThereAreTwoScenes (0.298s)
    2. ---
    3. Expected: 2
    4.   But was:  1

    Questions:
    1) Why are there a different number of scenes?
    Note: I know PlayModeTest and Edit mode test are in different projects within the solution. But I don't remember updating anything special in the EditModeTest project.

    2) About the Debug output from the EditMode Test case.
    - According to the Build Setting Dialog, I would have expected that the "Game" scene would be in position "0".
    - The Scene at position 1, is an object (it is not null) but, it does not have the state I would have expected from eh "game" scene.

    I appreciate any pointers on this.
    Thank you,
     
    Last edited: May 23, 2021
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    First of all, looks like you have a deleted scene in the scene list? try removing it.

    Also, the number of scenes should be identical between play mode and edit mode to my understanding.
    Are you sure you saved the project before executing the tests?

    Finally, i am not sure you should load scenes with their full path, (OK i just double checked here), it says that you can load by full path, i usually just use the scene name.

    Were you able to work around this issue ?
     
  3. smatalonga

    smatalonga

    Joined:
    May 21, 2021
    Posts:
    2
    Hi Litoral, Thanks a lot for your reply.

    I removed the deleted scene and run the Build.

    I still have two different outputs from what seems to be the same code. My hunch would tell me that the PlayModeTest Project is running a different version of the game than the EditModeTest project. Yet the assembly the two projects are referencing seems to be the same.


    There are 2 scenes in the EditMode Test Project and 1 in the PlayModeTest Project. And what puzzles me more is that the names of the scenes are not the same:

    Debug output from Edit Mode test:
    Code (CSharp):
    1. Scene(0):Game (Loaded, Clean,  in build)
    2.  
    3. Scene(1): (Loaded, Clean,  NOT in build)
    Debug output from Play Mode Test:
    Code (CSharp):
    1. AssertTwoScenesInPlayModeTest (0.263s)
    2. ---
    3. Expected: 2
    4.   But was:  1
    5. ---
    6. at Tests.GameTestPlayModeTests+<AssertTwoScenesInPlayModeTest>d__4.MoveNext () [0x00163] in C:\--.cs:82
    7. at UnityEngine.TestTools.TestEnumerator+<Execute>d__6.MoveNext () [0x0004c] in C:\\-----
    8. ---
    9. Scene(0):InitTestScene637596113024255120 (Loaded, Clean,  in build)
    My Expectation was that Scene(0) should be called "Game" and Scene(1) should be called "Scene2".

    Anyways, I'll keep looking into this and post here if I find the cause.
    Any input is welcomed.
    Cheers,