具有昂贵设置的 Junit ParameterizedTest
Junit ParameterizedTest with expensive setup
我正在寻找一种方法来优化 运行 具有昂贵设置的多个参数化测试
我当前的代码如下所示
@ParameterizedTest
@MethodSource("test1CasesProvider")
void test1(String param) {
// expensive setup code 1
// execution & assertions
}
@ParameterizedTest
@MethodSource("test2CasesProvider")
void test2(String param) {
// expensive setup code 2
// execution & assertions
}
但在那种情况下,每个测试用例都会运行昂贵的设置,这不是很好
我可以将此测试分成两个单独的测试并使用 @BeforeAll
,然后每次测试仅运行一次设置,但我正在寻找一种方法将两种情况都保留在一个测试中
在这种情况下,您可以使用 @Nested
测试,如下所示:
public class MyTests {
@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class Test1Cases {
@BeforeAll
void setUpForTest1() {
System.out.println("Test1Cases: setting up things!");
}
@AfterAll
void tearDownForTest1() {
System.out.println("Test1Cases: tear down things!");
}
@ParameterizedTest
@MethodSource("source")
void shouldDoSomeTests(String testCase) {
System.out.println("Test1Cases: Doing parametrized tests: " + testCase);
}
Stream<Arguments> source() {
return Stream.of(
Arguments.of("first source param!"),
Arguments.of("second source param!"),
Arguments.of("third source param!")
);
}
}
@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class Test2Cases {
@BeforeAll
void setUpForTest2() {
System.out.println("Test2Cases: setting up things!");
}
@AfterAll
void tearDownForTest2() {
System.out.println("Test2Cases: tear down things!");
}
@ParameterizedTest
@MethodSource("source")
void shouldDoSomeTests(String testCase) {
System.out.println("Test2Cases: Doing parametrized tests: " + testCase);
}
Stream<Arguments> source() {
return Stream.of(
Arguments.of("first source param!"),
Arguments.of("second source param!"),
Arguments.of("third source param!")
);
}
}
}
这种情况下的输出是:
Test2Cases: setting up things!
Test2Cases: Doing parametrized tests: first source param!
Test2Cases: Doing parametrized tests: second source param!
Test2Cases: Doing parametrized tests: third source param!
Test2Cases: tear down things!
Test1Cases: setting up things!
Test1Cases: Doing parametrized tests: first source param!
Test1Cases: Doing parametrized tests: second source param!
Test1Cases: Doing parametrized tests: third source param!
Test1Cases: tear down things!
我正在寻找一种方法来优化 运行 具有昂贵设置的多个参数化测试
我当前的代码如下所示
@ParameterizedTest
@MethodSource("test1CasesProvider")
void test1(String param) {
// expensive setup code 1
// execution & assertions
}
@ParameterizedTest
@MethodSource("test2CasesProvider")
void test2(String param) {
// expensive setup code 2
// execution & assertions
}
但在那种情况下,每个测试用例都会运行昂贵的设置,这不是很好
我可以将此测试分成两个单独的测试并使用 @BeforeAll
,然后每次测试仅运行一次设置,但我正在寻找一种方法将两种情况都保留在一个测试中
在这种情况下,您可以使用 @Nested
测试,如下所示:
public class MyTests {
@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class Test1Cases {
@BeforeAll
void setUpForTest1() {
System.out.println("Test1Cases: setting up things!");
}
@AfterAll
void tearDownForTest1() {
System.out.println("Test1Cases: tear down things!");
}
@ParameterizedTest
@MethodSource("source")
void shouldDoSomeTests(String testCase) {
System.out.println("Test1Cases: Doing parametrized tests: " + testCase);
}
Stream<Arguments> source() {
return Stream.of(
Arguments.of("first source param!"),
Arguments.of("second source param!"),
Arguments.of("third source param!")
);
}
}
@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class Test2Cases {
@BeforeAll
void setUpForTest2() {
System.out.println("Test2Cases: setting up things!");
}
@AfterAll
void tearDownForTest2() {
System.out.println("Test2Cases: tear down things!");
}
@ParameterizedTest
@MethodSource("source")
void shouldDoSomeTests(String testCase) {
System.out.println("Test2Cases: Doing parametrized tests: " + testCase);
}
Stream<Arguments> source() {
return Stream.of(
Arguments.of("first source param!"),
Arguments.of("second source param!"),
Arguments.of("third source param!")
);
}
}
}
这种情况下的输出是:
Test2Cases: setting up things!
Test2Cases: Doing parametrized tests: first source param!
Test2Cases: Doing parametrized tests: second source param!
Test2Cases: Doing parametrized tests: third source param!
Test2Cases: tear down things!
Test1Cases: setting up things!
Test1Cases: Doing parametrized tests: first source param!
Test1Cases: Doing parametrized tests: second source param!
Test1Cases: Doing parametrized tests: third source param!
Test1Cases: tear down things!