暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RepeatCommand.cs 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using NUnit.Framework.Interfaces;
  5. using NUnit.Framework.Internal;
  6. using NUnit.Framework.Internal.Commands;
  7. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  8. namespace UnityEngine.TestTools
  9. {
  10. internal class RepeatCommand : DelegatingTestCommand, IEnumerableTestMethodCommand
  11. {
  12. public RepeatCommand(TestCommand innerCommand)
  13. : base(innerCommand)
  14. {
  15. }
  16. public override TestResult Execute(ITestExecutionContext context)
  17. {
  18. throw new NotImplementedException("Use ExecuteEnumerable");
  19. }
  20. public IEnumerable ExecuteEnumerable(ITestExecutionContext context)
  21. {
  22. var unityContext = (UnityTestExecutionContext)context;
  23. if (unityContext.RetryRepeatState?.GetHashCode() == null)
  24. {
  25. unityContext.RetryRepeatState = new EnumerableTestState();
  26. }
  27. while(unityContext.RetryRepeatState.Repeat < unityContext.RepeatCount + 1)
  28. {
  29. if (innerCommand is IEnumerableTestMethodCommand)
  30. {
  31. var executeEnumerable = ((IEnumerableTestMethodCommand)innerCommand).ExecuteEnumerable(context);
  32. foreach (var iterator in executeEnumerable)
  33. {
  34. yield return iterator;
  35. }
  36. }
  37. else
  38. {
  39. context.CurrentResult = innerCommand.Execute(context);
  40. }
  41. if (context.CurrentResult.ResultState != ResultState.Success)
  42. {
  43. unityContext.RetryRepeatState.Repeat++;
  44. break;
  45. }
  46. if (unityContext.RetryRepeatState.Repeat < unityContext.RepeatCount)
  47. {
  48. ReportTestFinishStartPair(unityContext);
  49. }
  50. unityContext.RetryRepeatState.Repeat++;
  51. }
  52. SetIterationProperty(unityContext, unityContext.RetryRepeatState.Repeat-1);
  53. unityContext.RetryRepeatState.Repeat = 0;
  54. }
  55. private static void ReportTestFinishStartPair(UnityTestExecutionContext unityContext)
  56. {
  57. unityContext.CurrentResult.StartTime = unityContext.StartTime;
  58. unityContext.CurrentResult.EndTime = DateTime.UtcNow;
  59. long tickCount = Stopwatch.GetTimestamp() - unityContext.StartTicks;
  60. double seconds = (double) tickCount / Stopwatch.Frequency;
  61. unityContext.CurrentResult.Duration = seconds;
  62. SetIterationProperty(unityContext, unityContext.RetryRepeatState.Repeat);
  63. unityContext.Listener.TestFinished(unityContext.CurrentResult);
  64. // Start new test iteration
  65. unityContext.CurrentResult = unityContext.CurrentTest.MakeTestResult();
  66. unityContext.StartTime = DateTime.UtcNow;
  67. unityContext.StartTicks = Stopwatch.GetTimestamp();
  68. unityContext.Listener.TestStarted(unityContext.CurrentTest);
  69. }
  70. private static void SetIterationProperty(UnityTestExecutionContext unityContext, int iteration)
  71. {
  72. unityContext.CurrentResult.Test.Properties.Set("repeatIteration", iteration);
  73. }
  74. }
  75. }