JUnit 5 Repeated Tests - @RepeatedTest

How to repeatedly execute test code with @RepeatedTest

Repeated Tests

Annotating a method with @RepeatedTest repeats the test as many times as the specified value.

The default display name for each repeated execution is repetition <current repetition> of <total repetitions>.

Specifying the Display Name

The name attribute of @RepeatedTest lets you specify the display name for repeated tests. Placeholders can be used in the name.

  • displayName: Display name of the test method. The default is the method name, but @DisplayName takes precedence.
  • currentRepetition: Current repetition count, starting at 1.
  • totalRepetitions: Total repetition count.

The predefined long display pattern is {displayName} :: repetition {currentRepetition} of {totalRepetitions}.

Receiving Repetition Information

A test method annotated with @RepeatedTest can receive a RepetitionInfo object as an argument. It provides getCurrentRepetition() and getTotalRepetitions().

RepetitionInfo can also be received in @BeforeEach and @AfterEach. If ordinary @Test methods are mixed in, RepetitionInfo cannot be resolved for those tests and a runtime error occurs.