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.

EditModeRunner.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using NUnit.Framework.Interfaces;
  6. using NUnit.Framework.Internal;
  7. using UnityEngine;
  8. using UnityEngine.TestRunner.NUnitExtensions;
  9. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  10. using UnityEngine.TestTools;
  11. using UnityEngine.TestTools.NUnitExtensions;
  12. using UnityEngine.TestTools.TestRunner;
  13. namespace UnityEditor.TestTools.TestRunner
  14. {
  15. internal interface IUnityTestAssemblyRunnerFactory
  16. {
  17. IUnityTestAssemblyRunner Create(TestPlatform testPlatform, string[] orderedTestNames, int randomOrderSeed, WorkItemFactory factory, UnityTestExecutionContext context);
  18. }
  19. internal class UnityTestAssemblyRunnerFactory : IUnityTestAssemblyRunnerFactory
  20. {
  21. public IUnityTestAssemblyRunner Create(TestPlatform testPlatform, string[] orderedTestNames, int randomOrderSeed,
  22. WorkItemFactory factory, UnityTestExecutionContext context)
  23. {
  24. return new UnityTestAssemblyRunner(new UnityTestAssemblyBuilder(orderedTestNames, randomOrderSeed), factory, context);
  25. }
  26. }
  27. [Serializable]
  28. internal class EditModeRunner : ScriptableObject, IDisposable
  29. {
  30. //The counter from the IEnumerator object
  31. [SerializeField]
  32. private int m_CurrentPC;
  33. [SerializeField]
  34. private bool m_ExecuteOnEnable;
  35. [SerializeField]
  36. private List<string> m_AlreadyStartedTests;
  37. [SerializeField]
  38. private List<TestResultSerializer> m_ExecutedTests;
  39. private TestStartedEvent m_TestStartedEvent;
  40. private TestFinishedEvent m_TestFinishedEvent;
  41. [SerializeField]
  42. private object m_CurrentYieldObject;
  43. [SerializeField]
  44. private string[] m_OrderedTestNames;
  45. [SerializeField]
  46. public bool RunFinished;
  47. [SerializeField]
  48. private bool m_DisableNestedEnumeratorBugfix;
  49. public bool RunningSynchronously { get; private set; }
  50. internal IUnityTestAssemblyRunner m_Runner;
  51. private IEnumerator m_RunStep;
  52. public IUnityTestAssemblyRunnerFactory UnityTestAssemblyRunnerFactory { get; set; }
  53. public void Init(ITestFilter filter, bool runningSynchronously, ITest testTree, TestStartedEvent testStartedEvent, TestFinishedEvent testFinishedEvent, UnityTestExecutionContext context,
  54. string[] orderedTestNames, int randomOrderSeed, bool disableNestedEnumeratorBugfix)
  55. {
  56. TestEnumerator.Reset();
  57. m_AlreadyStartedTests = new List<string>();
  58. m_ExecutedTests = new List<TestResultSerializer>();
  59. m_OrderedTestNames = orderedTestNames;
  60. m_randomOrderSeed = randomOrderSeed;
  61. RunningSynchronously = runningSynchronously;
  62. m_DisableNestedEnumeratorBugfix = disableNestedEnumeratorBugfix;
  63. Run(testTree, filter, context, testStartedEvent, testFinishedEvent);
  64. }
  65. public void Resume(ITestFilter filter, ITest testTree, TestStartedEvent testStartedEvent, TestFinishedEvent testFinishedEvent, UnityTestExecutionContext context)
  66. {
  67. if (m_ExecuteOnEnable)
  68. {
  69. m_ExecuteOnEnable = false;
  70. if (m_CurrentPC >= 0)
  71. {
  72. EnumeratorStepHelper.SetEnumeratorPC(m_CurrentPC);
  73. }
  74. UnityWorkItemDataHolder.alreadyExecutedTests = m_ExecutedTests.Select(x => x.uniqueName).ToList();
  75. UnityWorkItemDataHolder.alreadyStartedTests = m_AlreadyStartedTests;
  76. Run(testTree, filter, context, testStartedEvent, testFinishedEvent);
  77. }
  78. }
  79. public void TestStartedEvent(ITest test)
  80. {
  81. m_AlreadyStartedTests.Add(test.GetUniqueName());
  82. }
  83. public void TestFinishedEvent(ITestResult testResult)
  84. {
  85. m_AlreadyStartedTests.Remove(testResult.Test.GetUniqueName());
  86. m_ExecutedTests.Add(TestResultSerializer.MakeFromTestResult(testResult));
  87. }
  88. private void Run(ITest testTree, ITestFilter filter, UnityTestExecutionContext context, TestStartedEvent testStartedEvent, TestFinishedEvent testFinishedEvent)
  89. {
  90. m_TestStartedEvent = testStartedEvent;
  91. m_TestFinishedEvent = testFinishedEvent;
  92. m_Runner = (UnityTestAssemblyRunnerFactory ?? new UnityTestAssemblyRunnerFactory()).Create(TestPlatform.EditMode, m_OrderedTestNames, m_randomOrderSeed, new EditmodeWorkItemFactory(), context);
  93. m_Runner.LoadTestTree(testTree);
  94. hideFlags |= HideFlags.DontSave;
  95. EnumerableSetUpTearDownCommand.ActivePcHelper = new EditModePcHelper();
  96. OuterUnityTestActionCommand.ActivePcHelper = new EditModePcHelper();
  97. EditModeTestCallbacks.RestoringTestContext += OnRestoringTest;
  98. m_TestStartedEvent.AddListener(TestStartedEvent);
  99. m_TestFinishedEvent.AddListener(TestFinishedEvent);
  100. AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload;
  101. RunningTests = true;
  102. EditorApplication.LockReloadAssemblies();
  103. var testListenerWrapper = new TestListenerWrapper(m_TestStartedEvent, m_TestFinishedEvent);
  104. m_RunStep = m_Runner.Run(testListenerWrapper, filter).GetEnumerator();
  105. }
  106. private void OnBeforeAssemblyReload()
  107. {
  108. if (m_ExecuteOnEnable)
  109. {
  110. AssemblyReloadEvents.beforeAssemblyReload -= OnBeforeAssemblyReload;
  111. return;
  112. }
  113. if (m_Runner != null && m_Runner.TopLevelWorkItem != null)
  114. m_Runner.TopLevelWorkItem.ResultedInDomainReload = true;
  115. if (RunningTests)
  116. {
  117. Debug.LogError("TestRunner: Unexpected assembly reload happened while running tests");
  118. EditorUtility.ClearProgressBar();
  119. if (m_Runner.GetCurrentContext() != null && m_Runner.GetCurrentContext().CurrentResult != null)
  120. {
  121. m_Runner.GetCurrentContext().CurrentResult.SetResult(ResultState.Cancelled, "Unexpected assembly reload happened");
  122. }
  123. OnRunCancel();
  124. }
  125. }
  126. private bool RunningTests;
  127. private Stack<IEnumerator> StepStack = new Stack<IEnumerator>();
  128. private int m_randomOrderSeed;
  129. private bool MoveNextAndUpdateYieldObject()
  130. {
  131. var result = m_RunStep.MoveNext();
  132. if (result)
  133. {
  134. m_CurrentYieldObject = m_RunStep.Current;
  135. while (m_CurrentYieldObject is IEnumerator) // going deeper
  136. {
  137. var currentEnumerator = (IEnumerator)m_CurrentYieldObject;
  138. // go deeper and add parent to stack
  139. StepStack.Push(m_RunStep);
  140. m_RunStep = currentEnumerator;
  141. m_CurrentYieldObject = m_RunStep.Current;
  142. if (!m_DisableNestedEnumeratorBugfix)
  143. {
  144. return MoveNextAndUpdateYieldObject();
  145. }
  146. }
  147. if (StepStack.Count > 0 && m_CurrentYieldObject != null) // not null and not IEnumerator, nested
  148. {
  149. Debug.LogError("EditMode test can only yield null, but not <" + m_CurrentYieldObject.GetType().Name + ">");
  150. }
  151. return true;
  152. }
  153. if (StepStack.Count == 0) // done
  154. return false;
  155. m_RunStep = StepStack.Pop(); // going up
  156. return MoveNextAndUpdateYieldObject();
  157. }
  158. public void TestConsumer(TestRunnerStateSerializer testRunnerStateSerializer)
  159. {
  160. var moveNext = MoveNextAndUpdateYieldObject();
  161. if (m_CurrentYieldObject != null)
  162. {
  163. InvokeDelegator(testRunnerStateSerializer);
  164. }
  165. if (!moveNext && !m_Runner.IsTestComplete)
  166. {
  167. CompleteTestRun();
  168. throw new IndexOutOfRangeException("There are no more elements to process and IsTestComplete is false");
  169. }
  170. if (m_Runner.IsTestComplete)
  171. {
  172. CompleteTestRun();
  173. }
  174. }
  175. private void CompleteTestRun()
  176. {
  177. RunFinished = true;
  178. UnityWorkItemDataHolder.alreadyExecutedTests = null;
  179. }
  180. private void OnRestoringTest()
  181. {
  182. var item = m_ExecutedTests.Find(t => t.fullName == UnityTestExecutionContext.CurrentContext.CurrentTest.FullName);
  183. if (item != null)
  184. {
  185. item.RestoreTestResult(UnityTestExecutionContext.CurrentContext.CurrentResult);
  186. }
  187. }
  188. private static bool IsCancelled()
  189. {
  190. return UnityTestExecutionContext.CurrentContext.ExecutionStatus == TestExecutionStatus.AbortRequested || UnityTestExecutionContext.CurrentContext.ExecutionStatus == TestExecutionStatus.StopRequested;
  191. }
  192. private void InvokeDelegator(TestRunnerStateSerializer testRunnerStateSerializer)
  193. {
  194. if (m_CurrentYieldObject == null)
  195. {
  196. return;
  197. }
  198. if (IsCancelled())
  199. {
  200. return;
  201. }
  202. if (m_CurrentYieldObject is RestoreTestContextAfterDomainReload)
  203. {
  204. if (testRunnerStateSerializer.ShouldRestore())
  205. {
  206. testRunnerStateSerializer.RestoreContext();
  207. }
  208. return;
  209. }
  210. try
  211. {
  212. if (m_CurrentYieldObject is IEditModeTestYieldInstruction)
  213. {
  214. var editModeTestYieldInstruction = (IEditModeTestYieldInstruction)m_CurrentYieldObject;
  215. if (editModeTestYieldInstruction.ExpectDomainReload)
  216. {
  217. PrepareForDomainReload(testRunnerStateSerializer);
  218. }
  219. return;
  220. }
  221. }
  222. catch (Exception e)
  223. {
  224. UnityTestExecutionContext.CurrentContext.CurrentResult.RecordException(e);
  225. return;
  226. }
  227. UnityTestExecutionContext.CurrentContext.CurrentResult.RecordException(new InvalidOperationException("EditMode test can only yield null"));
  228. }
  229. private void CompilationFailureWatch()
  230. {
  231. if (EditorApplication.isCompiling)
  232. return;
  233. EditorApplication.update -= CompilationFailureWatch;
  234. if (EditorUtility.scriptCompilationFailed)
  235. {
  236. EditorUtility.ClearProgressBar();
  237. OnRunCancel();
  238. }
  239. }
  240. private void PrepareForDomainReload(TestRunnerStateSerializer testRunnerStateSerializer)
  241. {
  242. testRunnerStateSerializer.SaveContext();
  243. m_CurrentPC = EnumeratorStepHelper.GetEnumeratorPC(TestEnumerator.Enumerator);
  244. m_ExecuteOnEnable = true;
  245. RunningTests = false;
  246. }
  247. public void Dispose()
  248. {
  249. Reflect.MethodCallWrapper = null;
  250. DestroyImmediate(this);
  251. RunningTests = false;
  252. EditorApplication.UnlockReloadAssemblies();
  253. }
  254. public void OnRunCancel()
  255. {
  256. UnityWorkItemDataHolder.alreadyExecutedTests = null;
  257. m_ExecuteOnEnable = false;
  258. m_Runner.StopRun();
  259. RunFinished = true;
  260. }
  261. }
  262. }