[英]Parallel test case execution with JUnit 5
You can write parallel tests with JUnit 5.3.您可以使用 JUnit 5.3 编写并行测试。 https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution
@Execution(ExecutionMode.CONCURRENT)
class MyTest {
@Test
void test1() throws InterruptedException {
Thread.sleep(1000);
System.out.println("Test1 " + Thread.currentThread().getName());
}
@Test
void test2() throws InterruptedException {
Thread.sleep(1000);
System.out.println("Test 2! " + Thread.currentThread().getName());
}
}
// should run simultaneously
Remember to add the junit.jupiter.execution.parallel.enabled=true
to your JUnit configuration请记住将junit.jupiter.execution.parallel.enabled=true
添加到您的 JUnit 配置中
https://junit.org/junit5/docs/current/user-guide/#running-tests-config-params https://junit.org/junit5/docs/current/user-guide/#running-tests-config-params
Add this to your JUnit configuration if you need a fixed thread pool
:如果您需要fixed thread pool
,请将其添加到您的 JUnit 配置中:
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=4
跟进 - 现在可以作为 JUnit 5.3 的实验性功能使用: https : //junit.org/junit5/docs/snapshot/user-guide/index.html#writing-tests-parallel-execution
As of writing, Parallel test execution is an experimental feature in Junit5:在撰写本文时,并行测试执行是 Junit5 中的一项实验性功能:
https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-parallel-execution https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-parallel-execution
Update the junit-platform.properties
file:更新junit-platform.properties
文件:
Configuration parameters to execute top-level classes in parallel but methods in same thread:并行执行顶级类但方法在同一线程中的配置参数:
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = same_thread
junit.jupiter.execution.parallel.mode.classes.default = concurrent
Configuration parameters to execute top-level classes sequentially but their methods in parallel:依次执行顶级类但并行执行它们的方法的配置参数:
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.mode.classes.default = same_thread
The default execution mode is applied to all nodes of the test tree with a few notable exceptions, namely test classes that use the Lifecycle.PER_CLASS mode or a MethodOrderer (except for Random).默认执行模式应用于测试树的所有节点,但有一些值得注意的例外,即使用 Lifecycle.PER_CLASS 模式或 MethodOrderer(Random 除外)的测试类。 In the former case, test authors have to ensure that the test class is thread-safe;在前一种情况下,测试作者必须确保测试类是线程安全的; in the latter, concurrent execution might conflict with the configured execution order.在后者中,并发执行可能与配置的执行顺序冲突。 Thus, in both cases, test methods in such test classes are only executed concurrently if the @Execution(CONCURRENT) annotation is present on the test class or method.因此,在这两种情况下,只有在测试类或方法上存在 @Execution(CONCURRENT) 注释时,此类测试类中的测试方法才会并发执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.