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

Maven执行Cucumber测试时运行数为0,IntelliJ可正常运行如何解决

Cucumber Maven执行测试用例数为0解决方案

核心原因

当前配置中同时存在基于JUnit4实现的cucumber-junit执行器、以及JUnit Jupiter(JUnit5)依赖,Maven Surefire 3.x版本默认优先识别JUnit5引擎,无对应适配组件时会忽略JUnit4风格的Cucumber测试,同时也可能存在测试类路径匹配规则不正确的问题。


修复方案

方案1:补全JUnit Vintage引擎适配JUnit4风格用例

通过引入JUnit Vintage引擎让Surefire可以同时识别JUnit4、JUnit5体系的测试用例:

  • 在pom.xml的dependencies节点新增依赖:
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.9.2</version>
    <scope>test</scope>
</dependency>

方案2:升级为JUnit5体系的Cucumber执行器(推荐)

替换旧版本执行器,完全适配JUnit5技术栈:

  • 移除原有cucumber-junit依赖
  • 新增JUnit平台适配的Cucumber引擎依赖:
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit-platform-engine</artifactId>
    <version>${cucumber.version}</version>
    <scope>test</scope>
</dependency>
  • 调整CucumberE2ETest.java的注解为JUnit5风格,补充@Suite、@SelectClasspathResource等Cucumber对应注解即可。

方案3:修正Surefire的测试类匹配规则

当前配置的<include>CucumberE2ETest.java</include>仅能匹配根测试目录下的测试类,如果测试类放在子包中会无法识别,调整规则如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M5</version>
    <configuration>
        <includes>
            <include>**/CucumberE2ETest.java</include>
        </includes>
    </configuration>
</plugin>

如果以上方案都无法解决,可执行mvn test -X查看详细的测试类扫描日志,确认Cucumber测试类是否被Surefire正常识别。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 16:15:05