我使用 JUnit5 进行集成测试,其中我有一个在类中重复测试的用例,但我想保留测试的原始顺序。JUnit5 有办法实现这一点吗?
@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());
}
}
Run Code Online (Sandbox Code Playgroud)
这输出:
One #0
One #1
One #2
two #0
two #1
two #2
Run Code Online (Sandbox Code Playgroud)
我想得到:
One #0
two #0
One #1
two #1
One #2
two #2
Run Code Online (Sandbox Code Playgroud)
首先我想到了以下解决方案:
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))
);
}
}
Run Code Online (Sandbox Code Playgroud)
但由于 JUnit 5 的限制,它不起作用:
测试方法不能组合 RepeatedTest 和 ParameterizedTest 注解
我能想到的实现您目标的最佳方法不太优雅,但仍然符合您的期望:
@RepeatedTest(value = nrOfIterations)
void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) {
final int runNr = repetitionInfo.getCurrentRepetition() - 1;
test1(runNr);
test2(runNr);
assertEquals(3, repetitionInfo.getTotalRepetitions());
}
Run Code Online (Sandbox Code Playgroud)
缺点是仅将每个完整的重复显示为单个测试运行,而不是按照您的要求显示每个单独的测试。
我知道这并不能完全回答您的问题,我宁愿将其作为评论发布,但我不具备所需的格式化功能和文本长度;最重要的是,我的解决方案至少部分满足您的要求:)
| 归档时间: |
|
| 查看次数: |
2055 次 |
| 最近记录: |