Specify which tests to run
Compose Filter objects for the TestRunnerAPI to determine which tests to include in a test run.
Read time 2 minutesLast updated 13 days ago
To run tests from code, call on the TestRunnerApi and specify which tests to run with a Filter.
ExecuteThe following example runs all Play mode tests in a project:
var testRunnerApi = ScriptableObject.CreateInstance<TestRunnerApi>();var filter = new Filter(){ testMode = TestMode.PlayMode};testRunnerApi.Execute(new ExecutionSettings(filter));
Multiple filter values
You can define additional fields on the class to create a more specific filter.
FilterMany of the fields allow for multiple values. When you define multiple values for a field, the test runner runs any tests that match at least one of the values.
The following example runs tests with full names that match either of the two names provided:
var api = ScriptableObject.CreateInstance<TestRunnerApi>();api.Execute(new ExecutionSettings(new Filter(){ testNames = new[] {"MyTestClass.NameOfMyTest", "SpecificTestFixture.NameOfAnotherTest"}}));
Multiple filter fields
If you define multiple fields on a filter, the runner only runs tests that match all the fields.
In this example, it runs any test in the specified assembly with a name that matches either of the two specified test names.
var api = ScriptableObject.CreateInstance<TestRunnerApi>();api.Execute(new ExecutionSettings(new Filter(){ assemblyNames = new [] {"MyTestAssembly"}, testNames = new [] {"MyTestClass.NameOfMyTest", "MyTestClass.AnotherNameOfATest"}}));
Multiple constructor filters
The execution settings take 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.
The following runs all tests in the assembly named any test with a name that matches either of the two specified test names:
MyTestAssemblyvar api = ScriptableObject.CreateInstance<TestRunnerApi>();api.Execute(new ExecutionSettings( new Filter() { assemblyNames = new[] {"MyTestAssembly"}, }, new Filter() { testNames = new[] {"MyTestClass.NameOfMyTest", "MyTestClass.AnotherNameOfATest"} }));