EnumerableRetryTestCommand.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. int count = unityContext.EnumerableRetryTestState;
  28. while (count < retryCount)
  29. {
  30. count++;
  31. unityContext.EnumerableRetryTestState = count;
  32. if (innerCommand is IEnumerableTestMethodCommand)
  33. {
  34. var executeEnumerable = ((IEnumerableTestMethodCommand)innerCommand).ExecuteEnumerable(context);
  35. foreach (var iterator in executeEnumerable)
  36. {
  37. yield return iterator;
  38. }
  39. }
  40. else
  41. {
  42. context.CurrentResult = innerCommand.Execute(context);
  43. }
  44. if (context.CurrentResult.ResultState != ResultState.Failure)
  45. {
  46. break;
  47. }
  48. }
  49. unityContext.EnumerableRetryTestState = 0;
  50. }
  51. }
  52. }