使用 JUnit 5 的 @ParametrizedTest 而不是

首先,在 Junit 5 动态测试指南的结论中,它们是什么意思?

参数化测试可以替代本文中的许多示例。但是,动态测试与参数化测试不同,因为它们支持完整的测试生命周期,而参数化测试不支持。

我浏览了JUnit 5 – Parameterized Tests并相信我理解了句法层面的差异,并且相信我明白了这一点:

此外,动态测试在如何生成输入以及如何执行测试方面提供了更大的灵活性。

但是看起来,为什么有人更喜欢参数化测试而不是动态测试?


阿晨1998
浏览 193回答 2
2回答

绝地无双

动态测试,我将它们称为 testlets,只是软/分组断言(assertAll(...))。您将在报告中看到每个生成的动态测试的条目,但它们不是真正的测试。您复制到问题中的引用(来自 baeldung)是错误的。它应该在 JUnit 的用户指南中阅读:动态测试生命周期@Test动态测试的执行生命周期与标准案例的执行生命周期完全不同。具体来说,单个动态测试没有生命周期回调。这意味着@BeforeEach和@AfterEach方法及其相应的扩展回调是针对@TestFactory方法执行的,而不是针对每个动态测试执行的。有关更多详细信息,请阅读:https ://junit.org/junit5/docs/current/user-guide/#writing-tests-dynamic-tests为什么有人更喜欢参数化测试而不是动态测试?如果您需要为每个测试(模板调用)提供完整的生命周期支持。如果你想在注释中声明你的参数。在此处找到有关如何以各种形式向 a 提供参数的更多详细信息@ParameterizedTest:https ://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests - 请注意“类/容器模板”计划在以后发布:https ://github.com/junit-team/junit5/issues/878我在这里写了一篇博客文章,比较了用 JUnit Jupiter 分散 3 个断言的 5 个步骤:https ://sormuras.github.io/blog/2018-05-14-junit5-scatter-assertions.html

Smart猫小萌

更大的灵活性在某种程度上意味着编写起来更加复杂和样板,尤其是在大多数情况下,测试用例是相当静态的,但不是那么动态的。考虑我想测试Math.add():参数化测试版本如下所示:@ParameterizedTest@CsvSource({ "1,1,2",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2,2,4",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "3,3,6",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "4,4,8",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5,5,10",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "6,6,12",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "7,7,14",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "10,90,100" })public void parameterizedTest(int left, int right, int expected) {&nbsp; &nbsp; assertEquals(expected, Math.addExact(left, right));}动态测试版本如下:@TestFactoryCollection<DynamicTest> dynamicTest() {&nbsp; &nbsp; return Arrays.asList(&nbsp; &nbsp; &nbsp; DynamicTest.dynamicTest("Test1", () -> assertEquals(2, Math.addExact(1, 1))),&nbsp; &nbsp; &nbsp; DynamicTest.dynamicTest("Test2", () -> assertEquals(4, Math.addExact(2, 2))),&nbsp; &nbsp; &nbsp; DynamicTest.dynamicTest("Test3", () -> assertEquals(6, Math.addExact(3, 3))),&nbsp; &nbsp; &nbsp; DynamicTest.dynamicTest("Test4", () -> assertEquals(8, Math.addExact(4, 4))),&nbsp; &nbsp; &nbsp; DynamicTest.dynamicTest("Test5", () -> assertEquals(10, Math.addExact(5, 5))),&nbsp; &nbsp; &nbsp; DynamicTest.dynamicTest("Test6", () -> assertEquals(12, Math.addExact(6, 6))),&nbsp; &nbsp; &nbsp; DynamicTest.dynamicTest("Test7", () -> assertEquals(14, Math.addExact(7, 7))),&nbsp; &nbsp; &nbsp; DynamicTest.dynamicTest("Test8", () -> assertEquals(100, Math.addExact(10, 90))));}它已经有许多样板代码。所以我尝试使用 returnStream<DynamicTest>删除这些样板代码:@TestFactoryStream<DynamicTest> dynamicTest2() {&nbsp; &nbsp; return Stream.of(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "1,1,2",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2,2,4",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "3,3,6",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "4,4,8" ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5,5,10" ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "6,6,12" ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "7,7,14",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "10,90,100")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //How to do????????&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(data-> DynamicTest.dynamicTest(data, () -> assertEquals(xxx, Math.addExact(yy,zz))));}但是如何将字符串格式的测试数据转换为参数并调用 SUT。我环顾DynamicTestAPI 看看是否有什么可以帮助我但找不到任何有用的东西,所以我放弃了.....所以,我更喜欢参数化测试。更优雅、干净、易读易写。测试用例的可读性更为重要。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java