Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Run tests from editor script

Discussion in 'Testing & Automation' started by mhgamework, May 13, 2019.

  1. mhgamework

    mhgamework

    Joined:
    Dec 13, 2014
    Posts:
    14
    Is there a way i can access the Unity Test Runner from editor scripts?

    I want to add a button to the unity editor that runs a predifined set of tests. Its behaviour should be the same as selecting the corresponding tests in the Test Runner window and pressing run.

    thanks,

    MH
     
  2. Warnecke

    Warnecke

    Unity Technologies

    Joined:
    Nov 28, 2017
    Posts:
    92
    Hey. It is currently not possible, but we will be releasing a new API that will make it possible to do that. We hope to have that ready in the summer.
     
  3. xanzandecki

    xanzandecki

    Joined:
    Dec 2, 2019
    Posts:
    1
    Is this API available already?
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Yes, if you are on Unity 2019.2 or higher, you can install the "Test framework" package that includes this functionality.

    Here's an example taken from my UnityTestExtensions library, that adds menu items that run your tests.
    You could also call these static methods from any editor script to achieve the same result:
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.TestTools.TestRunner.Api;
    3. using UnityEngine;
    4.  
    5. namespace UnityTestExtensions
    6. {
    7.     public static class TestMenuItems
    8.     {
    9.         /// <summary>
    10.         /// Runs all edit mode tests.
    11.         /// </summary>
    12.         [MenuItem("Window/General/Test Extensions/Run Edit Mode Tests")]
    13.         public static void RunEditModeTests()
    14.         {
    15.             RunTests(TestMode.EditMode);
    16.         }
    17.      
    18.         /// <summary>
    19.         /// Runs all play mode tests.
    20.         /// </summary>
    21.         [MenuItem("Window/General/Test Extensions/Run Play Mode Tests")]
    22.         public static void RunPlayModeTests()
    23.         {
    24.             RunTests(TestMode.PlayMode);
    25.         }
    26.  
    27.         private static void RunTests(TestMode testModeToRun)
    28.         {
    29.             var testRunnerApi = ScriptableObject.CreateInstance<TestRunnerApi>();
    30.             var filter = new Filter()
    31.             {
    32.                 testMode = testModeToRun
    33.             };
    34.          
    35.             testRunnerApi.Execute(new ExecutionSettings(filter));
    36.         }
    37.     }
    38. }
     
  5. mmedinaballesteros

    mmedinaballesteros

    Joined:
    Jan 6, 2021
    Posts:
    2
    Hey @Warnecke, did you finally released that new API?
     
  6. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,649