使用 DynamicNode 并在所有测试完成后需要生命周期挂钩 运行

Using DynamicNode and need a lifecycle hook to run after all tests have completed

我在动态生成测试并执行它们的框架中使用 DynamicNode 非常成功。

现在我需要在所有 DynamicNode 集合执行后执行一些代码。这可能意味着我有一个 JUnit5 class 和多个 return Iterable<DynamicNode> 的方法,但我想 运行 只有在所有测试方法完成后才做一些事情。

有没有办法自动执行此操作?

编辑:理想情况下,我希望我的框架能够注入自动执行的代码,而无需用户在方法上添加 @AfterAll 注释并编写一些额外的代码。

每个用 @TestFactory 注释的方法都参与默认生命周期。这意味着在您的情况下,一个 @AfterAll 注释方法应该可以解决问题。

@AfterAll

Denotes that the annotated method should be executed after all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class; analogous to JUnit 4’s @AfterClass. Such methods are inherited (unless they are hidden or overridden) and must be static (unless the "per-class" test instance lifecycle is used).

复制自https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations