使用Failsafe运行Cucumber场景时测试计数不正确问题排查
问题:Cucumber场景通过Failsafe执行时测试计数不正确
使用JUnit Runner运行编写的Cucumber测试场景,通过Failsafe插件执行时出现测试计数错误:明明只运行了1个场景,测试结果却显示运行了2个测试用例。
场景代码
@QA-100 @abc @xyz Scenario: Generation of xml file Given I have generated a xml file with below details | empname| department| | Jordan | Accounts | Then I am able to see the details in the file
POM文件Build片段
<build> <plugins> <plugin> <groupId>net.masterthought</groupId> <artifactId>maven-cucumber-reporting</artifactId> <version>2.8.0</version> <executions> <execution> <id>execution</id> <phase>verify</phase> <goals> <goal>generate</goal> </goals> <configuration> <projectName>Cucumber Report</projectName> <outputDirectory>${project.build.directory}/cucumber-report-html</outputDirectory> <cucumberOutput>${project.build.directory}/cucumber.json</cucumberOutput> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${maven.surefire.version}</version> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>${maven.failsafe.version}</version> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit4</artifactId> <version>${maven.surefire.version}</version> </dependency> </dependencies> <configuration> <includes> <include>**/${test}.*</include> </includes> <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine> <systemPropertyVariables> <configFile>${config}</configFile> </systemPropertyVariables> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.version}</version> <configuration> <encoding>UTF-8</encoding> <source>${java.source.version}</source> <target>${java.target.version}</target> <fork>true</fork> <meminitial>4096m</meminitial> <maxmem>8192m</maxmem> </configuration> </plugin> </plugins> </build>
运行测试的Maven命令
mvn clean verify -Dtest=RunTest -Dconfig=local '-Dcucumber.filter.tags=@abc'
测试结果详情
Failsafe汇总XML片段
<completed>2</completed> <errors>0</errors> <failures>0</failures> <skipped>0</skipped>
控制台输出
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
使用的依赖版本
<maven.failsafe.version>2.22.2</maven.failsafe.version> <maven.surefire.version>2.22.2</maven.surefire.version> <junit.version>4.13.2</junit.version> <cucumber.version>6.11.0</cucumber.version> <maven.compiler.version>3.8.1</maven.compiler.version>
问题原因及解决方案
核心原因
这是JUnit 4版本的Cucumber Runner的固有特性:JUnit 4的测试统计机制会将Cucumber Runner类(即你的RunTest类)和实际执行的Cucumber场景分别统计为两个测试用例,最终导致计数翻倍。
解决方案
方案1:升级到JUnit 5 + Cucumber 7.x(推荐)
Cucumber 7.x及以上版本全面支持JUnit 5平台运行器,其测试计数逻辑更精准,不会出现此类重复统计问题。具体操作:
- 升级
cucumber.version到7.x(比如7.14.0) - 替换JUnit 4依赖为JUnit 5相关依赖(
cucumber-junit-platform-engine、junit-jupiter-api等) - 调整Failsafe插件配置,使用JUnit 5的提供器(
surefire-junit-platform)
方案2:调整JUnit 4环境下的Failsafe配置
如果无法升级依赖,可在Failsafe插件的<configuration>中添加以下配置,避免重复统计:
<useSystemClassLoader>false</useSystemClassLoader> <forkCount>1</forkCount> <reuseForks>false</reuseForks>
该配置会强制Failsafe使用独立的类加载器运行测试,避免Runner类被重复识别为测试用例。
方案3:检查测试类命名与Failsafe匹配规则
确保你的测试类命名符合Failsafe的默认规则(比如以IT结尾),或者调整<includes>配置为明确的类名匹配,避免意外匹配到多余的类:
<include>**/RunTest.java</include>
内容的提问来源于stack exchange,提问作者Oasis001
相关产品推荐
相关产品推荐

