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

如何从pom.xml先执行指定Cucumber Feature文件再并行运行其余用例

实现先单跑指定Feature再并行执行其余文件的Maven配置方案

核心思路

利用Maven Surefire插件的多execution节点配置,将测试任务拆分为两个有序执行的阶段:先单独运行指定的feature_file1,再并行执行排除该文件的剩余Feature。

配置步骤与代码示例

方案1:通过cucumber.options参数控制(无需新增Runner类)

直接在pom.xml中配置Surefire插件的两个执行阶段,通过系统变量传递Cucumber参数:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.2.5</version> <!-- 使用最新稳定版本 -->
            <executions>
                <!-- 第一阶段:单独执行feature_file1 -->
                <execution>
                    <id>run-single-feature</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <includes>
                            <include>**/*Runner.java</include> <!-- 匹配项目中所有Cucumber Runner类 -->
                        </includes>
                        <systemPropertyVariables>
                            <!-- 指定仅运行目标Feature文件 -->
                            <cucumber.options>src/test/resources/features/feature_file1.feature</cucumber.options>
                        </systemPropertyVariables>
                    </configuration>
                </execution>

                <!-- 第二阶段:并行执行剩余Feature文件 -->
                <execution>
                    <id>run-parallel-features</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <includes>
                            <include>**/*Runner.java</include>
                        </includes>
                        <!-- 开启并行模式,可选择features/methods等策略 -->
                        <parallel>features</parallel>
                        <threadCount>4</threadCount> <!-- 自定义并行线程数 -->
                        <systemPropertyVariables>
                            <!-- 排除已单独执行的Feature文件 -->
                            <cucumber.options>--exclude src/test/resources/features/feature_file1.feature</cucumber.options>
                        </systemPropertyVariables>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

方案2:通过独立Runner类区分执行(更清晰的任务拆分)

如果需要更明确的任务隔离,可以创建两个Runner类,分别对应单文件测试和并行测试:

1. 创建单文件测试Runner

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features/feature_file1.feature",
    glue = "com.yourpackage.stepdefinitions" // 替换为你的步骤定义包路径
)
public class SingleFeatureRunner {}

2. 创建并行测试Runner

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features/",
    glue = "com.yourpackage.stepdefinitions",
    exclude = "src/test/resources/features/feature_file1.feature"
)
public class ParallelFeaturesRunner {}

3. 配置pom.xml的Surefire插件

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.2.5</version>
            <executions>
                <!-- 执行单文件Runner -->
                <execution>
                    <id>run-single-feature</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <includes>
                            <include>**/SingleFeatureRunner.java</include>
                        </includes>
                    </configuration>
                </execution>

                <!-- 执行并行Runner -->
                <execution>
                    <id>run-parallel-features</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <includes>
                            <include>**/ParallelFeaturesRunner.java</include>
                        </includes>
                        <parallel>features</parallel>
                        <threadCount>4</threadCount>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

验证执行效果

执行mvn test命令后,Maven会严格按照配置顺序:先完成feature_file1的单文件测试,再启动并行任务执行剩余所有Feature文件,完全满足需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 05:11:18