- Home /
Change BuildSettings before run Unit test in Play Mode
I need to create different builds of the project I'm working on.
My builds differs one from the other from some scenes. So I need to change them for each build.
The point is that I woud like to test some workflows of my project with the different builds settings. I've tryed something like this:
using NUnit.Framework;
using UnityEditor;
namespace Tests
{
public class TestRunner
{
private string oldScenePath;
[SetUp]
public override void Setup()
{
EditorBuildSettingsScene[] newScenes = EditorBuildSettings.scenes;
oldScenePath = newScenes[3].path;
newScenes[3].path = "scene/to/change.unity";
EditorBuildSettings.scenes = newScenes;
}
[TearDown]
public override void Teardown()
{
EditorBuildSettingsScene[] newScenes = EditorBuildSettings.scenes;
newScenes[3].path = oldScenePath;
EditorBuildSettings.scenes = newScenes;
}
[UnityTest]
public override string Test()
{
//Make things
}
}
}
But this does not work, because SetUp is run during running mode, where I can not modify the EditorBuildSettings.
Any advice? Is it possible to make run a script before Unity enters in Play mode before running all the tests?
Answer by Warnecke · Feb 21, 2020 at 04:25 PM
It sounds like you could use an IPrebuildSetup and IPostBuildCleanup for this. https://docs.unity3d.com/Packages/com.unity.test-framework@1.1/manual/reference-setup-and-cleanup.html
You should be able to modify the EditorBuildSettings inside those Setup and Cleanup methods. Note that UnityEditor is not available when building for a player, so you will need to surround the EditorBuildSettings with #if UNITY_EDITOR, like in the example in the link.