CompositeWorkItem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using NUnit.Framework;
  7. using NUnit.Framework.Interfaces;
  8. using NUnit.Framework.Internal;
  9. using NUnit.Framework.Internal.Commands;
  10. using NUnit.Framework.Internal.Execution;
  11. using UnityEngine.TestTools.Logging;
  12. using UnityEngine.TestTools.TestRunner;
  13. namespace UnityEngine.TestRunner.NUnitExtensions.Runner
  14. {
  15. internal class CompositeWorkItem : UnityWorkItem
  16. {
  17. private readonly TestSuite _suite;
  18. private readonly TestSuiteResult _suiteResult;
  19. private readonly ITestFilter _childFilter;
  20. private TestCommand _setupCommand;
  21. private TestCommand _teardownCommand;
  22. public List<UnityWorkItem> Children { get; private set; }
  23. private int _countOrder;
  24. private CountdownEvent _childTestCountdown;
  25. public CompositeWorkItem(TestSuite suite, ITestFilter childFilter, WorkItemFactory factory)
  26. : base(suite, factory)
  27. {
  28. _suite = suite;
  29. _suiteResult = Result as TestSuiteResult;
  30. _childFilter = childFilter;
  31. _countOrder = 0;
  32. }
  33. protected override IEnumerable PerformWork()
  34. {
  35. InitializeSetUpAndTearDownCommands();
  36. if (UnityTestExecutionContext.CurrentContext != null && m_DontRunRestoringResult && EditModeTestCallbacks.RestoringTestContext != null)
  37. {
  38. EditModeTestCallbacks.RestoringTestContext();
  39. }
  40. if (!CheckForCancellation())
  41. if (Test.RunState == RunState.Explicit && !_childFilter.IsExplicitMatch(Test))
  42. SkipFixture(ResultState.Explicit, GetSkipReason(), null);
  43. else
  44. switch (Test.RunState)
  45. {
  46. default:
  47. case RunState.Runnable:
  48. case RunState.Explicit:
  49. Result.SetResult(ResultState.Success);
  50. CreateChildWorkItems();
  51. if (Children.Count > 0)
  52. {
  53. if (!m_DontRunRestoringResult)
  54. {
  55. //This is needed to give the editor a chance to go out of playmode if needed before creating objects.
  56. //If we do not, the objects could be automatically destroyed when exiting playmode and could result in errors later on
  57. yield return null;
  58. PerformOneTimeSetUp();
  59. }
  60. if (!CheckForCancellation())
  61. {
  62. switch (Result.ResultState.Status)
  63. {
  64. case TestStatus.Passed:
  65. foreach (var child in RunChildren())
  66. {
  67. if (CheckForCancellation())
  68. {
  69. yield break;
  70. }
  71. yield return child;
  72. }
  73. break;
  74. case TestStatus.Skipped:
  75. case TestStatus.Inconclusive:
  76. case TestStatus.Failed:
  77. SkipChildren(_suite, Result.ResultState.WithSite(FailureSite.Parent), "OneTimeSetUp: " + Result.Message);
  78. break;
  79. }
  80. }
  81. if (Context.ExecutionStatus != TestExecutionStatus.AbortRequested && !m_DontRunRestoringResult)
  82. {
  83. PerformOneTimeTearDown();
  84. }
  85. }
  86. break;
  87. case RunState.Skipped:
  88. SkipFixture(ResultState.Skipped, GetSkipReason(), null);
  89. break;
  90. case RunState.Ignored:
  91. SkipFixture(ResultState.Ignored, GetSkipReason(), null);
  92. break;
  93. case RunState.NotRunnable:
  94. SkipFixture(ResultState.NotRunnable, GetSkipReason(), GetProviderStackTrace());
  95. break;
  96. }
  97. if (!ResultedInDomainReload)
  98. {
  99. WorkItemComplete();
  100. }
  101. }
  102. private bool CheckForCancellation()
  103. {
  104. if (Context.ExecutionStatus != TestExecutionStatus.Running)
  105. {
  106. Result.SetResult(ResultState.Cancelled, "Test cancelled by user");
  107. return true;
  108. }
  109. return false;
  110. }
  111. private void InitializeSetUpAndTearDownCommands()
  112. {
  113. List<SetUpTearDownItem> setUpTearDownItems = _suite.TypeInfo != null
  114. ? CommandBuilder.BuildSetUpTearDownList(_suite.TypeInfo.Type, typeof(OneTimeSetUpAttribute), typeof(OneTimeTearDownAttribute))
  115. : new List<SetUpTearDownItem>();
  116. var actionItems = new List<TestActionItem>();
  117. foreach (ITestAction action in Actions)
  118. {
  119. bool applyToSuite = (action.Targets & ActionTargets.Suite) == ActionTargets.Suite
  120. || action.Targets == ActionTargets.Default && !(Test is ParameterizedMethodSuite);
  121. bool applyToTest = (action.Targets & ActionTargets.Test) == ActionTargets.Test
  122. && !(Test is ParameterizedMethodSuite);
  123. if (applyToSuite)
  124. actionItems.Add(new TestActionItem(action));
  125. if (applyToTest)
  126. Context.UpstreamActions.Add(action);
  127. }
  128. _setupCommand = CommandBuilder.MakeOneTimeSetUpCommand(_suite, setUpTearDownItems, actionItems);
  129. _teardownCommand = CommandBuilder.MakeOneTimeTearDownCommand(_suite, setUpTearDownItems, actionItems);
  130. }
  131. private void PerformOneTimeSetUp()
  132. {
  133. var logScope = new LogScope();
  134. try
  135. {
  136. _setupCommand.Execute(Context);
  137. }
  138. catch (Exception ex)
  139. {
  140. if (ex is NUnitException || ex is TargetInvocationException)
  141. ex = ex.InnerException;
  142. Result.RecordException(ex, FailureSite.SetUp);
  143. }
  144. if (logScope.AnyFailingLogs())
  145. {
  146. Result.RecordException(new UnhandledLogMessageException(logScope.FailingLogs.First()));
  147. }
  148. logScope.Dispose();
  149. }
  150. private IEnumerable RunChildren()
  151. {
  152. int childCount = Children.Count;
  153. if (childCount == 0)
  154. throw new InvalidOperationException("RunChildren called but item has no children");
  155. _childTestCountdown = new CountdownEvent(childCount);
  156. foreach (UnityWorkItem child in Children)
  157. {
  158. if (CheckForCancellation())
  159. {
  160. yield break;
  161. }
  162. var unityTestExecutionContext = new UnityTestExecutionContext(Context);
  163. child.InitializeContext(unityTestExecutionContext);
  164. var enumerable = child.Execute().GetEnumerator();
  165. while (true)
  166. {
  167. if (!enumerable.MoveNext())
  168. {
  169. break;
  170. }
  171. ResultedInDomainReload |= child.ResultedInDomainReload;
  172. yield return enumerable.Current;
  173. }
  174. _suiteResult.AddResult(child.Result);
  175. childCount--;
  176. }
  177. if (childCount > 0)
  178. {
  179. while (childCount-- > 0)
  180. CountDownChildTest();
  181. }
  182. }
  183. private void CreateChildWorkItems()
  184. {
  185. Children = new List<UnityWorkItem>();
  186. var testSuite = _suite;
  187. foreach (ITest test in testSuite.Tests)
  188. {
  189. if (_childFilter.Pass(test))
  190. {
  191. var child = m_Factory.Create(test, _childFilter);
  192. if (test.Properties.ContainsKey(PropertyNames.Order))
  193. {
  194. Children.Insert(0, child);
  195. _countOrder++;
  196. }
  197. else
  198. {
  199. Children.Add(child);
  200. }
  201. }
  202. }
  203. if (_countOrder != 0) SortChildren();
  204. }
  205. private class UnityWorkItemOrderComparer : IComparer<UnityWorkItem>
  206. {
  207. public int Compare(UnityWorkItem x, UnityWorkItem y)
  208. {
  209. var xKey = int.MaxValue;
  210. var yKey = int.MaxValue;
  211. if (x.Test.Properties.ContainsKey(PropertyNames.Order))
  212. xKey = (int)x.Test.Properties[PropertyNames.Order][0];
  213. if (y.Test.Properties.ContainsKey(PropertyNames.Order))
  214. yKey = (int)y.Test.Properties[PropertyNames.Order][0];
  215. return xKey.CompareTo(yKey);
  216. }
  217. }
  218. private void SortChildren()
  219. {
  220. Children.Sort(0, _countOrder, new UnityWorkItemOrderComparer());
  221. }
  222. private void SkipFixture(ResultState resultState, string message, string stackTrace)
  223. {
  224. Result.SetResult(resultState.WithSite(FailureSite.SetUp), message, StackFilter.Filter(stackTrace));
  225. SkipChildren(_suite, resultState.WithSite(FailureSite.Parent), "OneTimeSetUp: " + message);
  226. }
  227. private void SkipChildren(TestSuite suite, ResultState resultState, string message)
  228. {
  229. foreach (Test child in suite.Tests)
  230. {
  231. if (_childFilter.Pass(child))
  232. {
  233. Context.Listener.TestStarted(child);
  234. TestResult childResult = child.MakeTestResult();
  235. childResult.SetResult(resultState, message);
  236. _suiteResult.AddResult(childResult);
  237. if (child.IsSuite)
  238. SkipChildren((TestSuite)child, resultState, message);
  239. Context.Listener.TestFinished(childResult);
  240. }
  241. }
  242. }
  243. private void PerformOneTimeTearDown()
  244. {
  245. _teardownCommand.Execute(Context);
  246. }
  247. private string GetSkipReason()
  248. {
  249. return (string)Test.Properties.Get(PropertyNames.SkipReason);
  250. }
  251. private string GetProviderStackTrace()
  252. {
  253. return (string)Test.Properties.Get(PropertyNames.ProviderStackTrace);
  254. }
  255. private void CountDownChildTest()
  256. {
  257. _childTestCountdown.Signal();
  258. if (_childTestCountdown.CurrentCount == 0)
  259. {
  260. if (Context.ExecutionStatus != TestExecutionStatus.AbortRequested)
  261. PerformOneTimeTearDown();
  262. foreach (var childResult in _suiteResult.Children)
  263. if (childResult.ResultState == ResultState.Cancelled)
  264. {
  265. this.Result.SetResult(ResultState.Cancelled, "Cancelled by user");
  266. break;
  267. }
  268. WorkItemComplete();
  269. }
  270. }
  271. public override void Cancel(bool force)
  272. {
  273. if (Children == null)
  274. return;
  275. foreach (var child in Children)
  276. {
  277. var ctx = child.Context;
  278. if (ctx != null)
  279. ctx.ExecutionStatus = force ? TestExecutionStatus.AbortRequested : TestExecutionStatus.StopRequested;
  280. if (child.State == WorkItemState.Running)
  281. child.Cancel(force);
  282. }
  283. }
  284. }
  285. }