I try to write my own JUnit 5 extension, providing some simple information about test duration. I also want to print out the repetition information but how can I access these informations in the extension? Are there any simple ways instead of reflection or writing and parsing the numbers to the display name?
simple example:
@ExtendWith(TimingExtension.class)
public class MyTestClass {
@RepeatedTest(value = 5, name = "{currentRepetition}/{totalRepetitions}")
public void myTest(TestInfo testInfo, RepetitionInfo repInfo) {
// do some work here...
}
}
public class TimingExtension implements AfterTestExecutionCallback {
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
if(context.getRequiredTestMethod().getDeclaredAnnotation(RepeatedTest.class) != null) {
System.out.println("This was test X of Y"); // how to get currentRepetition and totalRepetitions here?
}
}
}
Unfortunately there is no support for parameter injection in extensions. It's only one way. So in order to get
RepetitionInfo
in yourTimingExtension
you have to set it.First you need to use
@RegisterExtension
e.g.