Search Unity

Question Triggering unit tests using C# API

Discussion in 'Testing & Automation' started by elmar1028, May 2, 2020.

  1. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Hi,

    Does anyone know if it's possible to trigger unit tests through C# in editor?

    I know that it's possible to run unit tests in batch mode but I need to run them in editor.

    Thanks,

    Elmar
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    This is possible using the Unity Test Framework v1.1.0 or higher (installed via Package manager).

    To run tests, you use the TestRunnerApi class. Here's an example on how to run tests:
    Code (CSharp):
    1. var testRunnerApi = ScriptableObject.CreateInstance<TestRunnerApi>();
    2. var filter = new Filter()
    3. {
    4.     testMode = TestMode.PlayMode
    5. };
    6. testRunnerApi.Execute(new ExecutionSettings(filter));
    See this manual page for more information: https://docs.unity3d.com/Packages/com.unity.test-framework@1.1/manual/extension-run-tests.html
     
    elmar1028 likes this.
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    This is great. But I was wondering if it's possible to run unit tests in earlier Unity versions too.
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    AFAIK, it's not.