Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. using NUnit.Framework.Interfaces;
  2. using NUnit.Framework.Internal;
  3. using NUnit.Framework.Internal.Commands;
  4. using System;
  5.  
  6. namespace NUnit.Framework
  7. {
  8. /// <summary>
  9. /// Specifies that a test method should be rerun on failure up to the specified
  10. /// maximum number of times.
  11. /// </summary>
  12. [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
  13. public class RetryTestEvenWhenExceptionIsThrownAttribute : PropertyAttribute, IRepeatTest
  14. {
  15. private readonly int _tryCount;
  16.  
  17. /// <summary>
  18. /// Construct a <see cref="RetryAttribute" />
  19. /// </summary>
  20. /// <param name="tryCount">The maximum number of times the test should be run if it fails</param>
  21. public RetryTestEvenWhenExceptionIsThrownAttribute(int tryCount) : base(tryCount)
  22. {
  23. _tryCount = tryCount;
  24. }
  25.  
  26. #region IRepeatTest Members
  27.  
  28. /// <summary>
  29. /// Wrap a command and return the result.
  30. /// </summary>
  31. /// <param name="command">The command to be wrapped</param>
  32. /// <returns>The wrapped command</returns>
  33. public TestCommand Wrap(TestCommand command)
  34. {
  35. return new RetryCommand(command, _tryCount);
  36. }
  37.  
  38. #endregion
  39.  
  40. #region Nested RetryCommand Class
  41.  
  42. /// <summary>
  43. /// The test command for the <see cref="RetryAttribute"/>
  44. /// </summary>
  45. public class RetryCommand : DelegatingTestCommand
  46. {
  47. private readonly int _tryCount;
  48.  
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="RetryCommand"/> class.
  51. /// </summary>
  52. /// <param name="innerCommand">The inner command.</param>
  53. /// <param name="tryCount">The maximum number of repetitions</param>
  54. public RetryCommand(TestCommand innerCommand, int tryCount)
  55. : base(innerCommand)
  56. {
  57. _tryCount = tryCount;
  58. }
  59.  
  60. /// <summary>
  61. /// Runs the test, saving a TestResult in the supplied TestExecutionContext.
  62. /// </summary>
  63. /// <param name="context">The context in which the test should run.</param>
  64. /// <returns>A TestResult</returns>
  65. public override TestResult Execute(TestExecutionContext context)
  66. {
  67. int count = _tryCount;
  68.  
  69. while (count-- > 0)
  70. {
  71. try
  72. {
  73. context.CurrentResult = innerCommand.Execute(context);
  74. }
  75. catch (Exception ex)
  76. {
  77. Assert.Fail("Exception : " + ex.Message);
  78. }
  79.  
  80. if (context.CurrentResult.ResultState != ResultState.Failure)
  81. break;
  82.  
  83. // Clear result for retry
  84. if (count > 0)
  85. {
  86. context.CurrentResult = context.CurrentTest.MakeTestResult();
  87. context.CurrentRepeatCount++; // increment Retry count for next iteration. will only happen if we are guaranteed another iteration
  88. }
  89. }
  90.  
  91. return context.CurrentResult;
  92. }
  93. }
  94.  
  95. #endregion
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement