Last active
November 11, 2018 08:01
-
-
Save thebeardphantom/76d6dd960851e1f388bbbd92d25d7b56 to your computer and use it in GitHub Desktop.
How to execute edit mode tests from script. Only subscribes to finished events. Requires that you can reference test assemblies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void ExecuteTests() | |
{ | |
// Select all types in appdomain | |
var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).ToArray(); | |
// Need to create an empty runner filter | |
var filterType = types.First(t => t.Name == "TestRunnerFilter"); | |
var filter = Activator.CreateInstance(filterType); | |
// Create a test launcher | |
var launcherType = types.First(t => t.Name == "EditModeLauncher"); | |
var launcher = Activator.CreateInstance(launcherType, filter, TestPlatform.EditMode); | |
// Grab the runner from the launcher to subscribe to test events | |
var runner = launcherType.GetField("m_EditModeRunner", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(launcher); | |
var evt = (UnityEvent<ITestResult>)runner.GetType().GetField("m_TestFinishedEvent", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(runner); | |
evt.AddListener(OnTestComplete); | |
// Finally run all events | |
launcherType.GetMethod("Run").Invoke(launcher, null); | |
} | |
private static void OnTestComplete(ITestResult result) | |
{ | |
// Root level test node in test window | |
if(result.Name == Application.productName | |
&& Equals(result.ResultState, ResultState.Success)) | |
{ | |
// All tests succeeded | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I figured this out so I could integrate running tests into my in-editor build pipeline.
Make sure that if you use an asmdef file for this script that you enable the Test Assemblies references.

If you want to learn more I recommend grabbing a copy of Jetbrains DotPeek and checking out the decompiled source for EditModeRunner.cs in UnityEditor.TestRunner.dll.