[英]JUnit 5: Difference between BeforeEachCallback and BeforeTestExecutionCallback
I can't find any ressources explaining what exactly the difference between BeforeEachCallback
and BeforeTestExecutionCallback
in the JUnit Jupiter extension model is. 我找不到任何资源来解释JUnit Jupiter扩展模型中
BeforeEachCallback
和BeforeTestExecutionCallback
之间究竟有什么区别。 (I am of course also interested in the "After"-variants) (我当然也对“After” - 变量感兴趣)
To my understanding, the following timeline describes what is happening: 据我了解,以下时间表描述了正在发生的事情:
BeforeEach
- BeforeTestExecution
- Actual execution of the test
- AfterTestExecution
- AfterEach
BeforeEach
- BeforeTestExecution
- Actual execution of the test
- AfterTestExecution
- AfterEach
I suppose that BeforeTestExecution
exists so you can execute code after all the BeforeEach
callbacks have been worked on but before the actual test execution. 我认为
BeforeTestExecution
存在,所以你可以在所有BeforeEach
回调被处理之后但在实际测试执行之前执行代码。 However this is still unclear to me, because everyone could just use BeforeTestExecution
instead of BeforeEach
and the order of execution of these callbacks is random again. 但是我仍然不清楚这一点,因为每个人都可以使用
BeforeTestExecution
而不是BeforeEach
并且这些回调的执行顺序也是随机的。
So what is BeforeTestExecution
exactly for and what happens if you use this callback in multiple extensions at the same time? 那么什么是
BeforeTestExecution
,如果你同时在多个扩展中使用这个回调会发生什么?
The Javadocs ( here and here ) don't make a clear distinction between them but the JUnit5 docs include the following: Javadocs( 这里和这里 )没有明确区分它们,但JUnit5文档包括以下内容:
BeforeTestExecutionCallback
andAfterTestExecutionCallback
define the APIs for Extensions that wish to add behavior that will be executed immediately before and immediately after a test method is executed, respectively.BeforeTestExecutionCallback
和AfterTestExecutionCallback
为希望添加将在执行测试方法之前和之后立即执行的行为的Extensions定义API。 As such, these callbacks are well suited for timing, tracing, and similar use cases.因此,这些回调非常适合于计时,跟踪和类似的用例。 If you need to implement callbacks that are invoked around
@BeforeEach
and@AfterEach
methods, implementBeforeEachCallback
andAfterEachCallback
instead.如果需要实现围绕
@BeforeEach
和@AfterEach
方法调用的回调,AfterEachCallback
改为实现BeforeEachCallback
和AfterEachCallback
。
So, if you want to wrap just the test execution without any of the setup then use BeforeTestExecutionCallback
. 因此,如果您想在没有任何设置的情况下仅包装测试执行,那么请使用
BeforeTestExecutionCallback
。 The docs go on to suggest timing and logging test execution as possible use cases for BeforeTestExecutionCallback
. 文档继续建议计时和日志测试执行作为
BeforeTestExecutionCallback
可能用例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.