当我运行第一类时,结果会添加到报告中,但是当我运行第二类时,报告不会保留第一类的结果
//SimpleReportFactory {
package Rapport;
import com.relevantcodes.extentreports.DisplayOrder;
import com.relevantcodes.extentreports.ExtentReports;
public class SimpleReportFactory {
private static ExtentReports reporter;
public static synchronized ExtentReports getReporter() {
if (reporter == null) {
reporter = new ExtentReports("/Users/user/Desktop/untitled folder/SimpleReport3.html", true, DisplayOrder.NEWEST_FIRST);
}
return reporter;
}
public static synchronized void closeReporter() {
reporter.flush();
reporter.close();
}
}
//一级Test001
package Rapport;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
public class Test001 {
private ExtentReports reporter = SimpleReportFactory.getReporter();
@Test
public void simpleTest002()
{
ExtentTest testReporter = reporter.startTest("simpleTest002", "This is a simple simpleTest002");
testReporter.log(LogStatus.INFO, "Starting test simpleTest002");
int a = 100;
int b = 30;
testReporter.log(LogStatus.INFO, "Loading the value of a to " + a);
testReporter.log(LogStatus.PASS, "Loading the value of b to " + b);
reporter.endTest(testReporter);
}
@AfterSuite
public void afterSuite()
{
reporter.close();
}
}
//第二类Test002
package Rapport;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
public class Test002 {
private ExtentReports reporter = SimpleReportFactory.getReporter();
@Test
public void simpleTest004()
{
ExtentTest testReporter = reporter.startTest("simpleTest004", "This is a simple simpleTest004");
testReporter.log(LogStatus.INFO, "Starting test simpleTest004");
int a = 100;
int b = 30;
testReporter.log(LogStatus.INFO, "Loading the value of a to " + a);
testReporter.log(LogStatus.PASS, "Loading the value of b to " + b);
reporter.endTest(testReporter);
}
@AfterSuite
public void afterSuite()
{
reporter.close();
}
最佳答案
因为您正在使用
private ExtentReports reporter = SimpleReportFactory.getReporter();
两次;
对于 Extent 报告,如果您使用 ExtentReport 实例两次,则第一个实例的详细信息将被清除,它仅显示最后一个实例,在您的情况下,它仅显示第二个实例结果。
因此,尝试仅启动一次范围报告,然后在整个测试周期中使用此实例。
制作
ExtentReports reporter
实例全局且静态,然后使用此
reporter instance to the whole test cycle.
在测试执行最后关闭报告器实例。
关于java - 无法在 ExtentReport 中添加两个类的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36180844/