Hello guys,
I have a quick question.
I wrote a couple of tests using the unity test runner tool. All of them are Editor testing, so no need to run in play mode.
Now I was wondering if is possible to run those testing in multiple scenes. Because now, when I press play, only the loaded scene is been tested, but I want to run those test on multiple scenes instead of manually switch scene every time.
Maybe by using some terminal tool is possible to tell Unity to do us… any ideas?
Thanks, but how do I use it?
Assume I have 10 scene to test, I load one scene and the test starts automatically. But when the test is finished, how do i move to next scene to test?
I need something that can iterate through a list of scenes and run the test in each scene.
public class LevelValidationTests
{
public static IEnumerable<string> LevelTestCases
{
get { return new List<string> {"Level-1", "Level-2"}; }
}
[UnityTest]
public IEnumerator LevelIsValid([ValueSource("LevelTestCases")] string levelName)
{
yield return LoadLevel(levelName);
Assert.IsTrue(true);
}
[TearDown]
public void UnloadLevel()
{
SceneManager.UnloadSceneAsync(sceneToUnload);
}
private string sceneToUnload;
private IEnumerator LoadLevel(string levelName)
{
sceneToUnload = levelName;
var loadSceneOperation = SceneManager.LoadSceneAsync(levelName);
loadSceneOperation.allowSceneActivation = true;
while (!loadSceneOperation.isDone)
yield return null;
}
}
The code should run fine in both edit and play modes.
Also scene names doesn’t have to be hardcoded, LevelTestCases can contain any code you wish. I am for example getting my scene names from scriptable object.
I run my test through an editor script, here a simple example class:
using UnityEngine;
using NUnit.Framework;
public class PlayerTester
{
[Test]
public void OnlyOnePlayerInTheScene()
{
Assert.IsTrue(GameObject.FindGameObjectsWithTag(ProjectIds.TagIDs.Player).Length == 1, "There are " + GameObject.FindGameObjectsWithTag(ProjectIds.TagIDs.Player).Length + " player in the scene");
}
[Test]
public void OnlyOnePlayerMoving()
{
Assert.IsTrue(GameObject.FindObjectsOfType<PlayerMoving>().Length == 1, "Error: There are " + GameObject.FindObjectsOfType<PlayerMoving>().Length + " player in the scene");
}
}
Then I use test runner in edit mode to run the test
Any idea how to run this code in every scene and in case something fails, how to know which one is the scene who failed?
Thanks again
Thanks for the answer, I see your point on load one or more scene, but the question is how can I run my tests on the scenes that I’ve loaded?
I have 5 classes for example, and each one performs some kind of test. I want to load a scene and run those test, then if everything is all right, move to the next scene an do the same.
You CAN run all the tests on all the scenes automatically using the solution I sent you. You just have to at the start of each test to load a scene and after the test is finished to unload it. And that is all right, because each test should be atomic.