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

Maven测试运行结果与实际执行场景数不符问题求助

问题描述

执行mvn clean test运行feature文件中的4个测试场景时,Cucumber实际执行结果为:

  • Run1、Run3 执行通过
  • Run2、Run4 因NoSuchIdentifiableElement错误(找不到fileExplorerButton元素)失败

但Maven最终统计结果显示:Tests run:1, Failures:1,与实际场景执行数量不匹配。

相关配置如下:

Runner类

@CucumberOptions(
 features = "classpath:cucumber-features/someTest.feature",
 glue = { "automatedtest.tests.steps", "autotest.arch.steps" },
 plugin = {
 "pretty",
 "html:target/cucumber-html-report/",
 "json:target/cucumber-json-report/report.json",
 "io.qameta.allure.cucumber6jvm.AllureCucumber6Jvm"
 }
)
public class MainRunner extends AbstractTestNGCucumberTests {}

父POM

<dependencyManagement>    
<dependencies>        
<dependency>           
 <groupId>org.springframework</groupId>           
 <artifactId>spring-framework-bom</artifactId>           
 <version>${spring.version}</version>           
 <type>pom</type>           
 <scope>import</scope>       
 </dependency> 
 </dependencies></dependencyManagement>
 <build>    
 <pluginManagement>        
 <plugins>            
 <plugin>                
 <groupId>org.apache.maven.plugins</groupId>                
 <artifactId>maven-compiler-plugin</artifactId>              
 <configuration>                  
 <source>1.8</source>                    
 <target>1.8</target>               
 </configuration>          
 </plugin>            
 <plugin>                
 <groupId>org.apache.maven.plugins</groupId>            
 <artifactId>maven-surefire-plugin</artifactId>         
 <version>3.0.0-M3</version>    
 <configuration>    
 <includes>    
 <include>**/MainRunner.class</include>    
 </includes>      
 <printSummary>true</printSummary>  
 </configuration>  
 </plugin> 
 </plugins>  
 </pluginManagement>
 </build>

src/test下的POM

<plugin>   
 <groupId>org.apache.maven.plugins</groupId>  
 <artifactId>maven-surefire-plugin</artifactId> 
 <version>3.0.0-M3</version>
 <configuration>  
 <includes>    
 <include>**/MainRunner.class</include>   
 </includes> 
 </configuration>
 </plugin>

已尝试添加<forkCount>、<printSummary>参数,问题仍未解决。


原因分析

Maven Surefire插件与Cucumber的统计维度完全不同:

  • Surefire统计的是TestNG测试类/测试方法的数量,你配置的MainRunner是一个TestNG测试类,因此Surefire将其算作1个测试,不管内部包含多少Cucumber场景。
  • Run1-Run4是Cucumber层面的测试场景,属于TestNG测试类内部的子执行单元,不会被Surefire单独统计。
  • 你使用的Surefire版本是3.0.0-M3(里程碑预览版),可能存在兼容性或计数逻辑的bug,进一步放大了这个差异。

解决方法

1. 聚焦准确的测试报告

不用纠结Maven的统计数字,Cucumber生成的专用报告才是场景执行结果的权威来源:

  • 查看target/cucumber-html-report/下的HTML报告,可直观看到每个场景的执行状态
  • 执行allure serve target/allure-results生成Allure可视化报告,查看详细执行日志
  • 解析target/cucumber-json-report/report.json的结构化数据,用于自动化结果分析

2. 改用Cucumber Maven插件实现场景级统计

如果需要让Maven统计结果与Cucumber场景数一致,替换Surefire为cucumber-maven-plugin:
在src/test的POM中添加如下配置:

<plugin>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-maven-plugin</artifactId>
    <version>6.11.0</version> <!-- 与你使用的Cucumber6版本匹配 -->
    <executions>
        <execution>
            <id>run-tests</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <features>classpath:cucumber-features/someTest.feature</features>
        <glue>
            <package>automatedtest.tests.steps</package>
            <package>autotest.arch.steps</package>
        </glue>
        <plugin>
            <name>pretty</name>
            <name>html:target/cucumber-html-report/</name>
            <name>json:target/cucumber-json-report/report.json</name>
            <name>io.qameta.allure.cucumber6jvm.AllureCucumber6Jvm</name>
        </plugin>
    </configuration>
</plugin>

同时移除Surefire插件中对MainRunner的配置,这样Maven会直接执行Cucumber场景,统计数将与实际场景数量完全一致。

3. 升级Surefire到稳定版本

将Surefire版本从3.0.0-M3升级到最新稳定版(如3.2.5),修复预览版可能存在的bug:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.2.5</version>
    <configuration>
        <includes>
            <include>**/MainRunner.class</include>
        </includes>
        <printSummary>true</printSummary>
    </configuration>
</plugin>

4. 解决元素找不到的附带问题

针对Run2、Run4的NoSuchIdentifiableElement错误,可尝试:

  • 优化元素定位策略,避免使用易变的绝对路径XPath,改用ID、name或稳定的相对路径
  • 添加显式等待,确保元素加载完成后再执行操作:
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    WebElement fileExplorerButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("fileExplorerButton")));
    
  • 检查场景间的状态隔离,避免前一个场景修改页面状态导致后一个场景元素定位失败

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 11:01:29