运行 JUnit5 并行测试,但希望保留一些顺序测试

Running JUnit5 Test in Parallel But Want To Leave Some Tests Sequential

我有一个项目,我们有很多 Junit 测试。我们刚刚完成了从 JUnit4 到 JUnit5 的大规模迁移。我们希望 运行 并行进行大部分测试,但有一些测试需要 运行 顺序进行。有什么方法可以同时使用 JUnit5 和 运行 测试吗?

我问的原因是我有 4 个测试将数据库加载到内存中,并且我正在将数据加载到该数据库中。然后我 运行 对该数据库进行测试。这些是我需要按顺序 运行 而不能并行 运行 的四个测试。

您很可能想在测试中使用 @ResourceLock 注释 https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-parallel-execution-synchronization

根据Parallel Test Execution and Single Thread Execution

Since of maven-surefire-plugin:2.18, you can apply the JCIP annotation @net.jcip.annotations.NotThreadSafe on the Java class of JUnit test (pure test class, Suite, Parameterized, etc.) in order to execute it in single Thread instance. The Thread has name maven-surefire-plugin@NotThreadSafe and it is executed at the end of the test run.

所以你可以用 @NotThreadSafe 注释你的测试 类 以使它们在同一个线程上执行(名为 maven-surefire-plugin@NotThreadSafe).

  <dependency>
    <groupId>com.github.stephenc.jcip</groupId>
    <artifactId>jcip-annotations</artifactId>
    <version>1.0-1</version>
    <scope>test</scope>
  </dependency>

你可以看看 @Isolated 注释 https://junit.org/junit5/docs/5.7.1/api/org.junit.jupiter.api/org/junit/jupiter/api/parallel/Isolated.html.

保证test-class不会运行与其他类并行。