我创建了spring boot应用程序,当调用rest端点时触发testng测试。对于报告,我使用的是ExtentReport,文档位于:http://extentreports.com/docs/versions/3/java/
以下是文档中的完整代码:
public class ExtentTestNGITestListener implements ITestListener {
private static ExtentReports extent = ExtentManager.createInstance("extent.html");
private static ThreadLocal parentTest = new ThreadLocal();
private static ThreadLocal test = new ThreadLocal();
@Override
public synchronized void onStart(ITestContext context) {
ExtentTest parent = extent.createTest(getClass().getName());
parentTest.set(parent);
}
@Override
public synchronized void onFinish(ITestContext context) {
extent.flush();
}
@Override
public synchronized void onTestStart(ITestResult result) {
ExtentTest child = parentTest.get().createNode(result.getMethod().getMethodName());
test.set(child);
}
@Override
public synchronized void onTestSuccess(ITestResult result) {
test.get().pass("Test passed");
}
@Override
public synchronized void onTestFailure(ITestResult result) {
test.get().fail(result.getThrowable());
}
@Override
public synchronized void onTestSkipped(ITestResult result) {
test.get().skip(result.getThrowable());
}
@Override
public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult result) {
}
}
public class ExtentManager {
private static ExtentReports extent;
public static ExtentReports getInstance() {
if (extent == null)
createInstance("test-output/extent.html");
return extent;
}
public static ExtentReports createInstance(String fileName) {
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);
htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setDocumentTitle(fileName);
htmlReporter.config().setEncoding("utf-8");
htmlReporter.config().setReportName(fileName);
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
return extent;
}
}
由于我的应用程序正在持续工作,如果上面的代码可能会导致高度讨论的内存泄漏here和here,那么我很有意思?如果不是为什么?