DefaultTestWorkItem.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using NUnit.Framework.Interfaces;
  5. using NUnit.Framework.Internal;
  6. using NUnit.Framework.Internal.Commands;
  7. using NUnit.Framework.Internal.Execution;
  8. using UnityEngine.TestTools;
  9. using SetUpTearDownCommand = NUnit.Framework.Internal.Commands.SetUpTearDownCommand;
  10. using TestActionCommand = NUnit.Framework.Internal.Commands.TestActionCommand;
  11. namespace UnityEngine.TestRunner.NUnitExtensions.Runner
  12. {
  13. internal class EditModeTestCallbacks
  14. {
  15. public static Action RestoringTestContext { get; set; }
  16. }
  17. internal class DefaultTestWorkItem : UnityWorkItem
  18. {
  19. private TestCommand _command;
  20. public DefaultTestWorkItem(TestMethod test, ITestFilter filter)
  21. : base(test, null)
  22. {
  23. _command = TestCommandBuilder.BuildTestCommand(test, filter);
  24. }
  25. protected override IEnumerable PerformWork()
  26. {
  27. if (m_DontRunRestoringResult && EditModeTestCallbacks.RestoringTestContext != null)
  28. {
  29. EditModeTestCallbacks.RestoringTestContext();
  30. Result = Context.CurrentResult;
  31. yield break;
  32. }
  33. try
  34. {
  35. if (_command is SkipCommand || _command is FailCommand)
  36. {
  37. Result = _command.Execute(Context);
  38. yield break;
  39. }
  40. if (!(_command is IEnumerableTestMethodCommand))
  41. {
  42. Debug.LogError("Cannot perform work on " + _command.GetType().Name);
  43. yield break;
  44. }
  45. if (Context.TestCaseTimeout == 0)
  46. {
  47. Context.TestCaseTimeout = k_DefaultTimeout;
  48. }
  49. foreach (var workItemStep in ((IEnumerableTestMethodCommand)_command).ExecuteEnumerable(Context))
  50. {
  51. ResultedInDomainReload = false;
  52. if (workItemStep is IEditModeTestYieldInstruction)
  53. {
  54. var editModeTestYieldInstruction = (IEditModeTestYieldInstruction)workItemStep;
  55. yield return editModeTestYieldInstruction;
  56. var enumerator = editModeTestYieldInstruction.Perform();
  57. while (true)
  58. {
  59. bool moveNext;
  60. try
  61. {
  62. moveNext = enumerator.MoveNext();
  63. }
  64. catch (Exception e)
  65. {
  66. Context.CurrentResult.RecordException(e);
  67. break;
  68. }
  69. if (!moveNext)
  70. {
  71. break;
  72. }
  73. yield return null;
  74. }
  75. }
  76. else
  77. {
  78. yield return workItemStep;
  79. }
  80. }
  81. Result = Context.CurrentResult;
  82. }
  83. finally
  84. {
  85. WorkItemComplete();
  86. }
  87. }
  88. }
  89. }