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

如何实现CucumberJVM测试失败后自动重跑并生成单一报告?

Cucumber+Maven实现失败用例自动重跑2次并生成合并报告

核心思路

无需配置多个TestRunner,通过Maven多执行阶段实现流程闭环:

  • 第一阶段:运行全量测试用例,记录失败用例到failedrun.txt,生成初始测试结果JSON
  • 第二/第三阶段:仅运行failedrun.txt中的失败用例,生成独立的重跑结果JSON(支持2次重跑)
  • 最终阶段:用Cucumber报告插件合并所有JSON文件,生成一份包含完整执行流程的测试报告

步骤1:调整TestRunner配置

修改@CucumberOptions,避免报告文件被覆盖,保留rerun功能:

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/resources/features",
        glue = "stepDefinitions",
        monochrome = true,
        tags = "@cronjob-version",
        plugin = {
                "pretty",
                // 初始运行生成带标识的JSON,防止被重跑覆盖
                "json:target/CucumberTestReports/CucumberTestReport-initial.json",
                "rerun:target/failedrun.txt"
        }
)
public class TestRunner {
}

注:暂时移除HTML/JUnit报告的直接生成,统一由合并插件生成最终报告


步骤2:配置Maven Surefire插件实现2次重跑

在pom.xml中添加多执行目标,分别处理全量测试、第一次重跑、第二次重跑:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.1.2</version>
    <executions>
        <!-- 执行1:运行全部测试用例 -->
        <execution>
            <id>run-all-tests</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <includes>
                    <include>testRunners/TestRunner.java</include>
                </includes>
                <testFailureIgnore>true</testFailureIgnore>
            </configuration>
        </execution>
        <!-- 执行2:第一次重跑失败用例 -->
        <execution>
            <id>run-failed-tests-first</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <includes>
                    <include>testRunners/TestRunner.java</include>
                </includes>
                <systemPropertyVariables>
                    <cucumber.options>--plugin json:target/CucumberTestReports/CucumberTestReport-rerun1.json @target/failedrun.txt</cucumber.options>
                </systemPropertyVariables>
                <testFailureIgnore>true</testFailureIgnore>
            </configuration>
        </execution>
        <!-- 执行3:第二次重跑失败用例 -->
        <execution>
            <id>run-failed-tests-second</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <includes>
                    <include>testRunners/TestRunner.java</include>
                </includes>
                <systemPropertyVariables>
                    <cucumber.options>--plugin json:target/CucumberTestReports/CucumberTestReport-rerun2.json @target/failedrun.txt</cucumber.options>
                </systemPropertyVariables>
                <testFailureIgnore>true</testFailureIgnore>
            </configuration>
        </execution>
    </executions>
</plugin>

步骤3:配置Cucumber报告插件合并生成最终报告

添加maven-cucumber-reporting插件,自动合并所有JSON结果:

<plugin>
    <groupId>net.masterthought</groupId>
    <artifactId>maven-cucumber-reporting</artifactId>
    <version>5.7.0</version>
    <executions>
        <execution>
            <id>generate-final-report</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <jsonDirectory>target/CucumberTestReports</jsonDirectory>
                <outputDirectory>target/CucumberFinalReport</outputDirectory>
                <projectName>自动化测试合并报告</projectName>
                <!-- 自动合并重跑用例的结果 -->
                <mergeFeaturesWithRetest>true</mergeFeaturesWithRetest>
            </configuration>
        </execution>
    </executions>
</plugin>

执行命令

运行以下Maven命令即可完成全流程:

mvn clean test post-integration-test

执行流程:

  1. 清理历史构建文件
  2. 运行全量测试 → 生成初始结果+失败用例清单
  3. 第一次重跑失败用例 → 生成重跑1结果
  4. 第二次重跑失败用例 → 生成重跑2结果
  5. 合并所有JSON → 生成最终HTML报告(路径:target/CucumberFinalReport/index.html)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 06:36:01