我有 3 节课。 Test1、Test2 和基类。 Test1 和 Test2 扩展了基类。当我使用 TestNg xml 执行这两个类时,@BeforeClass 失败并出现空指针异常。正在生成 test1 的报告。也没有报告其工作正常。请帮助我
======================================基类========= =====================
public class Base
{
public ExtentReports reports;
public ExtentTest logger;
@BeforeClass
public void startBrowser()
{
String name= LocalDateTime.now().format(DateTimeFormatter.ofPattern("ddMMMYY_HHmmss"));
logger = reports.createTest("Test1"+name);
System.out.println("Started Test1");
}
@BeforeSuite
public void initialSetup()
{
String name= LocalDateTime.now().format(DateTimeFormatter.ofPattern("ddMMMYY_HHmmss"));
ExtentHtmlReporter extent = new ExtentHtmlReporter(new File("./Reports/"+name+".html"));
reports = new ExtentReports();
reports.attachReporter(extent);
}
@AfterClass
public void end()
{
reports.flush();
}
}
==============================测试1类================== ===
public class Test1 extends Base{
@Test
public void TestCase()
{
logger.info("Test1");
}
}
====================Test2类========================== ====
public class Test2 extends Base{
@Test
public void f()
{
System.out.println("Started Test2");
}
}
最佳答案
问题出在你的测试代码上。
您已初始化数据成员reports
在 @BeforeSuite
带注释的方法。
每个 <suite>
仅执行一次标签。
由于您的两个类都是从同一个基类扩展而来,因此 initialSetup()
方法只会执行一次(例如,Test1.java)。
这就是为什么当你的 @BeforeClass
带注释startBrowser()
尝试访问reports.createTest("Test1"+name);
对于 Test2
你遇到了 NullPointerException。
您需要通过移动 initialSetup()
来更改此设置从@BeforeClass
内部调用方法。
关于java - 使用 ExtentReport 获取空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55208747/