Issue
I'm using JUnit5 for the integration tests, where I have a use-case for repeating tests within the class, but I'd like to preserve the original order of tests. Is there a way with JUnit5 to achieve this?
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class TestExample {
final int nrOfIterations = 3;
@Order(1)
@DisplayName("One")
@RepeatedTest(value = nrOfIterations, name = RepeatedTest.LONG_DISPLAY_NAME)
void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) {
System.out.println("One #" + (repetitionInfo.getCurrentRepetition()-1));
assertEquals(3, repetitionInfo.getTotalRepetitions());
}
@Order(2)
@DisplayName("Two")
@RepeatedTest(value = nrOfIterations, name = RepeatedTest.LONG_DISPLAY_NAME)
void repeatedTestWithRepetitionInfoCont(RepetitionInfo repetitionInfo) {
System.out.println("two #" + (repetitionInfo.getCurrentRepetition()-1));
assertEquals(3, repetitionInfo.getTotalRepetitions());
}
}
This outputs:
One #0
One #1
One #2
two #0
two #1
two #2
And I want to get:
One #0
two #0
One #1
two #1
One #2
two #2
Solution
First I was thinking about the following solution:
class RepeatTest {
final int nrOfIterations = 3;
void test1(int runNr) {
System.out.println("One #" + runNr);
}
void test2(int runNr) {
System.out.println("Two #" + runNr);
}
@RepeatedTest(value = nrOfIterations)
@TestFactory
Stream<DynamicNode> factory(RepetitionInfo repetitionInfo) {
final int runNr = repetitionInfo.getCurrentRepetition() - 1;
return Stream.of(
DynamicTest.dynamicTest("One", () -> test1(runNr)),
DynamicTest.dynamicTest("Two", () -> test2(runNr))
);
}
}
but due to the limitation in JUnit 5 it does not work:
Test methods cannot combine RepeatedTest and ParameterizedTest annotations
The best way I can think about to reach your goal is less elegant, but is still does what you expect:
@RepeatedTest(value = nrOfIterations)
void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) {
final int runNr = repetitionInfo.getCurrentRepetition() - 1;
test1(runNr);
test2(runNr);
assertEquals(3, repetitionInfo.getTotalRepetitions());
}
The disadvantage is that only each complete repetition is displayed as a single test run, and not each individual tests as you requested.
I am aware that this does not fully answer your question and I'd rather post it as a comment, but I would not have the required formatting capabilities and text length; and above all my solution at least partially does what you requested :)
Answered By - Honza Zidek
Answer Checked By - Senaida (JavaFixing Volunteer)