[英]Junit 4 execution order vs Junit 5
I am seeing some unwanted behaviour using Junit 5. I have the following structure: 我看到使用Junit 5的一些不需要的行为。我有以下结构:
website
config
BaseTest.java
tests
package a
Test 1
Test 2
package b
Test 3
Test 4
BaseTest
contains an @BeforeAll
with some one-time setup and an @BeforeEach
and @AfterEach
to setup and teardown some data before and after each test. BaseTest
含有@BeforeAll
一些一次性设置和@BeforeEach
和@AfterEach
设置和在每次试验后拆毁一些数据。 Each test extends from BaseTest
. 每个测试都从BaseTest
扩展。
The way I am used to with Junit 4 is that the @BeforeAll
in BaseTest
is only run once when executing all the tests in all the packages at once. 我习惯使用Junit 4的方式是, @BeforeAll
中的BaseTest
只在一次执行所有包中的所有测试时运行一次。 However, with Junit 5 it seems that the @BeforeAll
is repeated when a test from another package is run. 但是,使用Junit 5时,似乎在运行另一个包的测试时会重复@BeforeAll
。 To clarify, I get something like this: 为了澄清,我得到这样的东西:
BeforeAll
BeforeEach
Package 1 Test 1
AfterEach
BeforeEach
Package 1 Test 2
AfterEach
AfterAll
BeforeAll
BeforeEach
Package 2 Test 1
AfterEach
BeforeEach
Package 2 Test 2
AfterEach
AfterAll
Thanks in advance! 提前致谢! Regards 问候
The way I am used to with Junit 4 is that the
@BeforeAll
inBaseTest
is only run once when executing all the tests in all the packages at once. 我习惯使用Junit 4的方式是,@BeforeAll
中的BaseTest
只在一次执行所有包中的所有测试时运行一次。
That is incorrect. 那是不对的。
I recreated your example using JUnit 4 annotations, and the following is the result: 我使用JUnit 4注释重新创建了您的示例,结果如下:
BeforeClass
Before
Package 1 Test 1
After
Before
Package 1 Test 2
After
AfterClass
BeforeClass
Before
Package 2 Test 1
After
Before
Package 2 Test 2
After
AfterClass
Thus, the behavior in question is identical in JUnit 4 and JUnit Jupiter. 因此,有问题的行为在JUnit 4和JUnit Jupiter中是相同的。
I managed to fix it by overriding the BeforeAllCallback
and registering it with my BaseTest
like so: 我设法通过覆盖BeforeAllCallback
并使用我的BaseTest
注册它来修复它,如下所示:
JunitExtensions.class
import com.codeborne.selenide.Configuration;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
public class JunitExtensions implements BeforeAllCallback {
@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
//Code that needs to be ran before all tests goes here
}
}
Basetest.java
@ExtendWith(JunitExtensions.class)
public class BaseTest {
//code goes here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.