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

如何配置Java版Playwright测试:实现Surefire重跑失败用例并生成Cucumber报告

问题描述

我正在使用Playwright进行测试,希望配置maven-surefire-plugin实现失败测试用例的重跑,同时让maven-cucumber-reporting-plugin在命令行执行完所有测试后生成报告。目前我能成功生成Cucumber报告,但无法实现失败用例的重跑。

我尝试过调整POM文件中插件的顺序、配置两个插件在verify阶段执行,也试过在pluginManagement和plugins节点之间移动插件配置,但只有当maven-cucumber-reporting-plugin位于plugins节点的最后位置时,才能生成报告。以下是当前的POM配置:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>maven</executable>
                </configuration>
            </plugin>

        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>16</source>
                <target>16</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.version}</version>
            <configuration>
                <rerunFailingTestsCount>3</rerunFailingTestsCount>
            </configuration>
        </plugin>
        <plugin>
            <groupId>net.masterthought</groupId>
            <artifactId>maven-cucumber-reporting</artifactId>
            <version>5.7.5</version>
            <executions>
                <execution>
                    <id>execution</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <projectName>Supersonic Report</projectName>
                        <skip>false</skip>
                        <outputDirectory>../Reports/Supersonic/TestReports/</outputDirectory>
                        <inputDirectory>../Reports/Supersonic/JsonOutput/</inputDirectory>
                        <skipEmptyJSONFiles>true</skipEmptyJSONFiles>
                        <jsonFiles>
                            <param>**/*.json</param>
                        </jsonFiles>
                        <mergeFeaturesById>false</mergeFeaturesById>
                        <mergeFeaturesWithRetest>true</mergeFeaturesWithRetest>
                        <checkBuildResult>true</checkBuildResult>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
解决方案

核心问题是Cucumber的批量场景执行逻辑与Surefire默认重跑机制不兼容,Surefire无法直接识别单个失败的Cucumber场景,需要调整配置实现两者协同:

1. 锁定兼容的Surefire版本

使用3.0.0-M5及以上的Surefire版本,该版本开始支持Cucumber单个场景的重跑。建议显式指定版本:

<properties>
    <maven.surefire.version>3.2.5</maven.surefire.version>
</properties>

2. 配置Surefire适配Cucumber测试

在Surefire配置中开启fork模式(重跑功能的必要条件),并指定扫描Cucumber测试类:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven.surefire.version}</version>
    <configuration>
        <rerunFailingTestsCount>3</rerunFailingTestsCount>
        <!-- 开启独立fork进程执行测试,确保重跑环境干净 -->
        <forkCount>1</forkCount>
        <reuseForks>false</reuseForks>
        <!-- 指定扫描你的Cucumber运行器类 -->
        <includes>
            <include>**/*Runner.java</include>
        </includes>
        <useModulePath>true</useModulePath>
    </configuration>
</plugin>

3. 修改报告插件的终止逻辑

当前<checkBuildResult>true</checkBuildResult>会导致测试失败时Maven直接终止,无法完成重跑。需改为false,让Maven跑完所有重跑后再生成报告:

<plugin>
    <groupId>net.masterthought</groupId>
    <artifactId>maven-cucumber-reporting</artifactId>
    <version>5.7.5</version>
    <executions>
        <execution>
            <id>execution</id>
            <phase>verify</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <projectName>Supersonic Report</projectName>
                <skip>false</skip>
                <outputDirectory>../Reports/Supersonic/TestReports/</outputDirectory>
                <inputDirectory>../Reports/Supersonic/JsonOutput/</inputDirectory>
                <skipEmptyJSONFiles>true</skipEmptyJSONFiles>
                <jsonFiles>
                    <param>**/*.json</param>
                </jsonFiles>
                <mergeFeaturesById>false</mergeFeaturesById>
                <mergeFeaturesWithRetest>true</mergeFeaturesWithRetest>
                <!-- 允许Maven完成重跑后生成报告 -->
                <checkBuildResult>false</checkBuildResult>
            </configuration>
        </execution>
    </executions>
</plugin>

4. 验证执行流程

运行mvn clean verify命令,此时流程为:

  • Surefire执行所有Cucumber场景
  • 自动重跑失败场景(最多3次)
  • 所有测试完成后,报告插件在verify阶段生成包含重跑记录的完整报告

额外注意事项

  • 确保你的Cucumber运行器类(*Runner.java)中配置了JSON输出插件:@CucumberOptions(plugin = {"json:../Reports/Supersonic/JsonOutput/cucumber.json"}),保证报告插件能读取测试结果。
  • 移除或调整exec-maven-plugin中执行Maven的配置,避免干扰Surefire的测试执行流程。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 08:07:04