Hi,
I’m trying to create tests for the Loading System for my game. It does a lot of additional work before and after using calls to SceneManager.LoadScene().
For my use case, I’ve created three empty test scenes. In order to test the system, I need to have them added to BuildSettings. I would love not to have the need of having them kept there forever though, and to have them present in BuildSettings only when the tests are running.
I wrote this piece of code to add them there, before test runs.
[assembly: TestRunCallback(typeof(SetBuildSettingsTestCallbacks))]
namespace Core.Tests.Editor
{
public class SetBuildSettingsTestCallbacks : ITestRunCallback
{
private EditorBuildSettingsScene[] _originalScenes;
public void RunStarted(ITest testsToRun)
{
_originalScenes = EditorBuildSettings.scenes;
var additionalScenes = TestSettings.Instance.AdditionalScenes;
var newScenes = new EditorBuildSettingsScene[_originalScenes.Length + additionalScenes.Length];
for (int i = 0; i < _originalScenes.Length; i++)
newScenes[i] = _originalScenes[i];
for (int i = 0; i < additionalScenes.Length; i++)
newScenes[_originalScenes.Length + i] = new EditorBuildSettingsScene(additionalScenes[i].FullPath, true);
EditorBuildSettings.scenes = newScenes;
EditorApplication.playModeStateChanged += RestoreOriginalScenesOnExitPlayMode;
}
private void RestoreOriginalScenesOnExitPlayMode(PlayModeStateChange state)
{
if(state == PlayModeStateChange.ExitingPlayMode)
RestoreOriginalScenes();
}
public void RunFinished(ITestResult testResults)
{
RestoreOriginalScenes();
}
private void RestoreOriginalScenes()
{
EditorBuildSettings.scenes = _originalScenes;
}
public void TestStarted(ITest test)
{
//Intentionally left empty.
}
public void TestFinished(ITestResult result)
{
//Intentionally left empty.
}
}
}
The issue is, that even though the settings are modified correctly - and I can verify it by opening BuildSettings when the tests are running - I still get Scene 'SceneToLoad_1' couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded. error. I think it is caused by the RunStarted being invoked in PlayMode instead of before running tests. Is there a way to detect the start of the test run before it enters to PlayMode?
I’ve also tried to use the [SetUp] attribute but it has the same result.
I know about the EditorSceneManager.LoadSceneInPlayMode, for those who would propose to use it instead. I don’t want to use it because that would require modifying the runtime Loading System to use editor code and that is not something that I want to do because it would mean that there is different behaviour during tests.