Using the @RetryingTest Annotation from the org.junit-pioneer Library
Instead of using the standard @Test annotation, you can use the @RetryingTest annotation from the org.junit-pioneer library.
This annotation is intended to rerun a test multiple times in case it fails, up to the maximum number of attempts defined.
Example:
@RetryingTest(maxAttempts = 3)
In this example, the test will run up to three times
Example Test:
private static int runCount = 0;
@RetryingTest(maxAttempts = 3)
void succeedsOnSecondAttempt() {
runCount++;
System.out.println("Running attempt #" + runCount);
if (runCount < 2) {
throw new RuntimeException("Failing on first attempt");
}
System.out.println("Success on attempt #" + runCount);
}
In this example, the test will run up to three times. The test fails on the first attempt and succeeds on the second, so it does not try a third time because it succeeded. If it had failed on both the second and third attempts, the test would have ultimately failed.
Display in Extent Report:
-
If all three attempts fail, the report will look like this:
- Three executions of the same test (
UsingTagsTests) listed as[1],[2],[3]. - Each failed attempt is marked as
Skip. - After the final attempt, the result is marked as
Fail. - The log shows that an error occurred and the test was retried automatically.
- Three executions of the same test (
-
If the test passes on the second attempt:
- The first attempt will appear as
SkiporFail. - The second attempt will be marked as
Pass. - No additional attempts will be executed.
- The first attempt will appear as
-
If the test passes on the first attempt:
- Only one attempt will appear, marked as
Pass. - No retries will be made.
- Only one attempt will appear, marked as
Display in Report Portal:
-
If all three attempts fail,
Status: ❌ FAILED -
Retries:
- Attempt 1: ❌ Failed
- Attempt 2: ❌ Failed
- Attempt 3: ❌ Failed
-
Logs:
[ERROR] AssertionError at line 24[INFO] Retrying test (2 of 3)[INFO] Retrying test (3 of 3)[ERROR] AssertionError at line 24
-
Final result: Test failed after 3 attempts.
-
If the test passes on the second attempt:
-
Status: ✅ PASSED
-
Retries:
-
Attempt 1: ❌ Failed
-
Attempt 2: ✅ Passed
-
The framework stops further retries after the test passes.
-
Logs will show:
-
[ERROR] AssertionError at line 24 -
[INFO] Retrying test (2 of 3) -
[INFO] Test passed on retry.
-
-
If the test passes on the first attempt:
-
Status: ✅ PASSED
-
Retries:
- Attempt 1: ✅ Passed
-
Since the test passed on the first try, no retry is executed.
-
Logs will simply confirm success:
[INFO] Test started[INFO] Test passed