问题描述
我在一个动态生成测试并执行它们的框架中非常成功地使用了DynamicNode
.
I'm using DynamicNode
very successfully in a framework that dynamically generates tests and executes them.
现在,在所有DynamicNode
集合执行之后,我需要执行一些代码.这可能意味着我只有一个JUnit5类,其中包含多个返回Iterable<DynamicNode>
的方法,但是我只想在所有测试方法都完成后才运行某些东西.
Now I have a need to execute some code after all DynamicNode
collections have executed. This can mean that I have a single JUnit5 class with multiple methods that return Iterable<DynamicNode>
, but I want to run something only after all the test methods have completed.
有没有一种方法可以自动执行此操作?
Is there a way to do this automatically ?
理想情况下,我希望我的框架注入要自动执行的代码,而无需用户在方法上添加@AfterAll
批注并编写一些额外的代码.
ideally I would like my framework to inject the code to be executed automatically, without the user needing to add a @AfterAll
annotation on a method and write some extra code.
推荐答案
用@TestFactory
注释的每个方法都参与默认的生命周期.这意味着在您的情况下,应使用@AfterAll
带注释的方法来解决问题.
Each method that is annotated with @TestFactory
takes part in the default lifecycle. That means in your case an @AfterAll
annotated method should do the trick.
表示带注释的方法毕竟应该执行 @Test
,@RepeatedTest
,@ParameterizedTest
和@TestFactory
当前类中的方法;类似于JUnit 4的@AfterClass
. 此类方法是继承的(除非它们被隐藏或覆盖),并且 必须是静态的(除非每类"测试实例生命周期是 使用).
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
这篇关于使用DynamicNode并需要生命周期挂钩才能在所有测试完成后运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!