No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestRunnerApi.cs 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using UnityEditor.TestTools.TestRunner.CommandLineTest;
  5. using UnityEditor.TestTools.TestRunner.TestRun;
  6. using UnityEngine;
  7. using UnityEngine.TestRunner.TestLaunchers;
  8. using UnityEngine.TestTools;
  9. using UnityEngine.TestTools.NUnitExtensions;
  10. namespace UnityEditor.TestTools.TestRunner.Api
  11. {
  12. /// <summary>
  13. /// The TestRunnerApi retrieves and runs tests programmatically from code inside the project, or inside other packages. TestRunnerApi is a [ScriptableObject](https://docs.unity3d.com/ScriptReference/ScriptableObject.html).
  14. ///You can initialize the API like this:
  15. /// <code>
  16. /// var testRunnerApi = ScriptableObject.CreateInstance&lt;TestRunnerApi&gt;();
  17. /// </code>
  18. /// Note: You can subscribe and receive test results in one instance of the API, even if the run starts from another instance.
  19. /// The TestRunnerApi supports the following workflows:
  20. /// - [How to run tests programmatically](https://docs.unity3d.com/Packages/com.unity.test-framework@1.1/manual/extension-run-tests.html)
  21. /// - [How to get test results](https://docs.unity3d.com/Packages/com.unity.test-framework@1.1/manual/extension-get-test-results.html)
  22. /// - [How to retrieve the list of tests](https://docs.unity3d.com/Packages/com.unity.test-framework@1.1/manual/extension-retrieve-test-list.html)
  23. /// </summary>
  24. public class TestRunnerApi : ScriptableObject, ITestRunnerApi
  25. {
  26. internal ICallbacksHolder callbacksHolder;
  27. private ICallbacksHolder m_CallbacksHolder
  28. {
  29. get
  30. {
  31. if (callbacksHolder == null)
  32. {
  33. return CallbacksHolder.instance;
  34. }
  35. return callbacksHolder;
  36. }
  37. }
  38. internal Func<ExecutionSettings,string> ScheduleJob = (executionSettings) =>
  39. {
  40. var runner = new TestJobRunner();
  41. return runner.RunJob(new TestJobData(executionSettings));
  42. };
  43. /// <summary>
  44. /// Starts a test run with a given set of executionSettings.
  45. /// </summary>
  46. /// <param name="executionSettings">Set of <see cref="ExecutionSettings"/></param>
  47. /// <returns>A GUID that identifies the TestJobData.</returns>
  48. public string Execute(ExecutionSettings executionSettings)
  49. {
  50. if (executionSettings == null)
  51. {
  52. throw new ArgumentNullException(nameof(executionSettings));
  53. }
  54. if ((executionSettings.filters == null || executionSettings.filters.Length == 0) && executionSettings.filter != null)
  55. {
  56. // Map filter (singular) to filters (plural), for backwards compatibility.
  57. executionSettings.filters = new [] {executionSettings.filter};
  58. }
  59. if (executionSettings.targetPlatform == null && executionSettings.filters != null &&
  60. executionSettings.filters.Length > 0)
  61. {
  62. executionSettings.targetPlatform = executionSettings.filters[0].targetPlatform;
  63. }
  64. return ScheduleJob(executionSettings);
  65. }
  66. /// <summary>
  67. /// Sets up a given instance of <see cref="ICallbacks"/> to be invoked on test runs.
  68. /// </summary>
  69. /// <typeparam name="T">
  70. /// Generic representing a type of callback.
  71. /// </typeparam>
  72. /// <param name="testCallbacks">
  73. /// The test callbacks to be invoked.
  74. /// </param>
  75. /// <param name="priority">
  76. /// Sets the order in which the callbacks are invoked, starting with the highest value first.
  77. /// </param>
  78. public void RegisterCallbacks<T>(T testCallbacks, int priority = 0) where T : ICallbacks
  79. {
  80. if (testCallbacks == null)
  81. {
  82. throw new ArgumentNullException(nameof(testCallbacks));
  83. }
  84. m_CallbacksHolder.Add(testCallbacks, priority);
  85. }
  86. /// <summary>
  87. /// Unregister an instance of <see cref="ICallbacks"/> to no longer receive callbacks from test runs.
  88. /// </summary>
  89. /// <typeparam name="T">
  90. /// Generic representing a type of callback.
  91. /// </typeparam>
  92. /// <param name="testCallbacks">The test callbacks to unregister.</param>
  93. public void UnregisterCallbacks<T>(T testCallbacks) where T : ICallbacks
  94. {
  95. if (testCallbacks == null)
  96. {
  97. throw new ArgumentNullException(nameof(testCallbacks));
  98. }
  99. m_CallbacksHolder.Remove(testCallbacks);
  100. }
  101. internal void RetrieveTestList(ExecutionSettings executionSettings, Action<ITestAdaptor> callback)
  102. {
  103. if (executionSettings == null)
  104. {
  105. throw new ArgumentNullException(nameof(executionSettings));
  106. }
  107. var firstFilter = executionSettings.filters?.FirstOrDefault() ?? executionSettings.filter;
  108. RetrieveTestList(firstFilter.testMode, callback);
  109. }
  110. /// <summary>
  111. /// Retrieve the full test tree as ITestAdaptor for a given test mode. This is obsolete. Use TestRunnerApi.RetrieveTestTree instead.
  112. /// </summary>
  113. /// <param name="testMode"></param>
  114. /// <param name="callback"></param>
  115. public void RetrieveTestList(TestMode testMode, Action<ITestAdaptor> callback)
  116. {
  117. if (callback == null)
  118. {
  119. throw new ArgumentNullException(nameof(callback));
  120. }
  121. var platform = ParseTestMode(testMode);
  122. var testAssemblyProvider = new EditorLoadedTestAssemblyProvider(new EditorCompilationInterfaceProxy(), new EditorAssembliesProxy());
  123. var testAdaptorFactory = new TestAdaptorFactory();
  124. var testListCache = new TestListCache(testAdaptorFactory, new RemoteTestResultDataFactory(), TestListCacheData.instance);
  125. var testListProvider = new TestListProvider(testAssemblyProvider, new UnityTestAssemblyBuilder());
  126. var cachedTestListProvider = new CachingTestListProvider(testListProvider, testListCache, testAdaptorFactory);
  127. var job = new TestListJob(cachedTestListProvider, platform, (testRoot) =>
  128. {
  129. callback(testRoot);
  130. });
  131. job.Start();
  132. }
  133. internal static bool IsRunActive()
  134. {
  135. return RunData.instance.isRunning;
  136. }
  137. private static TestPlatform ParseTestMode(TestMode testMode)
  138. {
  139. return (((testMode & TestMode.EditMode) == TestMode.EditMode) ? TestPlatform.EditMode : 0) | (((testMode & TestMode.PlayMode) == TestMode.PlayMode) ? TestPlatform.PlayMode : 0);
  140. }
  141. }
  142. }