前言
《高大上的测试报告-Allure开源框架探索》一文,笔者针对Allure框架做了详细的介绍,该篇文章再安利一个备受欢迎的测试报告框架-ExtentReports。
ExtentReports介绍
一睹ExtentReports风采
案例
pom.xml配置
4.0.0
TestNG-Learning
TestNG
1.0-SNAPSHOT
org.apache.maven.plugins
maven-compiler-plugin
6
org.testng
testng
6.11
com.aventstack
extentreports
3.1.5
案例详述
此处使用官网的一个例子:
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 org.testng.*;
import org.testng.xml.XmlSuite;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class ExtentTestNGIReporterListener1 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 (ISuite suite : suites) {
Map result = suite.getResults();
for (ISuiteResult r : result.values()) {
ITestContext context = 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("自动化测试报告");
htmlReporter.config().setReportName("API自动化测试报告");
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP); //图表位置
//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()); //显示方法名称
//test.createNode("子案例"); //创建子案例
for (String group : result.getMethod().getGroups())
test.assignCategory(group); //根据group
if (result.getThrowable() != null) {
test.log(status, result.getThrowable()); //异常案例,显示log到报告
}
else {
test.log(status, "Test " + status.toString().toLowerCase() + "ed");
}
test.getModel().setStartTime(getTime(result.getStartMillis()));
test.getModel().setEndTime(getTime(result.getEndMillis()));
test.getModel().setDescription("测试案例执行!");
}
}
}
private Date getTime(long millis) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
}
编写测试类:
import org.testng.Assert;
import org.testng.annotations.*;
@Test(groups = "Group")
@Listeners(TestListenerAdapterImp.class)
public class TestNGHelloWorld1 {
@BeforeTest
public void bfTest() {
System.out.println("TestNGHelloWorld1 beforTest!");
}
@Test(expectedExceptions = ArithmeticException.class, expectedExceptionsMessageRegExp = ".*zero")
public void helloWorldTest1() {
System.out.println("TestNGHelloWorld1 Test1!");
int c = 1 / 0;
Assert.assertEquals("1", "1");
}
@Test(groups = "Group1")
@Parameters(value = "para")
public void helloWorldTest2(@Optional("Tom")String str) {
Assert.assertEquals("1", "2");
System.out.println("TestNGHelloWorld1 Test2! "+ str);
}
@Test(groups = "Group2")
public void helloWorldTest3(){
System.out.println("TestNGHelloWorld1 Test3!");
}
@AfterTest
public void AfTest() {
System.out.println("TestNGHelloWorld1 AfterTest!");
}
}
配置testng.xml,监听器ExtentTestNGIReporterListener1:
扩展学习资料
ExtentReports 结合 TestNg 生成自动化 html 报告 (支持多 suite)
ExtentReports 官方说明文档翻译
官网