java - 使用 JUnit 5 的 @ParametrizedTest 与 @TestFactory Stream<DynamicTest> 之间的区别/优点是什么?

标签 java junit5 parametrized-testing

首先,Guide to Dynamic Tests in Junit 5 的结论中的这句话是什么意思? ?

The parameterized tests can replace many of the examples in this article. However, the dynamic tests differ from the parameterized tests as they support full test lifecycle, while parametrized tests don’t.

我浏览了一下JUnit 5 – Parameterized Tests并相信我理解语法层面上的差异,并且也相信我明白了:

Moreover, dynamic tests provide more flexibility regarding how the input is generated and how the tests are executed.

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

最佳答案

动态测试,我将它们称为 teSTLet,只是类固醇的软/分组断言 (assertAll(...))。您将在报告中看到每个生成的动态测试的条目,但它们不是真实测试。

您复制到问题中的引用(来自 baeldung)错误。它应该如 JUnit 的用户指南中所示:

Dynamic Test Lifecycle

The execution lifecycle of a dynamic test is quite different than it is for a standard @Test case. Specifically, there are no lifecycle callbacks for individual dynamic tests. This means that @BeforeEach and @AfterEach methods and their corresponding extension callbacks are executed for the @TestFactory method but not for each dynamic test.

有关更多详细信息,请阅读:https://junit.org/junit5/docs/current/user-guide/#writing-tests-dynamic-tests

Why would anyone prefer parametrized tests over dynamic tests?

  1. 如果您需要每个测试的完整生命周期支持(模板调用)。
  2. 如果您想在注释中声明您的参数。

在此处查找有关如何以各种形式向 @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 enter image description here

关于java - 使用 JUnit 5 的 @ParametrizedTest 与 @TestFactory Stream<DynamicTest> 之间的区别/优点是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54922345/

相关文章:

java - Selenium Webdriver - Chrome - 切换窗口并返回 - 无法从渲染器接收消息

java - 我在哪里可以找到 JOSSO 数据库配置文件?

java - 从 Set 中删除负值

java - JUnit 5,如何从 BeforeEach 回调中获取参数化测试参数?

c++ - GTest 参数化测试从数组或类似文件中解压值参数

java - Java中字符串定界程序中的差一错误

java - 使用 Mockito 2 模拟服务导致 stub 错误

java - Maven 未运行 JUnit 5 测试

java - 如何使用 CsvFileSource 在 Junit 5 参数化测试中转义双引号

java - 使用 @MethodSource 和返回 ArrayList 的方法进行参数化 JUnit 测试