CompositeWorkItem.cs 13 KB

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