【发布时间】:2019-06-06 15:06:35
【问题描述】:
我正在使用 Extent Report 库(版本 4)将 html-reports 集成到我们的测试框架中。
为此,我编写了一个包装器,它封装了我们的默认 TestNG 记录器(用于控制台输出)和 ExtentReport 记录器(ExtentTest.log,用于收集 html 报告的数据)。 我们使用 TestNg 作为我们的测试框架。
在从失败的断言(软断言和硬断言)中捕获日志消息以将其显示在 html-report 中时遇到问题,它们只会转到控制台日志。 捕获这些 Assert 日志消息以在 html-report 中显示它们的可能解决方案是什么?
我无法扩展 Assert(或 SoftAssert)类并添加自己的实现(通过在其中添加 ExtentTest.log 的实例),因为我必须替换数十个测试中的所有断言。
public class Loggers {
private static final Loggers instance = new Loggers();
private static ExtentTest test;
private static ExtentReports extent;
private static Logger LOG;
private static final String REPORT_LOCATION = "test-output/reports.extent.html";
/**
* Returns an instance of {@link ExtentReports} object. If it doesn't exist creates a new instance and returns it
*/
public static ExtentReports getLogger() {
if ( extent == null ) {
createInstance();
}
return extent;
}
/**
* Create ExtentReport and attaches htmlReporter to it
*/
public static void createInstance() {
ExtentHtmlReporter htmlReporter = getHTMLReporter();
extent = new ExtentReports();
extent.attachReporter( htmlReporter );
}
/**
* This method creates, configures and returns an instance of ExtentHtmlReporter
*
*/
public static ExtentHtmlReporter getHTMLReporter() {
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter( REPORT_LOCATION );
return htmlReporter;
}
/**
* This method logs a message with the INFO level for both instances of TestNG Logger and ExtentTest
*/
public void info( String message ) {
LOG.info( message );
test.log( Status.INFO, message );
}
【问题讨论】:
-
最简单的方法是使用版本 4 的 testng-adapter:github.com/extent-framework/extentreports-testng-adapter。它无需编写任何代码即可完成所有操作,只需通过 testng.xml 使用侦听器构造或注释类。
标签: java selenium testng extentreports