UnityWorkItem.cs 3.7 KB

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