11using Microsoft . VisualStudio . TestTools . UnitTesting ;
22
33using System ;
4+ using System . Runtime . CompilerServices ;
45using System . Threading ;
6+ using System . Threading . Tasks ;
57
68namespace G4 . UnitTests . Attributes
79{
810 /// <summary>
9- /// A custom test method attribute that allows for retry logic in unit tests.
11+ /// Marks a test method as retryable, allowing it to be executed multiple times
12+ /// before being considered failed.
1013 /// </summary>
11- /// <param name="numberOfAttempts">The number of times to attempt the test.</param>
12- /// <param name="displayName">The display name of the test method.</param>
1314 [ AttributeUsage ( AttributeTargets . Method ) ]
14- public class RetryableTestMethodAttribute ( int numberOfAttempts , string displayName ) : TestMethodAttribute ( displayName )
15+ public class RetryableTestMethodAttribute (
16+ int numberOfAttempts ,
17+ [ CallerFilePath ] string callerFilePath = "" ,
18+ [ CallerLineNumber ] int callerLineNumber = - 1 ) : TestMethodAttribute ( callerFilePath , callerLineNumber )
1519 {
1620 /// <summary>
17- /// Initializes a new instance of the <see cref="RetryableTestMethodAttribute"/> class with a single attempt.
21+ /// Creates a retryable test attribute with a default of 1 attempt.
1822 /// </summary>
19- public RetryableTestMethodAttribute ( )
20- : this ( numberOfAttempts : 1 , displayName : null )
23+ /// <param name="callerFilePath">Automatically populated with the source file path of the calling method.</param>
24+ /// <param name="callerLineNumber">Automatically populated with the source line number of the calling method.</param>
25+ public RetryableTestMethodAttribute (
26+ [ CallerFilePath ] string callerFilePath = "" ,
27+ [ CallerLineNumber ] int callerLineNumber = - 1 )
28+ : this ( numberOfAttempts : 1 , callerFilePath , callerLineNumber )
2129 { }
2230
2331 /// <summary>
24- /// Initializes a new instance of the <see cref="RetryableTestMethodAttribute"/> class with a specified number of attempts.
25- /// </summary>
26- /// <param name="numberOfAttempts">The number of times to attempt the test.</param>
27- public RetryableTestMethodAttribute ( int numberOfAttempts )
28- : this ( numberOfAttempts , displayName : null )
29- { }
30-
31- /// <summary>
32- /// Initializes a new instance of the <see cref="RetryableTestMethodAttribute"/> class with a specified display name.
33- /// </summary>
34- /// <param name="displayName">The display name of the test method.</param>
35- public RetryableTestMethodAttribute ( string displayName )
36- : this ( numberOfAttempts : 1 , displayName )
37- { }
38-
39- /// <summary>
40- /// Gets or sets the delay between test attempts in milliseconds.
32+ /// Gets or sets the delay (in milliseconds) between retry attempts.
4133 /// </summary>
34+ /// <remarks>This delay is applied after a failed attempt and before retrying. Default is 1000 ms (1 second).</remarks>
4235 public int DelayBetweenAttempts { get ; set ; } = 1000 ;
4336
4437 /// <summary>
45- /// Gets the number of times the test will be attempted .
38+ /// Gets the total number of attempts allowed for this test method .
4639 /// </summary>
40+ /// <remarks>This includes the initial attempt. For example, a value of 3 means: 1 initial run + 2 retries.</remarks>
4741 public int NumberOfAttempts { get ; } = numberOfAttempts ;
4842
49- /// <summary>
50- /// Executes the test method, retrying up to <see cref="NumberOfAttempts"/> times if it fails.
51- /// </summary>
52- /// <param name="testMethod">The test method to be executed.</param>
53- /// <returns>An array of <see cref="TestResult"/> objects representing the outcome of each test attempt.</returns>
54- public override TestResult [ ] Execute ( ITestMethod testMethod )
43+ /// <inheritdoc />
44+ public override async Task < TestResult [ ] > ExecuteAsync ( ITestMethod testMethod )
5545 {
5646 // Array to hold the results of each test attempt
5747 TestResult [ ] results = [ ] ;
@@ -63,7 +53,7 @@ public override TestResult[] Execute(ITestMethod testMethod)
6353 try
6454 {
6555 // Execute the test method
66- results = base . Execute ( testMethod ) ;
56+ results = await base . ExecuteAsync ( testMethod ) ;
6757
6858 // If all test results are successful, return the results
6959 if ( Array . TrueForAll ( results , r => r . Outcome == UnitTestOutcome . Passed ) )
0 commit comments