{!See https://docs.google.com/document/d/1takg_GmIBBKKTj-GHZCwzxohpQz7Bhekivkk72kYMtE/edit for reference implementation of OneTrust, dataLayer and GTM} {!OneTrust Cookies Consent} {!OneTrust Cookies Consent end} {!dataLayer initialization push} {!dataLayer initialization push end} {!Google Tag Manager} {!Google Tag Manager end} Class TestRunnerApi | Test Framework | 2.0.1-exp.2
docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class TestRunnerApi

    The TestRunnerApi retrieves and runs tests programmatically from code inside the project, or inside other packages. TestRunnerApi is a ScriptableObject. You can initialize the API like this:

    var testRunnerApi = ScriptableObject.CreateInstance<TestRunnerApi>();

    You can subscribe and receive test results in one instance of the API, even if the run starts from another instance. The TestRunnerApi supports the following workflows:

    • Running tests programmatically
    • Gettting test results
    • Retrieving the list of tests

    Note: Non-static methods in this class will become obsolete in future versions so you should use the static methods (e.g. RegisterTestCallback, ExecuteTestRun) rather than their non-static equivalents (e.g. RegisterCallbacks, Execute).

    Inheritance
    object
    Object
    ScriptableObject
    TestRunnerApi
    Inherited Members
    ScriptableObject.SetDirty()
    ScriptableObject.CreateInstance(string)
    ScriptableObject.CreateInstance(Type)
    ScriptableObject.CreateInstance<T>()
    Object.GetInstanceID()
    Object.GetHashCode()
    Object.Equals(object)
    Object.Instantiate(Object, Vector3, Quaternion)
    Object.Instantiate(Object, Vector3, Quaternion, Transform)
    Object.Instantiate(Object)
    Object.Instantiate(Object, Transform)
    Object.Instantiate(Object, Transform, bool)
    Object.Instantiate<T>(T)
    Object.Instantiate<T>(T, Vector3, Quaternion)
    Object.Instantiate<T>(T, Vector3, Quaternion, Transform)
    Object.Instantiate<T>(T, Transform)
    Object.Instantiate<T>(T, Transform, bool)
    Object.Destroy(Object, float)
    Object.Destroy(Object)
    Object.DestroyImmediate(Object, bool)
    Object.DestroyImmediate(Object)
    Object.FindObjectsOfType(Type)
    Object.DontDestroyOnLoad(Object)
    Object.DestroyObject(Object, float)
    Object.DestroyObject(Object)
    Object.FindSceneObjectsOfType(Type)
    Object.FindObjectsOfTypeIncludingAssets(Type)
    Object.FindObjectsOfType<T>()
    Object.FindObjectOfType<T>()
    Object.FindObjectsOfTypeAll(Type)
    Object.FindObjectOfType(Type)
    Object.ToString()
    Object.name
    Object.hideFlags
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetType()
    object.MemberwiseClone()
    Namespace: UnityEditor.TestTools.TestRunner.Api
    Assembly: solution.dll
    Syntax
    public class TestRunnerApi : ScriptableObject
    Examples

    Run tests

    Run tests by calling ExecuteTestRun and providing some execution settings in the form of a Filter. The Filter specifies what tests to run. The following is an example of how to run all Play Mode tests in a project:

    var filter = new Filter()
    {
        testMode = TestMode.PlayMode
    };
        TestRunnerApi.ExecuteTestRun(new ExecutionSettings(filter));

    Multiple filter values

    You can specify a more specific filter by filling out the fields on the Filter class in more detail. Many of the fields allow for multiple values. The runner runs any test that matches at least one of the specified values. In this example, the API runs tests with full names that match either of the two names provided:

    TestRunnerApi.ExecuteTestRun(new ExecutionSettings(new Filter()
    {
        testNames = new[] {"MyTestClass.NameOfMyTest", "SpecificTestFixture.NameOfAnotherTest"}
    }));

    Multiple filter fields

    If using multiple different fields on the filter, then the runner runs tests that match each of the different fields. In this example, a test is run if it matches either of the two test names and belongs to an assembly with the specified name:

    TestRunnerApi.ExecuteTestRun(new ExecutionSettings(new Filter()
    {
        assemblyNames = new [] {"MyTestAssembly"},
        testNames = new [] {"MyTestClass.NameOfMyTest", "MyTestClass.AnotherNameOfATest"}
    }));

    Multiple constructor filters

    ExecutionSettings takes one or more filters in its constructor. If there is no filter provided, then it runs all Edit Mode tests by default. If there are multiple filters provided, then a test runs if it matches any of the filters. In this example, it runs any tests in the assembly named MyTestAssembly and any test with a full name matching either of the two provided test names:

    TestRunnerApi.ExecuteTestRun(new ExecutionSettings(
        new Filter()
        {
            assemblyNames = new[] { "MyTestAssembly" },
        },
        new Filter()
        {
            testNames = new[] { "MyTestClass.NameOfMyTest", "MyTestClass.AnotherNameOfATest" }
        }
    ));

    Get test results

    You can receive callbacks when the active test run, or an individual test, starts and finishes. You can register callbacks by invoking RegisterTestCallback with an instance of a class that implements ICallbacks. There are four ICallbacks methods for the start and finish of both the whole run and each level of the test tree. An example of how listeners can be set up:

    Note: Listeners receive callbacks from all test runs, regardless of the registered TestRunnerApi for that instance.

    public void SetupListeners()
    {
        TestRunnerApi.RegisterTestCallback(new MyCallbacks());
    }
    
    private class MyCallbacks : ICallbacks
    {
        public void RunStarted(ITestAdaptor testsToRun)
        {
    
        }
    
        public void RunFinished(ITestResultAdaptor result)
        {
    
        }
    
        public void TestStarted(ITestAdaptor test)
        {
    
        }
    
        public void TestFinished(ITestResultAdaptor result)
        {
           if (!result.HasChildren && result.ResultState != "Passed")
           {
               Debug.Log(string.Format("Test {0} {1}", result.Test.Name, result.ResultState));
           }
        }
    }

    Note: The registered callbacks are not persisted on domain reloads. So it is necessary to re-register the callback after a domain reload, usually with InitializeOnLoad.

    It's possible to provide a priority as an integer as the second argument when registering a callback. This influences the invocation order of different callbacks. The default value is zero. It's also possible to provide RegisterTestCallback with a class instance that implements IErrorCallbacks that is an extended version of ICallbacks. IErrorCallbacks also has a callback method for OnError that invokes if the run fails to start, for example, due to compilation errors or if an IPrebuildSetup throws an exception.

    Retrieve list of tests

    You can retrieve the test tree by invoking RetrieveTestTree with the desired ExecutionSettings and a callback action, with an ITestAdaptor representing the test tree.

    var filter = new Filter()
    {
        assemblyNames = new [] {"myTestAssembly"}
    };
    TestRunnerApi.RetrieveTestTree(new ExecutionSettings(filter), (testRoot) =>
    {
        Debug.Log(string.Format("Tree contains {0} tests.", testRoot.TestCaseCount));
    });

    Methods

    Name Description
    CancelAllTestRuns()

    Cancels all running test runs. Currently only supports EditMode tests.

    CancelTestRun(string)

    Cancel the test run with a given guid. The guid can be retrieved when executing the test run. Currently only supports EditMode tests.

    Execute(ExecutionSettings)

    Starts a test run with a given set of executionSettings.

    ExecuteTestRun(ExecutionSettings)

    Starts a test run with a given set of executionSettings.

    GetActiveRunGuids()

    Provides a list of guids for all test runs that are currently active.

    GetCustomRunnerNames()

    Get all the names of the currently registered custom runners.

    GetExecutionSettings(string)

    Gets the execution settings for a test run with a specific guid.

    IsAnyRunActive()

    Checks if any current test run is currently running.

    IsRunActive(string)

    Checks if a test run with a given guid is active.

    RegisterCallbacks<T>(T, int)

    Sets up a given instance of ICallbacks to be invoked on test runs.

    RegisterCustomRunner(CustomRunnerBase)

    Registers an implementation of CustomRunnerBase with the test framework, allowing for running custom test frameworks. Since custom runners are identified by name, the provided custom runner must use a name that has not already been registered.

    RegisterTestCallback<T>(T, int)

    Sets up a given instance of ICallbacks to be invoked on test runs.

    RetrieveTestList(TestMode, Action<ITestAdaptor>)

    Retrieve the full test tree as ITestAdaptor for a given test mode.

    RetrieveTestTree(ExecutionSettings, Action<ITestAdaptor>)

    Retrieve a list of tests in a tree structure matching a given execution settings.

    SaveResultToFile(ITestResultAdaptor, string)

    Save a given set of ITestResultAdaptor to a file at the provided file path.

    UnregisterCallbacks<T>(T)

    Unregister an instance of ICallbacks to no longer receive callbacks from test runs.

    UnregisterCustomRunner(CustomRunnerBase)

    Unregisters a custom runner from the test framework.

    UnregisterTestCallback<T>(T)

    Unregister an instance of ICallbacks to no longer receive callbacks from test runs.

    In This Article
    Back to top
    Copyright © 2023 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)