【问题标题】:JUnit5: Running a single "instance" of parameterized testJUnit5:运行参数化测试的单个“实例”
【发布时间】:2021-11-10 21:04:22
【问题描述】:

假设我们在 JUnit5 中有一个参数化测试 exampleTest

public class ExampleTestClass {

    @ParameterizedTest(name = "exampleTest[{0}]")
    @MethodSource("StringParams#getStrings")
    void exampleTest(String input) {
        // ...
    }
}

public class StringParams {
    static Stream<String> getStrings() {
        return Stream.of("a", "b", "c");
    }
}

有没有办法只在JUnit5 中执行exampleTest 的一个实例化?在JUnit4+Maven+Surefire 中,以下内容将起作用:

mvn test -Dtest="ExampleTestClass#exampleTest[a]"

但它似乎不适用于JUnit5+Maven+Surefire。是不是因为进入测试选择过程的测试名称(哪些测试将被执行,哪些将被省略)与@ParameterizedTest(name = "exampleTest[{0}])中指定的不同?

【问题讨论】:

    标签: maven junit5 maven-surefire-plugin


    【解决方案1】:

    我没试过,但surefire:test#test 的文档写道:

    [...] 自 2.7.3 起,您可以通过添加#myMethod 或#my*ethod 在测试中执行有限数量的方法。例如,-Dtest=MyTest#myMethod。 junit 4.x 和 TestNg 支持此功能。

    从 2.19 开始,一个参数(JUnit 4、JUnit 4.7+、TestNG)支持复杂的语法:

    (我强调。)

    【讨论】:

    • 我认为这是指非参数化的测试选择器。我可以确认这些在 JUnit5 中仍然有效,尽管文档中没有明确列出。可悲的是,他们的文档没有提到 JUnit5 中的参数化测试选择器支持的任何内容 - 基于我无法找到有关此主题的任何内容,我开始认为根本不支持此功能。
    猜你喜欢
    • 1970-01-01
    • 2019-01-23
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 2019-04-08
    相关资源
    最近更新 更多