You need to enable JavaScript to run this app. JUnit5结合Maven Surefire时@ParameterizedTest无法并行执行如何处理
优惠活动
大模型
产品
解决方案
定价
更多

JUnit5结合Maven Surefire时@ParameterizedTest无法并行执行如何处理

问题原因&解决方案

该问题可以通过补充配置修复,不属于JUnit 5的特性限制。
你当前配置仅开启了测试类、普通@Test方法维度的并行执行。参数化测试(@ParameterizedTest)属于JUnit 5的测试模板节点,同一个测试模板下不同参数对应的单次执行属于子节点,JUnit 5默认不对同测试模板下的子节点做并行调度,所以才会出现单线程执行的情况。

修复配置

你需要在maven-surefire-plugin的configurationParameters配置块中,额外添加测试模板维度的并行参数,修改后的完整配置如下:

<plugins>
  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M4</version>
        <configuration>
            <properties>
                <configurationParameters>
                    junit.jupiter.execution.parallel.enabled = true
                    junit.jupiter.execution.parallel.mode.default = concurrent
                    <!-- 新增配置:开启测试模板下子节点并行,对@ParameterizedTest生效 -->
                    junit.jupiter.execution.parallel.mode.test.template.default = concurrent
                    <!-- 可选配置:固定并行线程数,默认是CPU核心数 -->
                    junit.jupiter.execution.parallel.config.strategy = fixed
                    junit.jupiter.execution.parallel.config.fixed.parallelism = 8
                </configurationParameters>
            </properties>
        </configuration>
    </plugin>
</plugins>

补充说明

  • 如果仅需要部分参数化测试开启并行,可以全局不添加junit.jupiter.execution.parallel.mode.test.template.default配置,而是在需要并行的参数化测试方法上添加@Execution(ExecutionMode.CONCURRENT)注解即可
  • 参数化测试并行时注意做好测试用例的线程安全处理,避免共享变量、外部资源的读写冲突

内容的提问来源于stack exchange,提问作者diego

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 17:54:10