Contents

@RepeatedTest using JUnit 5

Written by: David Vlijmincx

Introduction

In this tutorial, I will show you how you can repeat test executions using JUnit 5. The examples use the following dependency of JUnit.

1
2
3
4
5
6
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.10.0</version>
    <scope>test</scope>
</dependency>

With JUnit 5.10.0 you can use the failureThreshold experimental feature.

@RepeatedTest example

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.

1
2
3
4
5
6
7
8
9
@RepeatedTest(5)
void testCase(){

}

@RepeatedTest(5)
void repeatWithRepetitionInfo(RepetitionInfo repetitionInfo){
    System.out.println("rep number: " + repetitionInfo.getCurrentRepetition());
}

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.

1
2
3
4
@RepeatedTest(value = 8, failureThreshold = 2)
void testCaseWithThreshold(){

}

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:

  • currentRepetition
  • totalRepetitions
  • displayName

You can use these placeholders inside the name property of the @RepeatedTest annotation. In the following example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@RepeatedTest(value = 1, name = "{displayName} number: {currentRepetition} of {totalRepetitions}")
void testCaseWithCustomName(){

}

@RepeatedTest(value = 1, name = RepeatedTest.LONG_DISPLAY_NAME)
@DisplayName("Display name of test:")
void testCaseLongDisplayName(){

}

The output of the previous two test cases looks like the following:

1
2
3
4
testCaseWithCustomName()
- testCaseWithCustomName() number: 1 of 1
Display name of test:
- Display name of test: :: repetition 1 of 1

Conclusion

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.