简体   繁体   English

JUnit Jupiter中的参数化测试执行(JUnit5)

[英]Parameterised Test Execution in JUnit Jupiter(JUnit5)

Is there a way to have parameterized tests in JUnit Jupiter ( Junit5 )? 有没有办法在JUnit JupiterJunit5 )中进行参数化测试?

@RunWith(Parameterized.class)

Background: 背景:

I went through How to write junit tests for interfaces? 我经历了如何编写接口的junit测试? and wanted to implement test cases as answered here using JUnit5. 并希望实施测试用例在这里找到答案使用JUnit5。 But couldn't find the same class/es in the Jupiter package. 但是在Jupiter包中找不到相同的类/ es。

Attempts: 尝试:

Looking further for substitutes I found that Extensions would be replacing the @RunWith as stated in Migrating tips 进一步寻找替代品,我发现Extensions将替换@RunWith,如迁移提示中所述

@RunWith no longer exists; @RunWith不再存在; superseded by @ExtendWith. 被@ExtendWith取代。

I tried defining a CustomExtension as provided in the sample here - MockitoExtension but couldn't succeed in getting to use the instances of classes Parameterized . 我尝试定义这里的示例中提供的CustomExtension - MockitoExtension但是无法成功使用参数化类的实例。

Looking forward to suggestions on how to test interfaces with parameterized instances of classes implementing it in JUnit5 . 期待有关如何使用在JUnit5实现它的类的参数化实例测试接口的建议。

JUnit 5 M4 was just released and now supports parameterized tests. JUnit 5 M4刚刚发布,现在支持参数化测试。

Here's the "Hello World" example: 这是“Hello World”示例:

@ParameterizedTest
@ValueSource(strings = { "Hello", "World" })
void testWithStringParameter(String argument) {
    assertNotNull(argument);
}

Please refer to the User Guide for the full documentation and additional examples. 有关完整文档和其他示例,请参阅“ 用户指南”

As of January 9th, 2017, JUnit 5 does not support parameterized tests per se, but work on that feature is in progress . 随着2017年1月9日的,JUnit的5不支持参数测试本身,而是该功能的工作正在进行中 Note that you might be able to achieve something similar with dynamic tests . 请注意,您可以通过动态测试实现类似的功能

That said, I would consider both bad ways to implement interface tests and JUnit Jupiter offers two better approaches: 也就是说,我会考虑两种不好的方法来实现接口测试,而JUnit Jupiter提供了两种更好的方法:

  1. @Test annotated interface default methods will be executed . 将执行 @Test注释的接口默认方法。 So if you have a production interface Interface you can write an interface InterfaceTest that uses default methods to test everything you want. 因此,如果您有一个生产界面Interface您可以编写一个接口InterfaceTest ,它使用默认方法来测试您想要的所有内容。 For every Impl implements Interface you can then write an ImplTest implements InterfaceTest . 对于每个Impl implements Interface您可以编写ImplTest implements InterfaceTest
  2. If you need more flexibility and would like to implement the tests for Interface in an abstract class AbstractInterfaceTest you can do that as well and then have a nested test class in your ImplTest that extends AbstractInterfaceTest . 如果您需要更多的灵活性并希望在抽象类AbstractInterfaceTest实现Interface的测试,您也可以这样做,然后在ImplTest中有一个扩展AbstractInterfaceTest嵌套测试类

Both of these approaches ensure that the test for the interface does not know the classes implementing the interface, which is a huge downside of the answer you linked to . 这两种方法都确保接口测试不会知道实现接口的类,这是您链接答案的一个巨大缺点。

You can use dynamic tests for that: 您可以使用动态测试

@TestFactory
Stream<DynamicTest> params() {

        return Stream.of(new double[][]{{2d, 4d}, {3d, 9d}, {4d, 16d}}).
                map(i ->
                        dynamicTest("Square root test " + Arrays.toString(i),
                                () -> assertEquals(i[0], Math.sqrt(i[1]))
                        )
                );
}

In my projects i have been using this library: 在我的项目中,我一直在使用这个库:

https://github.com/TNG/junit-dataprovider/ https://github.com/TNG/junit-dataprovider/

in order to add parameterization to my JUnit tests. 为了给我的JUnit测试添加参数化。 I have been using version 4.x but was never a fan of the embedded parameterization it provided. 我一直在使用版本4.x,但从未成为它提供的嵌入式参数化的粉丝。 If you are familiar with TestNg's @DataProvider then this extension is not much different. 如果您熟悉TestNg的@DataProvider,那么这个扩展名没有太大区别。

Check it out as see if it will be of any benefit to you as it was for me and my team. 检查一下,看看它对我和我的团队是否有任何好处。 Frankly i could not imagine working without it now. 坦率地说,我无法想象现在没有它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM