DefaultTestWorkItem.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. foreach (var workItemStep in ((IEnumerableTestMethodCommand)_command).ExecuteEnumerable(Context))
  46. {
  47. ResultedInDomainReload = false;
  48. if (workItemStep is IEditModeTestYieldInstruction)
  49. {
  50. var editModeTestYieldInstruction = (IEditModeTestYieldInstruction)workItemStep;
  51. yield return editModeTestYieldInstruction;
  52. var enumerator = editModeTestYieldInstruction.Perform();
  53. while (true)
  54. {
  55. bool moveNext;
  56. try
  57. {
  58. moveNext = enumerator.MoveNext();
  59. }
  60. catch (Exception e)
  61. {
  62. Context.CurrentResult.RecordException(e);
  63. break;
  64. }
  65. if (!moveNext)
  66. {
  67. break;
  68. }
  69. yield return null;
  70. }
  71. }
  72. else
  73. {
  74. yield return workItemStep;
  75. }
  76. }
  77. Result = Context.CurrentResult;
  78. }
  79. finally
  80. {
  81. WorkItemComplete();
  82. }
  83. }
  84. }
  85. }