【问题标题】:Junit ParameterizedTest with expensive setup具有昂贵设置的 Junit ParameterizedTest
【发布时间】:2021-04-29 11:08:10
【问题描述】:

我正在寻找一种通过昂贵的设置优化运行多个参数化测试的方法

我当前的代码是这样的

    @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,然后安装程序在每个测试中只运行一次,但我正在寻找一种方法将两种情况都保留在一个测试中

【问题讨论】:

  • 我的第一个问题是“为什么你有这么昂贵的设置?”。这是一种气味,表明您正在测试的方式没有问题。感觉考试是事后才想到的
  • @Bart 我不知道我是否同意你的看法!我认为这取决于您要测试的内容,尤其是在集成测试中。
  • 那些测试是针对搜索引擎的,可能整个测试应该有一个包含所有测试数据的设置方法,但是我们正在添加新的搜索字段,这些字段需要新的复杂数据来验证边界情况,因此将其全部添加在一种设置中可能会影响其他已经存在的测试

标签: java unit-testing integration-testing junit5 junit-jupiter


【解决方案1】:

在这种情况下,您可以使用@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!

【讨论】:

  • 但请注意,afaik surefire 不会在 @Nested 类中执行测试。
  • 另外,@lukwas 请注意,将测试分成两个文件是没有问题的......
猜你喜欢
  • 1970-01-01
  • 2011-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-31
  • 1970-01-01
  • 2018-06-05
  • 2012-08-27
相关资源
最近更新 更多