- Home /
TestRunner playmode IPrebuildSetup test doesn't run
Currently I've got my script set up to run using IPrebuildSetup so I can call the Setup() function to load a scene before the test runs, which so far means my test doesn't run at all. I've put in several Debug.Logs that never fire, so it's clear the test just doesn't run at all.
Here's the code:
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor;
using UnityEngine.TestTools;
using UnityEditor.SceneManagement;
using NUnit.Framework;
using System.Collections;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System;
public class TrainPlayModeTest : IPrebuildSetup
{
Game LocInstance;
SavedPlayer Player;
public void Setup()
{
EditorSceneManager.OpenScene(Application.dataPath + "/Scenes/sceneGame.unity");
}
[UnityTest]
public IEnumerator TrainPlayModeTestWithEnumeratorPasses()
{
Debug.Log("**The test started**");
if(Game.Instance != null)
{
if(LocInstance == null)
{
LocInstance = Game.Instance;
Player = LocInstance.Player;
}
}
if(Player != null)
{
Debug.Log("Test success, player is not null");
yield break;
}
Assert.Fail("Player was null, test failed");
yield return null;
}
}
The scene will successfully switch in Setup() however TrainPlayModeTestWithEnumeratorPasses() never actually fires. Any thoughts?
Edit: Pay no mind to the amount of usings I have, I tend to clean those up when I'm done trying out various methods to get things to work lol
Answer by Warnecke · Jan 05, 2018 at 09:18 AM
The Setup method given with IPrebuildSetup is executed in the editor before the game is build. The EditorSceneManager.OpenScene will change what scene is loaded in the editor. The scene that playmode or the standalone player uses should be set right before the test run. This can be done by using the NUnit Setup and TearDown tags.
using System.Collections;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
public class TrainPlayModeTest
{
private string initialScenePath;
[SetUp]
public void Setup()
{
Debug.Log("Load Scene");
initialScenePath = SceneManager.GetActiveScene().path;
SceneManager.LoadScene("Scenes/sceneGame");
}
[TearDown]
public void TearDown()
{
SceneManager.LoadScene(initialScenePath);
}
[UnityTest]
public IEnumerator TrainPlayModeTestWithEnumeratorPasses2()
{
Debug.Log("**The test started**");
yield return null;
}
}
Two things to note:
The scene that was initially loaded when the Setup of the test is performed should be restored on TearDown. This is done to avoid interfearing with other tests that will be run afterwards.
The scene that you load should be added to the build. This is done in the Build Settings window.
This works for getting the scene loaded so I definitely thank you for that, however the scripts for the scene start executing immediately (because of pre-processing functions) and therefore start throwing nullrefs, is there any way to halt that until the scene is loaded?
The Test Runner basically just executes the method in a coroutine. So it would work just as if you loaded a scene normally. Im pretty sure you need to skip a frame before its loaded. You could also just use the async method and yield until its done.
Documentation https://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene.html