使用extentreports美化testng报告2,增加监听

时间:2022-01-06 17:07:13

有兴趣研究了extentreports报告美化插件,但是因为发现插件有很多内容不能自定义,所以放弃了这个插件,我扩充了官方代码的demo,在testng中增加了监听,并打印了一些测试用例,现在我把两个demo都展示下

如果不想增加监听,可以查看我第一篇文章http://www.cnblogs.com/chongyou/articles/7518959.html,3.0的改变有些大,maven配置

                <dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.0.7</version>
</dependency>

1.运行用例后的demo

显示内容截图

使用extentreports美化testng报告2,增加监听

使用extentreports美化testng报告2,增加监听

使用extentreports美化testng报告2,增加监听

代码块,有两个主要的方法,1,监听testng中的reporter,2.实现testng中的ITestListener接口,如下图,在有一个类mytest2就是运行

使用extentreports美化testng报告2,增加监听

ExtentTestNGIReporterListener.java

package report;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map; import org.testng.IReporter;
import org.testng.IResultMap;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.Reporter;
import com.aventstack.extentreports.Status;
import org.testng.xml.XmlSuite; import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme; 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); 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 report;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult; import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme; 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) { }
} 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("zhangjun")
.assignAuthor("admin")
.pass("details"); return extent;
}
}

mytest2.java

package report;

import org.testng.Assert;
import org.testng.annotations.Test; import until.logTest; public class mytest2{
@Test
public void choose_product(){
logTest.logInfo("成功的结果");
Assert.assertTrue(true);
}
@Test
public void choose_product2(){
logTest.logInfo("成功的结果");
Assert.assertTrue(true);
}
@Test
public void choose_product3(){
logTest.logInfo("失败的结果");
Assert.assertTrue(false);
}
}

xml文件配置

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="rosewholePC" >
<listeners>
<listener class-name="report.ExtentTestNGIReporterListener" /> </listeners>
<test name="version" preserve-order="true"> <classes>
<class name="report.mytest2">
<methods>
<include name="choose_product"/>
<include name="choose_product2"/>
<include name="choose_product3"/>
</methods>
</class>
</classes> </test>
</suite>

  

2.报告中增加测试用例

我开始增加从我的excel中去读取测试方法,并在报告中展示,如图,就是因为测试用例字体也太小了,觉得不合适,所以也就放弃了

使用extentreports美化testng报告2,增加监听

使用extentreports美化testng报告2,增加监听

修改了就修改了1个地方,在ExtentTestNGIReporterListener类的方法buildTestNodes中增加读取测试用例,获取excel的测试用例的描述,

注publicmethod的readstestcase的方法在文章selenium项目读取测试用例有写到

使用extentreports美化testng报告2,增加监听

readTestCase的方法,主要是从我项目中读取testcase

public static Map<String, List> readTestCase( String xlsFileName,String className) {

		Map<String, List> map = new HashMap<String, List>();

		List<String[]> list = new ArrayList<String[]>();

		Workbook rwb = null;
Cell cell = null; InputStream stream;
try {
stream = new FileInputStream(System.getProperty("user.dir") + "\\testCase\\" + xlsFileName + ".xls");
rwb = Workbook.getWorkbook(stream);
} catch (FileNotFoundException e) {
logTest.logError("读取excel出现异常,请检测名称是否对应正确或其他异常!!!");
e.printStackTrace();
} catch (BiffException e) {
logTest.logError("读取excel出现异常,请检测名称是否对应正确或其他异常!!!");
e.printStackTrace();
} catch (IOException e) {
logTest.logError("读取excel出现异常,请检测名称是否对应正确或其他异常!!!");
e.printStackTrace();
} Sheet sheet = rwb.getSheet(className);
int rows = sheet.getRows();//获取的行
int coumn = sheet.getColumns();//获取的列 String[] strkey = new String[rows - 1];// 存取testCase的值 for (int i = 1; i < rows; i++) {
String[] strValue = new String[coumn - 1];// 存取每一行的数据
strkey[i - 1] = sheet.getCell(0, i).getContents();
for (int j = 1; j < coumn; j++) {
strValue[j - 1] = sheet.getCell(j, i).getContents();
}
list.add(strValue);
} // 把行的数据加入map中
for (int i = 0; i < strkey.length; i++) {
map.put(strkey[i], Arrays.asList(list.get(i)));
} return map;
}

用例模板为

使用extentreports美化testng报告2,增加监听