暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CompositeWorkItem.cs 13KB

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