UnityWorkItem.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using NUnit.Framework;
  7. using NUnit.Framework.Interfaces;
  8. using NUnit.Framework.Internal;
  9. using NUnit.Framework.Internal.Execution;
  10. namespace UnityEngine.TestRunner.NUnitExtensions.Runner
  11. {
  12. internal abstract class UnityWorkItem
  13. {
  14. protected readonly WorkItemFactory m_Factory;
  15. protected bool m_ExecuteTestStartEvent;
  16. protected bool m_DontRunRestoringResult;
  17. protected const int k_DefaultTimeout = 1000 * 180;
  18. public event EventHandler Completed;
  19. public bool ResultedInDomainReload { get; internal set; }
  20. public UnityTestExecutionContext Context { get; private set; }
  21. public Test Test { get; private set; }
  22. public TestResult Result { get; protected set; }
  23. public WorkItemState State { get; private set; }
  24. public List<ITestAction> Actions { get; private set; }
  25. protected UnityWorkItem(Test test, WorkItemFactory factory)
  26. {
  27. m_Factory = factory;
  28. Test = test;
  29. Actions = new List<ITestAction>();
  30. Result = test.MakeTestResult();
  31. State = WorkItemState.Ready;
  32. m_ExecuteTestStartEvent = ShouldExecuteStartEvent();
  33. m_DontRunRestoringResult = ShouldRestore(test);
  34. }
  35. protected static bool ShouldRestore(ITest loadedTest)
  36. {
  37. return UnityWorkItemDataHolder.alreadyExecutedTests != null &&
  38. UnityWorkItemDataHolder.alreadyExecutedTests.Contains(loadedTest.GetUniqueName());
  39. }
  40. protected bool ShouldExecuteStartEvent()
  41. {
  42. return UnityWorkItemDataHolder.alreadyStartedTests != null &&
  43. UnityWorkItemDataHolder.alreadyStartedTests.All(x => x != Test.GetUniqueName()) &&
  44. !ShouldRestore(Test);
  45. }
  46. protected abstract IEnumerable PerformWork();
  47. public void InitializeContext(UnityTestExecutionContext context)
  48. {
  49. Context = context;
  50. if (Test is TestAssembly)
  51. Actions.AddRange(ActionsHelper.GetActionsFromTestAssembly((TestAssembly)Test));
  52. else if (Test is ParameterizedMethodSuite)
  53. Actions.AddRange(ActionsHelper.GetActionsFromTestMethodInfo(Test.Method));
  54. else if (Test.TypeInfo != null)
  55. Actions.AddRange(ActionsHelper.GetActionsFromTypesAttributes(Test.TypeInfo.Type));
  56. }
  57. public virtual IEnumerable Execute()
  58. {
  59. Context.CurrentTest = this.Test;
  60. Context.CurrentResult = this.Result;
  61. if (m_ExecuteTestStartEvent)
  62. {
  63. Context.Listener.TestStarted(Test);
  64. }
  65. Context.StartTime = DateTime.UtcNow;
  66. Context.StartTicks = Stopwatch.GetTimestamp();
  67. State = WorkItemState.Running;
  68. return PerformWork();
  69. }
  70. protected void WorkItemComplete()
  71. {
  72. State = WorkItemState.Complete;
  73. Result.StartTime = Context.StartTime;
  74. Result.EndTime = DateTime.UtcNow;
  75. long tickCount = Stopwatch.GetTimestamp() - Context.StartTicks;
  76. double seconds = (double)tickCount / Stopwatch.Frequency;
  77. Result.Duration = seconds;
  78. //Result.AssertCount += Context.AssertCount;
  79. Context.Listener.TestFinished(Result);
  80. if (Completed != null)
  81. Completed(this, EventArgs.Empty);
  82. Context.TestObject = null;
  83. Test.Fixture = null;
  84. }
  85. public virtual void Cancel(bool force)
  86. {
  87. Result.SetResult(ResultState.Cancelled, "Cancelled by user");
  88. Context.Listener.TestFinished(Result);
  89. }
  90. }
  91. }