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

Cucumber-jvm设置固定并行数后仍为每个场景创建线程

问题分析

核心问题是Cucumber的线程池配置未生效,导致每个场景都启动新线程,加上StepDef默认每个场景实例化一次,最终每个场景都创建新的Driver实例。

解决方案

1. 修正并行执行模式配置

当前配置的是cucumber.execution.execution-mode.feature=concurrent,属于按Feature级别并行,如果12个场景分属12个Feature,会直接启动12个线程,突破设置的固定并行度5。若要按场景级别并行并复用线程池,需修改配置:

# junit-platform.properties
cucumber.execution.parallel.enabled=true
cucumber.execution.execution-mode.scenario=concurrent  # 改为场景级并行
cucumber.execution.parallel.config.strategy=fixed
cucumber.execution.parallel.config.fixed.parallelism=5

2. 确保依赖版本一致

检查pom.xml中Cucumber相关依赖版本统一为7.9.0,避免版本不匹配导致配置失效:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>7.9.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit-platform-engine</artifactId>
    <version>7.9.0</version>
    <scope>test</scope>
</dependency>

3. 调整Driver初始化逻辑

StepDef默认每个场景都会创建实例,不要在SharedDriver的构造函数中初始化Driver,改为延迟初始化+场景前置钩子获取,确保线程复用时能拿到已存在的Driver:

修改SharedDriver类

public class SharedDriver {
    public WebDriver getDriver() {
        WebDriver driver = DriverFactory.getDriver();
        if (driver == null) {
            // 仅当前线程无Driver时才创建
            driver = new ChromeDriver(); // 替换为你的Driver创建逻辑
            DriverFactory.setDriver(driver);
        }
        return driver;
    }
}

修改StepDef类

public class LoginTest {
    private final SharedDriver sharedDriver;
    private WebDriver driver;

    public LoginTest(SharedDriver sharedDriver) {
        this.sharedDriver = sharedDriver;
    }

    @Before // 场景执行前触发
    public void setupDriver() {
        this.driver = sharedDriver.getDriver();
    }
}

确保DriverFactory的ThreadLocal实现正确

public class DriverFactory {
    private static final ThreadLocal<WebDriver> DRIVER_THREAD_LOCAL = new ThreadLocal<>();

    public static WebDriver getDriver() {
        return DRIVER_THREAD_LOCAL.get();
    }

    public static void setDriver(WebDriver driver) {
        DRIVER_THREAD_LOCAL.set(driver);
    }

    // 线程结束时清理Driver,避免内存泄漏
    public static void cleanupDriver() {
        WebDriver driver = DRIVER_THREAD_LOCAL.get();
        if (driver != null) {
            driver.quit();
            DRIVER_THREAD_LOCAL.remove();
        }
    }
}

4. 禁用Maven Surefire插件的并行

避免Surefire的并行配置与Cucumber冲突,在pom.xml中设置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.2.5</version>
    <configuration>
        <parallel>none</parallel> <!-- 交由Cucumber管理并行 -->
    </configuration>
</plugin>

5. 线程结束时清理Driver

添加全局钩子,在测试套件结束时清理所有线程的Driver:

@CucumberContextConfiguration
public class TestConfig {
    @AfterAll
    public static void cleanupAllDrivers() {
        DriverFactory.cleanupDriver();
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 20:49:53