No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RetryCommand.cs 3.4KB

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