我正在使用一个套件文件,在testng.xml文件中包含多个套件,如下所示:
<suite-files>
<suite-file path="suite1"></suite-file>
<suite-file path="suite2"></suite-file>
</suite-files>我在BeforeSuite中初始化ExtentReport。
private static void initializeExtentReport(Configuration config) {
if (extent == null) {
extent = new ExtentReports();
htmlReporter = new ExtentHtmlReporter("reportLocation");
ClassLoader classLoader = ExtentReportService.class.getClassLoader();
File extentConfigFile = new File(classLoader.getResource("extent-config.xml").getFile());
htmlReporter.loadXMLConfig(extentConfigFile);
extent.attachReporter(htmlReporter);
extent.setSystemInfo("Environment", config.getAutomationServer());
}
}在AfterSuite中,我调用flush()。
因此,基本上问题是,当为第二个套件调用之前的套件时,检查(extent==null)是假的。我还查看了ExtentReports的JavaDocs,在那里我找到了一个方法detachReporter()。但是我无法通过我的IDE访问。尝试了许多变化,但都没有结果。
编辑:
现在实际发生的情况是,我对报告使用了自定义名称,因此没有两个报告名称是相同的。而且,当我使用相同的名称时,结果会被覆盖在相同的套件文件中。
发布于 2018-08-29 09:11:56
这里更好的方法是使用单例,如下所示:
public class Extent
implements Serializable {
private static final long serialVersionUID = 1L;
private static class ExtentReportsLoader {
private static final ExtentReports INSTANCE = new ExtentReports();
static {
}
}
public static synchronized ExtentReports getInstance() {
return ExtentReportsLoader.INSTANCE;
}
@SuppressWarnings("unused")
private ExtentReports readResolve() {
return ExtentReportsLoader.INSTANCE;
}
}用法:
ExtentReports extent = Extent.getInstance();所以你的代码变成了:
private static void initializeExtentReport(Configuration config) {
extent = Extent.getInstance();
if (extent.getStartedReporters().isEmpty()) {
htmlReporter = new ExtentHtmlReporter("reportLocation");
ClassLoader classLoader = ExtentReportService.class.getClassLoader();
File extentConfigFile = new File(classLoader.getResource("extent-config.xml").getFile());
htmlReporter.loadXMLConfig(extentConfigFile);
extent.attachReporter(htmlReporter);
extent.setSystemInfo("Environment", config.getAutomationServer());
}
}我进一步建议去掉extent/htmlReporter的所有共享变量,直接使用Singleton
https://stackoverflow.com/questions/52057510
复制相似问题