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

Maven执行Cucumber测试始终返回0,失败仍显示BUILD SUCCESS求助

问题分析与解决方案

兄弟,我看你遇到的问题确实挺闹心:执行mvn clean test时Cucumber测试始终显示0用例执行,就算测试步骤明摆着失败,Maven还返回BUILD SUCCESS。结合你贴出来的pom.xml和Runner代码,我整理了几个核心排查点和解决办法:

1. 先解决依赖冲突问题

你在pom里同时引入了cucumber-junit和cucumber-testng,但你的Runner用的是@RunWith(Cucumber.class)——这是JUnit风格的Runner。两种测试框架的Cucumber适配器混在一起,很可能导致Surefire插件搞不清该用哪种逻辑去扫描和执行测试,直接导致测试被“忽略”。

  • 解决办法:移除cucumber-testng依赖,只保留cucumber-junit就行,毕竟你的Runner是基于JUnit的。

2. 确认Surefire插件能找到你的Runner

首先得保证MainRunner.java放在src/test/java目录下,而且包路径能被<include>**/*Runner.java</include>这个规则匹配到。如果Runner不在测试源码目录里,Surefire根本找不到它。

  • 另外,你可以给Surefire加个强制fork的配置,避免Maven缓存导致的奇怪问题:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20.1</version>
    <configuration>
        <includes>
            <include>**/*Runner.java</include>
        </includes>
        <forkCount>1</forkCount>
        <reuseForks>false</reuseForks>
    </configuration>
</plugin>

3. 检查标签过滤是不是误杀了所有测试

你的Runner里标签配置是~@ignore,意思是排除带@ignore的用例。但你得仔细看看所有feature文件,是不是所有场景都被打上了@ignore标签?如果是,那自然会显示0测试执行。

  • 临时把标签改成@(执行所有用例)试试,看能不能跑起来:
@CucumberOptions(features = "src/test/resources/features", tags = { "@" }, ...)

4. 老旧版本的兼容性坑

你用的Cucumber 1.2.5版本太老了,和Surefire 2.20.1可能存在兼容性问题。建议要么升级Cucumber到较新的稳定版本(比如4.x系列,注意1.x到4.x包名从info.cukes改成io.cucumber),要么把Surefire版本降到2.19.1试试。

  • 举个升级Cucumber的示例(注意包名变化):
<properties>
    <cucumber.version>4.8.1</cucumber.version>
</properties>
<dependencies>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>${cucumber.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>${cucumber.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- 其他依赖对应调整包名和版本 -->
</dependencies>

5. 去测试报告里找线索

执行完mvn clean test后,去target/cucumber-reports目录打开HTML报告看看,里面会有详细日志:比如是不是找不到feature文件?或者step定义没匹配上?这些信息能帮你快速定位根本原因。

建议先从依赖冲突和标签过滤这两个最常见的问题入手排查,应该能很快解决问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:08:44