如何将Cucumber默认报告存储至带时间戳的文件夹以便后续查看?
解决Cucumber-Java报告动态存储到时间戳文件夹的问题
方案一:动态指定报告输出路径(推荐,避免复制操作)
由于@CucumberOptions的参数属于编译期常量,无法直接使用动态路径,因此可以通过编程式启动Cucumber的方式实现:
- 编写自定义Cucumber运行主类,在启动前生成动态文件夹并构建插件参数:
import io.cucumber.core.cli.Main; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class DynamicReportCucumberRunner { public static void main(String[] args) { // 生成带时间戳的报告文件夹名 SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy-HH-mm-ss", Locale.ENGLISH); String timestamp = dateFormat.format(new Date()); String reportBaseDir = "target/cucumber-reports/" + timestamp; // 创建文件夹(自动创建父目录) new File(reportBaseDir).mkdirs(); // 构建Cucumber运行参数 String[] cucumberArgs = { "--features", "src/test/resources/featureFiles", "--glue", "stepDefinations", "--glue", "hooks", "--plugin", "pretty", "--plugin", "html:" + reportBaseDir + "/cucumber-report.html", "--plugin", "json:" + reportBaseDir + "/cucumber-report.json", "--tags", "@latest", "--monochrome" }; // 启动Cucumber测试 Main.run(cucumberArgs, Thread.currentThread().getContextClassLoader()); } }
- 修改Maven的
pom.xml,配置该主类作为测试入口:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.5</version> <configuration> <mainClass>your.package.DynamicReportCucumberRunner</mainClass> </configuration> </plugin> </plugins> </build>
执行mvn test后,Cucumber会直接将报告生成到动态创建的时间戳文件夹中,无需后续复制。
方案二:修复@AfterAll复制报告的问题(保留@RunWith方式)
如果坚持使用原有@RunWith配置,需要完整复制Cucumber生成的所有报告文件(HTML报告依赖配套的CSS/JS资源,并非单一文件):
- 在Hooks类中保存动态生成的目标文件夹路径,并实现目录复制逻辑:
import io.cucumber.java.AfterAll; import io.cucumber.java.BeforeAll; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; public class Hooks { private static String targetReportDir; @BeforeAll public static void setupReportDir() { // 生成动态时间戳文件夹 SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy-HH-mm-ss", Locale.ENGLISH); String timestamp = dateFormat.format(new Date()); targetReportDir = "target/cucumber-reports/" + timestamp; new File(targetReportDir).mkdirs(); } @AfterAll public static void copyReports() { // 源报告路径(对应@CucumberOptions中的配置) File sourceHtmlDir = new File("target/cucumber-reports/cucumber.html"); File sourceJsonFile = new File("target/cucumber-reports/cucumber.json"); try { // 复制HTML报告目录(包含所有资源文件) if (sourceHtmlDir.exists()) { copyDirectory(sourceHtmlDir.toPath(), new File(targetReportDir + "/cucumber.html").toPath()); } // 复制JSON报告文件 if (sourceJsonFile.exists()) { Files.copy(sourceJsonFile.toPath(), new File(targetReportDir + "/cucumber.json").toPath(), StandardCopyOption.REPLACE_EXISTING); } } catch (Exception e) { e.printStackTrace(); } } // 递归复制目录的工具方法 private static void copyDirectory(Path source, Path target) throws Exception { Files.walk(source).forEach(sourcePath -> { Path targetPath = target.resolve(source.relativize(sourcePath)); try { Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }); } }
注意:不同版本的Cucumber生成的HTML报告结构可能不同,需根据实际生成的文件结构调整源路径。
内容的提问来源于stack exchange,提问作者Tapas Mahato
相关产品推荐
相关产品推荐

