ExtentReports自动测试报告插件和TestNG集成
1. 自动化测试报告一般使用内部自带的report,生成的报告如下:
可以看到成功总数,失败总数;必须点进去后才能看到详细的哪些成功/失败;
2. 在网上看到一个插件比较好看,生成的报告可视化好,拿出来集成在testNG里面,下面是报告结果图:
3. 集成步骤说明:
1> build.gradle文件中添加jar依赖:【添加完后要build下】
compile 'com.aventstack:extentreports:3.0.6'
compile 'com.relevantcodes: extentreports:2.40.2'
compile 'com.vimalselvam:testng-extentsreport:1.3.1'
2> 创建TestNg的Report监听器:【代码见最后第6步】
ExtentTestNGIReporterListener.java
ExtentTestNGITestListener.java
ExtentManager.java
3> testNg.xml配置监听:
若需使用testNG自带的report,需要添加监听:
4> 运行testng.xml;
5> 运行完后,在该项目下的test-output下可看到Extent.html;
6> 代码参考:
ExtentTestNGIReporterListener.java
package auto.util.report;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.ResourceCDN;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
import org.testng.*;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class ExtentTestNGIReporterListener implements IReporter {
private static final String OUTPUT_FOLDER = "test-output/";
private static final String FILE_NAME = "Extent.html";
private ExtentReports extent;
@Override
public void generateReport(List xmlSuites, List suites, String outputDirectory) {
init();
for (Object suite : suites) {
Map result = ((ISuite) suite).getResults();
for (Object r : result.values()) {
ITestContext context = ((ISuiteResult) r).getTestContext();
buildTestNodes(context.getFailedTests(), Status.FAIL);
buildTestNodes(context.getSkippedTests(), Status.SKIP);
buildTestNodes(context.getPassedTests(), Status.PASS);
}
}
for (String s : Reporter.getOutput()) {
extent.setTestRunnerOutput(s);
}
extent.flush();
}
private void init() {
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
htmlReporter.config().setDocumentTitle("ExtentReports - Created by TestNG Listener");
htmlReporter.config().setReportName("ExtentReports - Created by TestNG Listener");
htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setReportUsesManualConfiguration(true);
}
private void buildTestNodes(IResultMap tests, Status status) {
ExtentTest test;
if (tests.size() > 0) {
for (ITestResult result : tests.getAllResults()) {
test = extent.createTest(result.getMethod().getMethodName());
for (String group : result.getMethod().getGroups())
test.assignCategory(group);
if (result.getThrowable() != null) {
test.log(status, result.getThrowable());
}
else {
test.log(status, "Test " + status.toString().toLowerCase() + "ed");
}
test.getModel().setStartTime(getTime(result.getStartMillis()));
test.getModel().setEndTime(getTime(result.getEndMillis()));
}
}
}
private Date getTime(long millis) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
}
ExtentTestNGITestListener.java
package auto.util.report;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
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 = ((ExtentTest) parentTest.get()).createNode(result.getMethod().getMethodName());
test.set(child);
}
@Override
public synchronized void onTestSuccess(ITestResult result) {
((ExtentTest) test.get()).pass("Test passed");
}
@Override
public synchronized void onTestFailure(ITestResult result) {
((ExtentTest) test.get()).fail(result.getThrowable());
}
@Override
public synchronized void onTestSkipped(ITestResult result) {
((ExtentTest) test.get()).skip(result.getThrowable());
}
@Override
public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult result) {
}
}
ExtentManager.java
package auto.util.report;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
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);
// while creating test
extent
.createTest("zzz")
.assignAuthor("admin")
.pass("details");
return extent;
}
}
4. 可能遇到的问题:
若生成的报告没有样式:需要在ExtentTestNGIReporterListener的init方法里面添加:htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
问题可参考:https://blog.csdn.net/HY_smile/article/details/81705951