Hi,
I am trying to get understand Unity Test framework.
I have evolved my game from this tutorial: Introduction To Unity Unit Testing | Kodeco
I created a second scene called “Scene2”, then added it to the Build Settings
Initially, I wanted to create a Play Mode Test Class for each scene. With the Set up Changing scenes with this code:
var scene2 = UnityEditor.SceneManagement.EditorSceneManager.GetSceneByName("RW/Scenes/Scene2");
Which returns this error:
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:
[Test]
public void AssertTwoScenesPresentinBuild()
{
int numScenes = UnityEditor.SceneManagement.EditorSceneManager.sceneCount;
Assert.AreEqual(2, numScenes);
string output = "";
for (int i = 0; i < numScenes; i++)
{
var temScene = UnityEditor.SceneManagement.EditorSceneManager.GetSceneAt(i);
output += temScene.name;
output += temScene.isLoaded ? " (Loaded, " : " (Not Loaded, ";
output += temScene.isDirty ? "Dirty, " : "Clean, ";
output += temScene.buildIndex >= 0 ? " in build)\n" : " NOT in build)\n";
Debug.Log("Scene("+i+"):"+ temScene.name);
}
Debug.Log(output);
}
In Edit Mode, the Assert passes, and the Debug Output is:
Scene(0):Scene2
Scene(1):
Scene2 (Loaded, Clean, in build)
(Loaded, Clean, NOT in build)
In Play Mode the assert does not pass.
And the output is:
AssertThereAreTwoScenes (0.298s)
---
Expected: 2
But was: 1
Questions:
-
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. -
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,