如何在使用maven-failsafe-plugin重跑失败用例前清理Cucumber report.json?
我之前也碰到过一模一样的情况——用cucumber-jvm-parallel-plugin配合maven-failsafe-plugin的rerunFailingTestsCount配置重跑失败用例时,生成的1.json、2.json这类重跑报告是空文件,直接导致Jenkins的Cucumber Reports Plugin解析失败抛出ValidationException。下面是我亲测有效的解决方案:
1. 升级插件版本,修复已知bug
cucumber-jvm-parallel-plugin的旧版本(比如低于5.0.0)存在重跑报告生成的逻辑缺陷,会创建空文件但不填充测试结果。先把插件升级到最新稳定版,同时确保maven-failsafe-plugin也使用较新的版本(比如3.0.0+):
<!-- 升级cucumber-jvm-parallel-plugin --> <plugin> <groupId>com.github.temyers</groupId> <artifactId>cucumber-jvm-parallel-plugin</artifactId> <version>5.0.0</version> <!-- 其他配置保持不变 --> </plugin> <!-- 升级maven-failsafe-plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.0.0-M7</version> <!-- 其他配置保持不变 --> </plugin>
2. 统一报告输出目录,确保重跑结果写入正确路径
默认情况下,重跑的报告可能会输出到单独的目录,和并行插件的主报告目录不匹配,导致生成空文件。需要在两个插件的配置中统一报告输出路径,并且开启并行插件的重跑支持:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.0.0-M7</version> <configuration> <rerunFailingTestsCount>${rerunFailingTestsCount}</rerunFailingTestsCount> <!-- 指定重跑报告和主报告输出到同一目录 --> <rerunReportsDirectory>${project.build.directory}/cucumber-reports</rerunReportsDirectory> <reportsDirectory>${project.build.directory}/cucumber-reports</reportsDirectory> <!-- 传递系统参数,让Cucumber把报告写到指定目录 --> <argLine> -Dcucumber.reporting.output.dir=${project.build.directory}/cucumber-reports -Dcucumber.parallel.reporting.dir=${project.build.directory}/cucumber-reports </argLine> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>com.github.temyers</groupId> <artifactId>cucumber-jvm-parallel-plugin</artifactId> <version>5.0.0</version> <executions> <execution> <id>generateRunners</id> <phase>generate-test-sources</phase> <goals> <goal>generateRunners</goal> </goals> <configuration> <glue>your.step.definitions.package</glue> <!-- 替换成你的步骤定义包 --> <featuresDirectory>src/test/resources/features</featuresDirectory> <outputDirectory>target/generated-test-sources/cucumber</outputDirectory> <parallelScheme>SCENARIO</parallelScheme> <format>json</format> <jsonOutputDirectory>${project.build.directory}/cucumber-reports</jsonOutputDirectory> <rerunFailedScenarios>true</rerunFailedScenarios> <!-- 开启重跑失败场景支持 --> </configuration> </execution> </executions> </plugin>
3. 预处理删除空JSON文件(临时 workaround)
如果升级和配置调整后还是有问题,可以在Jenkins构建的Cucumber Reports Plugin步骤之前添加一个预处理步骤,删除目录下的空JSON文件,避免插件解析空文件报错:
Linux/macOS 用Shell脚本:
find ${WORKSPACE}/target/cucumber-reports -name "*.json" -size 0 -delete
Windows 用批处理脚本:
for /f "delims=" %%f in ('dir /b /s ${WORKSPACE}\target\cucumber-reports\*.json') do if %%~zf equ 0 del "%%f"
4. 替代方案:改用Cucumber原生并行支持
cucumber-jvm-parallel-plugin已经不再维护,Cucumber 4.x及以上版本已经支持原生并行测试,重跑报告的生成逻辑更稳定。可以考虑替换掉cucumber-jvm-parallel-plugin,改用原生并行配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.0.0-M7</version> <configuration> <rerunFailingTestsCount>${rerunFailingTestsCount}</rerunFailingTestsCount> <!-- 开启方法级并行 --> <parallel>methods</parallel> <threadCount>4</threadCount> <!-- 根据你的服务器配置调整线程数 --> <argLine> -Dcucumber.execution.parallel.enabled=true -Dcucumber.execution.parallel.config.strategy=fixed -Dcucumber.execution.parallel.config.fixed.parallelism=4 -Dcucumber.reporting.output.dir=${project.build.directory}/cucumber-reports </argLine> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin>
这个方案不需要额外的并行插件,直接用Cucumber原生功能,重跑时的报告生成不会出现空文件问题。
内容的提问来源于stack exchange,提问作者Dmitry K

