EnumerableRetryTestCommand.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections;
  3. using System.Reflection;
  4. using NUnit.Framework;
  5. using NUnit.Framework.Interfaces;
  6. using NUnit.Framework.Internal;
  7. using NUnit.Framework.Internal.Commands;
  8. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  9. namespace UnityEngine.TestTools
  10. {
  11. internal class EnumerableRetryTestCommand : DelegatingTestCommand, IEnumerableTestMethodCommand
  12. {
  13. private int retryCount;
  14. public EnumerableRetryTestCommand(RetryAttribute.RetryCommand commandToReplace) : base(commandToReplace.GetInnerCommand())
  15. {
  16. retryCount = (int) typeof(RetryAttribute.RetryCommand)
  17. .GetField("_retryCount", BindingFlags.NonPublic | BindingFlags.Instance)
  18. .GetValue(commandToReplace);
  19. }
  20. public override TestResult Execute(ITestExecutionContext context)
  21. {
  22. throw new NotImplementedException("Use ExecuteEnumerable");
  23. }
  24. public IEnumerable ExecuteEnumerable(ITestExecutionContext context)
  25. {
  26. var unityContext = (UnityTestExecutionContext)context;
  27. if (unityContext.EnumerableTestState?.GetHashCode() == null)
  28. {
  29. unityContext.EnumerableTestState = new EnumerableTestState();
  30. }
  31. int count = unityContext.EnumerableTestState.Retry;
  32. var firstCycleAfterResume = count > 0;
  33. while (count < retryCount || (firstCycleAfterResume && count <= retryCount))
  34. {
  35. if (!firstCycleAfterResume)
  36. {
  37. count++;
  38. }
  39. firstCycleAfterResume = false;
  40. unityContext.EnumerableTestState.Retry = count;
  41. if (innerCommand is IEnumerableTestMethodCommand)
  42. {
  43. var executeEnumerable = ((IEnumerableTestMethodCommand)innerCommand).ExecuteEnumerable(context);
  44. foreach (var iterator in executeEnumerable)
  45. {
  46. yield return iterator;
  47. }
  48. }
  49. else
  50. {
  51. context.CurrentResult = innerCommand.Execute(context);
  52. }
  53. if (context.CurrentResult.ResultState != ResultState.Failure)
  54. {
  55. break;
  56. }
  57. }
  58. unityContext.EnumerableTestState.Retry = 0;
  59. }
  60. }
  61. }