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.

CompositeWorkItem.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. logScope.EvaluateLogScope(true);
  139. }
  140. catch (Exception ex)
  141. {
  142. if (ex is NUnitException || ex is TargetInvocationException)
  143. ex = ex.InnerException;
  144. Result.RecordException(ex, FailureSite.SetUp);
  145. }
  146. logScope.Dispose();
  147. }
  148. private IEnumerable RunChildren()
  149. {
  150. int childCount = Children.Count;
  151. if (childCount == 0)
  152. throw new InvalidOperationException("RunChildren called but item has no children");
  153. _childTestCountdown = new CountdownEvent(childCount);
  154. foreach (UnityWorkItem child in Children)
  155. {
  156. if (CheckForCancellation())
  157. {
  158. yield break;
  159. }
  160. var unityTestExecutionContext = new UnityTestExecutionContext(Context);
  161. child.InitializeContext(unityTestExecutionContext);
  162. var enumerable = child.Execute().GetEnumerator();
  163. while (true)
  164. {
  165. if (!enumerable.MoveNext())
  166. {
  167. break;
  168. }
  169. ResultedInDomainReload |= child.ResultedInDomainReload;
  170. yield return enumerable.Current;
  171. }
  172. _suiteResult.AddResult(child.Result);
  173. childCount--;
  174. }
  175. if (childCount > 0)
  176. {
  177. while (childCount-- > 0)
  178. CountDownChildTest();
  179. }
  180. }
  181. private void CreateChildWorkItems()
  182. {
  183. Children = new List<UnityWorkItem>();
  184. var testSuite = _suite;
  185. foreach (ITest test in testSuite.Tests)
  186. {
  187. if (_childFilter.Pass(test))
  188. {
  189. var child = m_Factory.Create(test, _childFilter);
  190. if (test.Properties.ContainsKey(PropertyNames.Order))
  191. {
  192. Children.Insert(0, child);
  193. _countOrder++;
  194. }
  195. else
  196. {
  197. Children.Add(child);
  198. }
  199. }
  200. }
  201. if (_countOrder != 0) SortChildren();
  202. }
  203. private class UnityWorkItemOrderComparer : IComparer<UnityWorkItem>
  204. {
  205. public int Compare(UnityWorkItem x, UnityWorkItem y)
  206. {
  207. var xKey = int.MaxValue;
  208. var yKey = int.MaxValue;
  209. if (x.Test.Properties.ContainsKey(PropertyNames.Order))
  210. xKey = (int)x.Test.Properties[PropertyNames.Order][0];
  211. if (y.Test.Properties.ContainsKey(PropertyNames.Order))
  212. yKey = (int)y.Test.Properties[PropertyNames.Order][0];
  213. return xKey.CompareTo(yKey);
  214. }
  215. }
  216. private void SortChildren()
  217. {
  218. Children.Sort(0, _countOrder, new UnityWorkItemOrderComparer());
  219. }
  220. private void SkipFixture(ResultState resultState, string message, string stackTrace)
  221. {
  222. Result.SetResult(resultState.WithSite(FailureSite.SetUp), message, StackFilter.Filter(stackTrace));
  223. SkipChildren(_suite, resultState.WithSite(FailureSite.Parent), "OneTimeSetUp: " + message);
  224. }
  225. private void SkipChildren(TestSuite suite, ResultState resultState, string message)
  226. {
  227. foreach (Test child in suite.Tests)
  228. {
  229. if (_childFilter.Pass(child))
  230. {
  231. Context.Listener.TestStarted(child);
  232. TestResult childResult = child.MakeTestResult();
  233. childResult.SetResult(resultState, message);
  234. _suiteResult.AddResult(childResult);
  235. if (child.IsSuite)
  236. SkipChildren((TestSuite)child, resultState, message);
  237. Context.Listener.TestFinished(childResult);
  238. }
  239. }
  240. }
  241. private void PerformOneTimeTearDown()
  242. {
  243. var logScope = new LogScope();
  244. try
  245. {
  246. _teardownCommand.Execute(Context);
  247. logScope.EvaluateLogScope(true);
  248. }
  249. catch (Exception ex)
  250. {
  251. if (ex is NUnitException || ex is TargetInvocationException)
  252. ex = ex.InnerException;
  253. Result.RecordException(ex, FailureSite.SetUp);
  254. }
  255. logScope.Dispose();
  256. }
  257. private string GetSkipReason()
  258. {
  259. return (string)Test.Properties.Get(PropertyNames.SkipReason);
  260. }
  261. private string GetProviderStackTrace()
  262. {
  263. return (string)Test.Properties.Get(PropertyNames.ProviderStackTrace);
  264. }
  265. private void CountDownChildTest()
  266. {
  267. _childTestCountdown.Signal();
  268. if (_childTestCountdown.CurrentCount == 0)
  269. {
  270. if (Context.ExecutionStatus != TestExecutionStatus.AbortRequested)
  271. PerformOneTimeTearDown();
  272. foreach (var childResult in _suiteResult.Children)
  273. if (childResult.ResultState == ResultState.Cancelled)
  274. {
  275. this.Result.SetResult(ResultState.Cancelled, "Cancelled by user");
  276. break;
  277. }
  278. WorkItemComplete();
  279. }
  280. }
  281. public override void Cancel(bool force)
  282. {
  283. if (Children == null)
  284. return;
  285. foreach (var child in Children)
  286. {
  287. var ctx = child.Context;
  288. if (ctx != null)
  289. ctx.ExecutionStatus = force ? TestExecutionStatus.AbortRequested : TestExecutionStatus.StopRequested;
  290. if (child.State == WorkItemState.Running)
  291. child.Cancel(force);
  292. }
  293. }
  294. }
  295. }