First, what do they mean by this in the conclusion of 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.
I skimmed through JUnit 5 – Parameterized Tests and believe I understand the differences at a syntactic level, and also believe I get this:
Moreover, dynamic tests provide more flexibility regarding how the input is generated and how the tests are executed.
But then it seems like, why would anyone prefer parametrized tests over dynamic tests?
Dynamic tests, I refer to them as testlets, are just soft/grouped-assertions (assertAll(...)
) on steroids. You'll see an entry for each generated dynamic test in the reports but they are not real tests.
The quote (from baeldung) your copied into your question is wrong. It should read as in JUnit's User-Guide:
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.
For more details read: https://junit.org/junit5/docs/current/user-guide/#writing-tests-dynamic-tests
Why would anyone prefer parametrized tests over dynamic tests?
Find a lot more details on how to provide arguments in various forms to a @ParameterizedTest
here: https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests -- mind that "class/container templates" are slated for a later release: https://github.com/junit-team/junit5/issues/878
I wrote a blog post comparing 5 steps of scattering 3 assertions with JUnit Jupiter here: https://sormuras.github.io/blog/2018-05-14-junit5-scatter-assertions.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With