大致簡介
用過很多的測試報告,testng自帶的,然后后面自己試著寫html代碼美化,效果都不是特別的好,直到我發(fā)現(xiàn)了現(xiàn)在這款自動化測試報告,簡潔直觀,UI相對testng來講不是一個級別的,話不多說,直接上圖;
轉(zhuǎn)載需要注明出處

Extentreport效果圖
如何使用
那么這么好的一款測試報告如何使用呢?那接下來和我一起探索這一款優(yōu)秀的測試報告吧~
首先第一步創(chuàng)建一個maven項目(如何創(chuàng)建請Google或度娘)
創(chuàng)建好的項目如下:
image.png
在pom文件中添加相關(guān)的引用jar包
<dependencies>
<!-- extentreports測試報告插件 -->
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
<!-- 測試報告插件和testng的結(jié)合 -->
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>testng-extentsreport</artifactId>
<version>1.3.1</version>
</dependency>
<!-- extentreports測試報告插件 -->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.0.6</version>
</dependency>
</dependencies>
點一下右下角的Import Changes,引入(有些同學(xué)是用的maven的中央倉庫因為被墻了會很慢,需要改成阿里巴巴的會比較快一點,修改的方法很簡單自行Google或度娘)
引入jar
我們來寫一點用來測試的代碼,如下:
package com.test.extentreport;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;
public class TestDemo {
@Test
public void test1(){
Assert.assertEquals(1,2);
}
@Test
public void test2(){
Assert.assertEquals(1,1);
}
@Test
public void test3(){
Assert.assertEquals("aaa","aaa");
}
@Test
public void logDemo(){
Reporter.log("這是我們自己寫的日志");
throw new RuntimeException("這是我自己的運行時異常");
}
}
寫完測試的代碼之后進行xml文件的編寫
在resources文件夾新建文件testng.xml,內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<suite name ="testname1">
<test name="testdemo1">
<classes>
<class name="com.tester.extentreports.demo.testDemo">
<methods>
<inclube name="test1"/>
<inclube name="test2"/>
<inclube name="test3"/>
<inclube name="logDemo"/>
</methods>
</class>
</classes>
</test>
<!--配置監(jiān)聽器-->
<listeners>
<listener class-name="com.tester.extentreports.demo.ExtentTestNGIReporterListenerOld"/>
</listeners>
</suite>
運行,運行完成之后會出現(xiàn)一個如下圖的目錄
運行完成
右鍵單擊emailable-report.html,點擊copypath
copypath
打開瀏覽器粘貼剛剛的path,得到結(jié)果沒有樣式的測試報告
copypath
刪除鏈接中的 emailable- ,發(fā)現(xiàn)還是沒有樣式,原因是因為我們的樣式加載不到,樣式文件在“墻外”那么怎么解決呢
更換地址
首先添加一個工具類(ExtentTestNGIReporterListenerOld.java)
package com.test.extentreport;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.ResourceCDN;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.model.TestAttribute;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
import org.testng.*;
import org.testng.xml.XmlSuite;
import java.io.File;
import java.util.*;
public class ExtentTestNGIReporterListenerOld implements IReporter {
//生成的路徑以及文件名
private static final String OUTPUT_FOLDER = "test-output/";
private static final String FILE_NAME = "index.html";
private ExtentReports extent;
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
init();
boolean createSuiteNode = false;
if(suites.size()>1){
createSuiteNode=true;
}
for (ISuite suite : suites) {
Map<String, ISuiteResult> result = suite.getResults();
//如果suite里面沒有任何用例,直接跳過,不在報告里生成
if(result.size()==0){
continue;
}
//統(tǒng)計suite下的成功、失敗、跳過的總用例數(shù)
int suiteFailSize=0;
int suitePassSize=0;
int suiteSkipSize=0;
ExtentTest suiteTest=null;
//存在多個suite的情況下,在報告中將同一個一個suite的測試結(jié)果歸為一類,創(chuàng)建一級節(jié)點。
if(createSuiteNode){
suiteTest = extent.createTest(suite.getName()).assignCategory(suite.getName());
}
boolean createSuiteResultNode = false;
if(result.size()>1){
createSuiteResultNode=true;
}
for (ISuiteResult r : result.values()) {
ExtentTest resultNode;
ITestContext context = r.getTestContext();
if(createSuiteResultNode){
//沒有創(chuàng)建suite的情況下,將在SuiteResult的創(chuàng)建為一級節(jié)點,否則創(chuàng)建為suite的一個子節(jié)點。
if( null == suiteTest){
resultNode = extent.createTest(r.getTestContext().getName());
}else{
resultNode = suiteTest.createNode(r.getTestContext().getName());
}
}else{
resultNode = suiteTest;
}
if(resultNode != null){
resultNode.getModel().setName(suite.getName()+" : "+r.getTestContext().getName());
if(resultNode.getModel().hasCategory()){
resultNode.assignCategory(r.getTestContext().getName());
}else{
resultNode.assignCategory(suite.getName(),r.getTestContext().getName());
}
resultNode.getModel().setStartTime(r.getTestContext().getStartDate());
resultNode.getModel().setEndTime(r.getTestContext().getEndDate());
//統(tǒng)計SuiteResult下的數(shù)據(jù)
int passSize = r.getTestContext().getPassedTests().size();
int failSize = r.getTestContext().getFailedTests().size();
int skipSize = r.getTestContext().getSkippedTests().size();
suitePassSize += passSize;
suiteFailSize += failSize;
suiteSkipSize += skipSize;
if(failSize>0){
resultNode.getModel().setStatus(Status.FAIL);
}
resultNode.getModel().setDescription(String.format("Pass: %s ; Fail: %s ; Skip: %s ;",passSize,failSize,skipSize));
}
buildTestNodes(resultNode,context.getFailedTests(), Status.FAIL);
buildTestNodes(resultNode,context.getSkippedTests(), Status.SKIP);
buildTestNodes(resultNode,context.getPassedTests(), Status.PASS);
}
if(suiteTest!= null){
suiteTest.getModel().setDescription(String.format("Pass: %s ; Fail: %s ; Skip: %s ;",suitePassSize,suiteFailSize,suiteSkipSize));
if(suiteFailSize>0){
suiteTest.getModel().setStatus(Status.FAIL);
}
}
}
// for (String s : Reporter.getOutput()) {
// extent.setTestRunnerOutput(s);
// }
extent.flush();
}
private void init() {
//文件夾不存在的話進行創(chuàng)建
File reportDir= new File(OUTPUT_FOLDER);
if(!reportDir.exists()&& !reportDir .isDirectory()){
reportDir.mkdir();
}
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
// 設(shè)置靜態(tài)文件的DNS
htmlReporter.config().setDocumentTitle("api自動化測試報告");
htmlReporter.config().setReportName("api自動化測試報告");
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setCSS(".node.level-1 ul{ display:none;} .node.level-1.active ul{display:block;}");
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setReportUsesManualConfiguration(true);
}
private void buildTestNodes(ExtentTest extenttest, IResultMap tests, Status status) {
//存在父節(jié)點時,獲取父節(jié)點的標(biāo)簽
String[] categories=new String[0];
if(extenttest != null ){
List<TestAttribute> categoryList = extenttest.getModel().getCategoryContext().getAll();
categories = new String[categoryList.size()];
for(int index=0;index<categoryList.size();index++){
categories[index] = categoryList.get(index).getName();
}
}
ExtentTest test;
if (tests.size() > 0) {
//調(diào)整用例排序,按時間排序
Set<ITestResult> treeSet = new TreeSet<ITestResult>(new Comparator<ITestResult>() {
@Override
public int compare(ITestResult o1, ITestResult o2) {
return o1.getStartMillis()<o2.getStartMillis()?-1:1;
}
});
treeSet.addAll(tests.getAllResults());
for (ITestResult result : treeSet) {
Object[] parameters = result.getParameters();
String name="";
//如果有參數(shù),則使用參數(shù)的toString組合代替報告中的name
for(Object param:parameters){
name+=param.toString();
}
if(name.length()>0){
if(name.length()>50){
name= name.substring(0,49)+"...";
}
}else{
name = result.getMethod().getMethodName();
}
if(extenttest==null){
test = extent.createTest(name);
}else{
//作為子節(jié)點進行創(chuàng)建時,設(shè)置同父節(jié)點的標(biāo)簽一致,便于報告檢索。
test = extenttest.createNode(name).assignCategory(categories);
}
//test.getModel().setDescription(description.toString());
//test = extent.createTest(result.getMethod().getMethodName());
for (String group : result.getMethod().getGroups())
test.assignCategory(group);
List<String> outputList = Reporter.getOutput(result);
for(String output:outputList){
//將用例的log輸出報告中
test.debug(output);
}
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();
}
}
修改一下代碼
private void init() {
//文件夾不存在的話進行創(chuàng)建
File reportDir= new File(OUTPUT_FOLDER);
if(!reportDir.exists()&& !reportDir .isDirectory()){
reportDir.mkdir();
}
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
// 設(shè)置靜態(tài)文件的DNS
//解決cdn訪問不了的問題
htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
htmlReporter.config().setDocumentTitle("api自動化測試報告");
htmlReporter.config().setReportName("api自動化測試報告");
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setCSS(".node.level-1 ul{ display:none;} .node.level-1.active ul{display:block;}");
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setReportUsesManualConfiguration(true);
}
修改監(jiān)聽器
<listeners>
<!-- <listener class-name="com.vimalselvam.testng.listener.ExtentTestNgFormatter"/>-->
<listener class-name="com.test.extentreport.ExtentTestNGIReporterListenerOld"/>
</listeners>
修改監(jiān)聽器后運行testng.xml文件會生成index.html
index.html
copy path
copy path
在瀏覽器打開
結(jié)果圖
完成!








