UnityTestExecutionContext.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using NUnit.Framework;
  6. using NUnit.Framework.Constraints;
  7. using NUnit.Framework.Interfaces;
  8. using NUnit.Framework.Internal;
  9. using NUnit.Framework.Internal.Execution;
  10. using UnityEngine.TestTools;
  11. namespace UnityEngine.TestRunner.NUnitExtensions.Runner
  12. {
  13. internal class UnityTestExecutionContext : ITestExecutionContext
  14. {
  15. private readonly UnityTestExecutionContext _priorContext;
  16. private TestResult _currentResult;
  17. private int _assertCount;
  18. public static UnityTestExecutionContext CurrentContext { get; set; }
  19. public UnityTestExecutionContext Context { get; private set; }
  20. public Test CurrentTest { get; set; }
  21. public DateTime StartTime { get; set; }
  22. public long StartTicks { get; set; }
  23. public TestResult CurrentResult
  24. {
  25. get { return _currentResult; }
  26. set
  27. {
  28. _currentResult = value;
  29. if (value != null)
  30. OutWriter = value.OutWriter;
  31. }
  32. }
  33. public object TestObject { get; set; }
  34. public string WorkDirectory { get; set; }
  35. private TestExecutionStatus _executionStatus;
  36. public TestExecutionStatus ExecutionStatus
  37. {
  38. get
  39. {
  40. // ExecutionStatus may have been set to StopRequested or AbortRequested
  41. // in a prior context. If so, reflect the same setting in this context.
  42. if (_executionStatus == TestExecutionStatus.Running && _priorContext != null)
  43. _executionStatus = _priorContext.ExecutionStatus;
  44. return _executionStatus;
  45. }
  46. set
  47. {
  48. _executionStatus = value;
  49. // Push the same setting up to all prior contexts
  50. if (_priorContext != null)
  51. _priorContext.ExecutionStatus = value;
  52. }
  53. }
  54. public List<ITestAction> UpstreamActions { get; private set; }
  55. public int TestCaseTimeout { get; set; }
  56. public CultureInfo CurrentCulture { get; set; }
  57. public CultureInfo CurrentUICulture { get; set; }
  58. public ITestListener Listener { get; set; }
  59. public UnityTestExecutionContext()
  60. {
  61. UpstreamActions = new List<ITestAction>();
  62. CurrentContext = this;
  63. }
  64. public UnityTestExecutionContext(UnityTestExecutionContext other)
  65. {
  66. _priorContext = other;
  67. CurrentTest = other.CurrentTest;
  68. CurrentResult = other.CurrentResult;
  69. TestObject = other.TestObject;
  70. WorkDirectory = other.WorkDirectory;
  71. Listener = other.Listener;
  72. TestCaseTimeout = other.TestCaseTimeout;
  73. UpstreamActions = new List<ITestAction>(other.UpstreamActions);
  74. SetUpTearDownState = other.SetUpTearDownState;
  75. OuterUnityTestActionState = other.OuterUnityTestActionState;
  76. TestContext.CurrentTestExecutionContext = this;
  77. CurrentCulture = other.CurrentCulture;
  78. CurrentUICulture = other.CurrentUICulture;
  79. CurrentContext = this;
  80. }
  81. public TextWriter OutWriter { get; private set; }
  82. public bool StopOnError { get; set; }
  83. public IWorkItemDispatcher Dispatcher { get; set; }
  84. public ParallelScope ParallelScope { get; set; }
  85. public string WorkerId { get; private set; }
  86. public Randomizer RandomGenerator { get; private set; }
  87. public ValueFormatter CurrentValueFormatter { get; private set; }
  88. public bool IsSingleThreaded { get; set; }
  89. public BeforeAfterTestCommandState SetUpTearDownState { get; set; }
  90. public BeforeAfterTestCommandState OuterUnityTestActionState { get; set; }
  91. public int EnumerableRepeatedTestState { get; set; }
  92. public int EnumerableRetryTestState { get; set; }
  93. internal int AssertCount
  94. {
  95. get
  96. {
  97. return _assertCount;
  98. }
  99. }
  100. public void IncrementAssertCount()
  101. {
  102. _assertCount += 1;
  103. }
  104. public void AddFormatter(ValueFormatterFactory formatterFactory)
  105. {
  106. throw new NotImplementedException();
  107. }
  108. }
  109. }