In this tutorial, I will show you how you can repeat test executions using JUnit 5. The examples use the following dependency of JUnit.
With JUnit 5.10.0 you can use the failureThreshold experimental feature.
You can repeat a test by using the @RepeatedTest annotation. The annotation takes as input the number of times the tests are repeated. In the following example, the test is repeated 5 times.
A test annotated @RepeatedTest can access the repetitionInfo if you add it as a parameter as is done in the second test case. The repetitionInfo instance you can get information about the repetitions. It has the following methods:
- getCurrentRepetition: current repetition
- getTotalRepetitions: The total number of repetitions
- getFailureCount: number of times the test failed
- getFailureThreshold: maximum number of failed tests
RepeatedTest with failure threshold
With failureThreshold, you can specify how many times the test can fail before the remaining repetitions are skipped. This is a new experimental feature added in JUnit 5.10. In the following example, a test is repeated 8 times if it does not fail more than 2 times.
If tests are executed in parallel there is no guarantee that the failureThreshold count is correct.
Set repeated test name
You can also change the display name of repeated tests. You can use the following placeholders:
You can use these placeholders inside the name property of the @RepeatedTest annotation. In the following example
The output of the previous two test cases looks like the following:
In this tutorial, you learned how you can repeat tests using JUnit5. If you want to know more about JUnit5 or repeating test cases, Please take a look at the official documentation.