如何获取 JUnit 5 中测试的当前重复计数?

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

在 JUnit 5 中,方法可以用 @RepeatedTest(n) 注释,因此它是 运行 n次。如何显示该测试方法中的当前重复次数?

如果您在测试方法中提供 RepetitionInfo 类型的参数,解析器将自动提供该类型的实例:

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

如您所见,您可以通过提供的参数访问当前重复次数以及总重复次数 (n)。