Mahozad
Mahozad

Reputation: 24592

How to get the current repetition count of the test in JUnit 5?

In JUnit 5, a method can be annotated with @RepeatedTest(n) so that it is run n times. How can I display the current number of repetition in that test method?

Upvotes: 3

Views: 1176

Answers (1)

Mahozad
Mahozad

Reputation: 24592

If you provide a parameter of type RepetitionInfo in your test method, a resolver will automatically provide an instance of that:

@RepeatedTest(5)
void repeat(RepetitionInfo repetitionInfo) { // Automatically injected
    System.out.println(repetitionInfo.getCurrentRepetition());
    System.out.println(repetitionInfo.getTotalRepetitions());
}

As you can see, you can access the current repetition count as well as the total repetitions (n) by the provided parameter.

Upvotes: 6

Related Questions