Rasagar/Library/PackageCache/com.unity.test-framework/Documentation~/reference-attribute-testruncallback.md
2024-08-26 23:07:20 +03:00

1.8 KiB

TestRunCallback attribute

It is possible for the test framework to invoke callbacks as the current test run progresses. To do this, there is a TestRunCallback attribute which takes the type of ITestRunCallback implementation. You can invoke the callbacks with NUnit ITest and ITestResult classes.

At the RunStarted and RunFinished methods, the test and test results are for the whole test tree. These methods invoke at each node in the test tree; first with the whole test assembly, then with the test class, and last with the test method.

From these callbacks, it is possible to read the partial or the full results, and it is furthermore possible to save the XML version of the result for further processing or continuous integration.

Example

using NUnit.Framework.Interfaces;
using UnityEngine;
using UnityEngine.TestRunner;

[assembly:TestRunCallback(typeof(MyTestRunCallback))]

public class MyTestRunCallback : ITestRunCallback
{
    public void RunStarted(ITest testsToRun)
    {
        
    }

    public void RunFinished(ITestResult testResults)
    {
        
    }

    public void TestStarted(ITest test)
    {
        
    }

    public void TestFinished(ITestResult result)
    {
        if (!result.Test.IsSuite)
        {
            Debug.Log($"Result of {result.Name}: {result.ResultState.Status}");
        }
    }
}

Note: The TestRunCallback does not need any references to the UnityEditor namespace and is thus able to run in standalone Players, on the Player side. Note also that TestRunCallback is part of the runtime code and should not be confused with iCallbacks which are part of the TestRunnerApi. If you want to receive callbacks for tests run programmatically through the TestRunnerApi, see iCallbacks.