Skip to content

Instantly share code, notes, and snippets.

@extrawurst
Created October 30, 2019 09:43
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save extrawurst/cf91a196bb450ef743252bc731c3cd0b to your computer and use it in GitHub Desktop.
Save extrawurst/cf91a196bb450ef743252bc731c3cd0b to your computer and use it in GitHub Desktop.
Unity TestRunnerApi to run UnitTests before each build
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEditor.TestTools.TestRunner.Api;
public class ResultCollector : ICallbacks
{
public ITestResultAdaptor Result { get; private set; }
public void RunFinished(ITestResultAdaptor result)
{
Result = result;
}
public void RunStarted(ITestAdaptor testsToRun) { }
public void TestFinished(ITestResultAdaptor result) { }
public void TestStarted(ITestAdaptor test) { }
}
public class RunTestsBeforeBuild : IPreprocessBuildWithReport
{
public int callbackOrder => 0;
public void OnPreprocessBuild(BuildReport report)
{
Debug.Log("run prebuild editmode tests");
var result = new ResultCollector();
var api = ScriptableObject.CreateInstance<TestRunnerApi>();
api.RegisterCallbacks(result);
api.Execute(new ExecutionSettings
{
runSynchronously = true,
filters = new[]{ new Filter{
testMode = TestMode.EditMode
}}
});
if (result.Result.FailCount > 0)
throw new BuildFailedException($"{result.Result.FailCount} tests failed");
Debug.Log($"tests passed: {result.Result.PassCount}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment