如何在 class 级别重复 JUnit5 测试?
How to repeat JUnit5 tests on class level?
我正在使用 JUnit5 进行集成测试,其中我有一个在 class 中重复测试的用例,但我想保留原始测试顺序。 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());
}
}
这输出:
One #0
One #1
One #2
two #0
two #1
two #2
我想得到:
One #0
two #0
One #1
two #1
One #2
two #2
首先我想到了以下解决方案:
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))
);
}
}
但由于 JUnit 5 的限制,它不起作用:
Test methods cannot combine RepeatedTest and ParameterizedTest annotations
我能想到的达到您的目标的最佳方式不是那么优雅,但仍然符合您的期望:
@RepeatedTest(value = nrOfIterations)
void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) {
final int runNr = repetitionInfo.getCurrentRepetition() - 1;
test1(runNr);
test2(runNr);
assertEquals(3, repetitionInfo.getTotalRepetitions());
}
缺点是只有每个完整的重复都显示为单个测试 运行,而不是您请求的每个单独的测试。
我知道这不能完全回答您的问题,我宁愿post将其作为评论,但我没有所需的格式化功能和文本长度;最重要的是,我的解决方案至少 部分 满足您的要求:)
我正在使用 JUnit5 进行集成测试,其中我有一个在 class 中重复测试的用例,但我想保留原始测试顺序。 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());
}
}
这输出:
One #0
One #1
One #2
two #0
two #1
two #2
我想得到:
One #0
two #0
One #1
two #1
One #2
two #2
首先我想到了以下解决方案:
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))
);
}
}
但由于 JUnit 5 的限制,它不起作用:
Test methods cannot combine RepeatedTest and ParameterizedTest annotations
我能想到的达到您的目标的最佳方式不是那么优雅,但仍然符合您的期望:
@RepeatedTest(value = nrOfIterations)
void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) {
final int runNr = repetitionInfo.getCurrentRepetition() - 1;
test1(runNr);
test2(runNr);
assertEquals(3, repetitionInfo.getTotalRepetitions());
}
缺点是只有每个完整的重复都显示为单个测试 运行,而不是您请求的每个单独的测试。
我知道这不能完全回答您的问题,我宁愿post将其作为评论,但我没有所需的格式化功能和文本长度;最重要的是,我的解决方案至少 部分 满足您的要求:)