首先,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?
- 如果您需要每个测试的完整生命周期支持(模板调用)。
- 如果您想在注释中声明您的参数。
在此处查找有关如何以各种形式向 @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
关于java - 使用 JUnit 5 的 @ParametrizedTest 与 @TestFactory Stream<DynamicTest> 之间的区别/优点是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54922345/