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

升级Cucumber JUnit至7.11.1后,@target/rerun目录重跑失败问题

解决方案:Cucumber 7.x 失败场景重跑及报告展示问题

核心问题分析

升级到Cucumber 7.x后,原重跑逻辑失效的核心原因是:

  • Cucumber 7.x不再支持在@CucumberOptions的features参数中直接指定目录路径(如@target/rerun/),只能指定单个具体的重跑文件,或用逗号分隔多个文件路径。
  • 直接指定目录会触发Is a directory的IO异常,属于新版本的行为变更。

方案一:合并重跑文件后统一执行

通过Maven插件自动合并target/rerun下所有重跑文件到一个总文件,再让重跑类指向这个总文件,实现批量重跑所有失败场景。

步骤1:配置Maven AntRun插件

在pom.xml的build/plugins中添加以下配置,负责合并所有rerun文件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>merge-rerun-files</id>
            <phase>test</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <!-- 清空旧的合并文件,避免重复内容 -->
                    <delete file="target/rerun/merged_rerun.txt" />
                    <!-- 合并rerun目录下所有txt文件 -->
                    <concat destfile="target/rerun/merged_rerun.txt">
                        <fileset dir="target/rerun" includes="*.txt" />
                    </concat>
                </tasks>
            </configuration>
        </execution>
    </executions>
</plugin>

步骤2:修改重跑类的CucumberOptions

将重跑类的features参数指向合并后的总文件:

package com.postmedia.automation.step_definitions.runners;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        strict = true,
        dryRun = false,
        monochrome = true,
        features = "@target/rerun/merged_rerun.txt", // 指向合并后的总文件
        glue = "com.postmedia.automation",
        plugin = {
                "pretty",
                "html:target/cucumber-reports/rerun_cucumber.html", // 单独生成重跑报告,避免覆盖原报告
                "json:target/cucumber-reports/FailedScenarios.json"
        }
)
public class Test_FailedScenarios {
}

步骤3:保留原Runner类配置

原Runner类的rerun:target/rerun/SearchResultsSorting.txt这类配置无需修改,每个Runner仍生成独立的重跑文件,由AntRun插件自动合并。


方案二:解决Surefire重跑后的报告合并问题

如果倾向于使用Surefire的rerunFailingTestsCount功能,可通过升级报告插件实现首次运行和重跑结果的合并展示:

步骤1:升级maven-cucumber-reporting版本

将插件版本升级到5.7.0+(该版本支持合并多个JSON报告):

<plugin>
    <groupId>net.masterthought</groupId>
    <artifactId>maven-cucumber-reporting</artifactId>
    <version>5.7.0</version>
    <executions>
        <execution>
            <id>generate-report</id>
            <phase>verify</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <projectName>Your Project Name</projectName>
                <!-- 匹配所有JSON报告,包括首次和重跑的 -->
                <jsonFiles>
                    <param>target/cucumber-reports/*.json</param>
                </jsonFiles>
                <outputDirectory>target/cucumber-reports/final</outputDirectory>
                <!-- 开启报告合并功能 -->
                <mergeFeaturesById>true</mergeFeaturesById>
                <mergeFeaturesWithRetest>true</mergeFeaturesWithRetest>
            </configuration>
        </execution>
    </executions>
</plugin>

步骤2:调整Surefire插件配置

确保重跑时的报告不会覆盖原报告:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.1.2</version>
    <configuration>
        <rerunFailingTestsCount>1</rerunFailingTestsCount>
        <!-- 重跑时生成独立的JSON报告 -->
        <properties>
            <property>
                <name>cucumber.plugin</name>
                <value>json:target/cucumber-reports/${surefire.forkNumber}_rerun.json</value>
            </property>
        </properties>
    </configuration>
</plugin>

额外注意事项

  • 所有Runner类的html插件路径必须指定具体文件名(如html:target/cucumber-reports/search_sorting_cucumber.html),避免和目录冲突,保持你已解决第一个错误的配置。
  • 若使用方案一,可在AntRun任务中添加<mkdir dir="target/rerun" />,确保目录存在。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 15:32:02