ExtentReports配置setTimeStampFormat后无法切换24小时制时间戳问题
ExtentReports 24小时制时间戳配置不生效解决方案
问题表现
调用配置代码 htmlReporter.config().setTimeStampFormat("dd/MM/yyyy HH:mm:ss"); 设置24小时制时间格式后,生成的报告仍显示带AM/PM标识的12小时制格式,效果如下:
根因与解决方案
按优先级逐一排查即可解决:
- 配置时机错误
时间戳配置必须在ExtentReports实例绑定Reporter之后、所有报告内容写入(包括createTest、输出日志等操作)之前执行,先写入内容再配置会导致配置不生效。 - 方法名拼写不匹配版本
部分ExtentReports版本(尤其是5.x以上版本)的配置方法名为setTimestampFormat(s小写),而非setTimeStampFormat(S大写),方法名拼写错误会导致配置未实际生效。 - 外部配置文件覆盖
如果你项目中引入了extent-config.xml等外部配置文件,文件内的timeStampFormat配置项优先级高于代码配置,需要同步修改文件内的格式为24小时制:<configuration> <timeStampFormat>dd/MM/yyyy HH:mm:ss</timeStampFormat> </configuration> - 旧版本已知bug
5.0以下的ExtentReports版本存在时间戳配置不生效的已知问题,建议升级到5.1.1及以上稳定版后重试。
正确配置示例
// 初始化HtmlReporter ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("测试报告.html"); // 优先完成所有配置项设置 htmlReporter.config().setTimeStampFormat("dd/MM/yyyy HH:mm:ss"); // 初始化ExtentReports实例并绑定Reporter ExtentReports extent = new ExtentReports(); extent.attachReporter(htmlReporter); // 后续再执行创建测试用例、写入日志等操作 ExtentTest test = extent.createTest("测试用例1"); test.info("测试日志"); extent.flush();
内容的提问来源于stack exchange,提问作者onur sezer
相关产品推荐
相关产品推荐

