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

Cucumber 7.11.2(JUnit5):如何通过CLI运行单个场景/特性?

Cucumber 7.11.2(JUnit5)CLI运行单个测试问题解决方案

Cucumber 7.x版本已弃用旧版的cucumber.options参数,同时需确保pom.xml配置适配JUnit5的最新绑定规则,以下是具体解决步骤:

正确的CLI命令格式

针对Cucumber 7.x,运行单个特性文件的有效命令为:

mvn test -Dcucumber.features="classpath:features/file.feature"

若要指定单个场景,可追加场景名称过滤参数:

mvn test -Dcucumber.features="classpath:features/file.feature" -Dcucumber.filter.name="你的场景精确名称"

注:使用classpath:前缀能避免项目结构差异导致的路径识别问题,比直接写相对路径更可靠。

校验pom.xml配置

确保pom.xml包含正确的依赖和插件配置:

核心依赖

<dependencies>
    <!-- Cucumber-JUnit5 绑定依赖 -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit-platform-engine</artifactId>
        <version>7.11.2</version>
        <scope>test</scope>
    </dependency>
    <!-- JUnit5 核心引擎依赖 -->
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite</artifactId>
        <version>1.9.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.9.3</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Maven构建插件配置

需确保maven-surefire-plugin版本支持JUnit5和Cucumber参数传递:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.1.2</version>
            <configuration>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

另外,项目中需要一个JUnit5测试启动类,示例如下:

import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME;

@Suite
@IncludeEngines("cucumber")
@SelectPackages("你的测试代码根包路径")
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty")
public class CucumberTestSuite {
}

VSCode运行问题排查

若VSCode无法运行单个特性文件,可按以下步骤检查:

  • 安装最新版的「Cucumber (Gherkin) Full Support」插件
  • 在VSCode设置中,配置Cucumber的特性文件路径和步骤定义路径,确保插件能正确关联两者
  • 直接在VSCode终端执行上述正确的CLI命令,验证是否为插件配置问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 13:03:31