You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于Gradle实现Cucumber Scenario Outline用例并行执行?

如何通过Gradle实现Cucumber Scenario Outline中Examples用例的并行执行?

问题描述

我查阅了大量资料,都是关于Cucumber与Maven结合实现并行测试的内容,但没找到Gradle相关方案。现有如下测试场景示例:

Scenario Outline: Author from API matches author on book page
  Given user goes on google.com
  When user search <item>
  And click on <locator>
  Then something happens

  Examples:
    | item    | locator |
    | item1   | locator1|
    | item2   | locator2|
    | item3   | locator3|

请问是否可以通过Gradle实现该Scenario Outline中Examples表格每条数据对应的用例并行执行?

解决方案

可以实现,核心是结合Cucumber的并行测试能力与Gradle的任务配置来完成,具体步骤如下:

  • 配置Cucumber依赖
    确保build.gradle中引入支持并行的Cucumber版本及JUnit 5适配依赖:
dependencies {
    testImplementation 'io.cucumber:cucumber-java:7.14.0'
    testImplementation 'io.cucumber:cucumber-junit-platform-engine:7.14.0'
    testImplementation 'org.junit.platform:junit-platform-launcher:1.10.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
}
  • 启用并行测试配置
    在build.gradle的test任务中添加Cucumber并行执行参数,指定按Examples行拆分任务:
test {
    useJUnitPlatform()
    // 开启Cucumber并行执行
    systemProperty 'cucumber.execution.parallel.enabled', 'true'
    // 设置并行策略为固定线程数,也可改为dynamic适配CPU核心数
    systemProperty 'cucumber.execution.parallel.config.strategy', 'fixed'
    systemProperty 'cucumber.execution.parallel.config.fixed.parallelism', '3'
    // 关键:按Scenario Outline的Examples行拆分测试实例
    systemProperty 'cucumber.execution.split.by', 'examples'
}
  • 关键参数说明

    • cucumber.execution.parallel.enabled: 全局开启Cucumber并行执行
    • cucumber.execution.split.by: 设置为examples时,会将每个Examples行对应的测试实例拆分为独立任务,实现单Outline下多实例并行
    • cucumber.execution.parallel.config.fixed.parallelism: 固定并行线程数,可根据机器性能调整
  • 验证执行效果
    运行./gradlew test命令后,查看测试日志会看到不同Examples实例分配到不同线程执行,测试总耗时会显著降低。

⚠️ 注意:如果测试用例依赖共享资源(如同一浏览器实例),需为每个测试实例提供独立资源,避免并发冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 22:10:42