使用extentreports的4.1.7版本,对v3版本的Listener进行改造。
依赖包
<dependency> <groupId>com.aventstack</groupId> <artifactId>extentreports</artifactId> <version>4.1.7</version> <scope>test</scope> </dependency>
配置Testng.xml
<listeners> <!--testng的XML配置文件中添加这些内容--> <listener class-name="org.uncommons.reportng.HTMLReporter"/> <listener class-name="org.uncommons.reportng.JUnitXMLReporter"/> <listener class-name="com.shein.dms.config.ExtentTestNGIReporterListener"/> </listeners>
配置pom.xml
<plugin> <artifactId>maven-surefire-plugin</artifactId> <!-- <version>2.22.1</version>--> <configuration> <!--<testFailureIgnore>true</testFailureIgnore>--> <forkMode>never</forkMode> <argLine>-Dfile.encoding=UTF-8</argLine> <suiteXmlFiles> <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile> </suiteXmlFiles> <properties> <property> <name>usedefaultlisteners</name> <value>true</value> </property> <property> <name>listener</name> <value> org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter, com.shein.dms.config.ExtentTestNGIReporterListener </value> </property> </properties> <workingDirectory>target/</workingDirectory> <forkMode>always</forkMode> <!-- 解决报告中中文乱码 --> <argLine>-Dfile.encoding=UTF-8</argLine> </configuration> </plugin> </plugins>
Listener源码:
import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.ExtentTest; import com.aventstack.extentreports.Status; import com.aventstack.extentreports.reporter.ExtentSparkReporter; import com.aventstack.extentreports.reporter.configuration.Theme; import org.springframework.util.StringUtils; import org.testng.*; import org.testng.xml.XmlSuite; import java.io.File; import java.util.*; /** * testng报告监听 * 生成extentreport测试报告 */ public class ExtentTestNGIReporterListener implements IReporter { //生成的路径以及文件名 private static final String OUTPUT_FOLDER = "test-output/"; private static final String FILE_NAME = "extent.html"; private static final String REPORT_TITLE = "DMS自动化测试报告"; private ExtentReports extent; private ExtentSparkReporter sparkReporter; @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; } //统计suite下的成功、失败、跳过的总用例数 int suiteFailSize = 0; int suitePassSize = 0; int suiteSkipSize = 0; ExtentTest suiteTest = null; //存在多个suite的情况下,在报告中将同一个suite的测试结果归为一类,创建一级节点。 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) { //没有创建suite的情况下,将在SuiteResult的创建为一级节点,否则创建为suite的一个子节点。 if (null == suiteTest) { resultNode = extent.createTest(r.getTestContext().getName()); } else { resultNode = suiteTest.createNode(r.getTestContext().getName()); } } else { resultNode = suiteTest; } if (resultNode != null) { // 设置功能名称 e.g. 测试用例集 : demo测试用例 / suite标签名:test标签名 resultNode.getModel().setName(suite.getName() + " : " + r.getTestContext().getName()); // 功能名打标签,影响数据统计 // resultNode.assignCategory(suite.getName(), r.getTestContext().getName()); // 设置用例执行开始和结束时间 resultNode.getModel().setStartTime(r.getTestContext().getStartDate()); resultNode.getModel().setEndTime(r.getTestContext().getEndDate()); //统计SuiteResult下的数据 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)); } // 设置标签信息 String[] categories = {suite.getName(), r.getTestContext().getName()}; // 生成子节点,测试用例 buildTestNodes(resultNode, context.getFailedTests(), Status.FAIL, categories); buildTestNodes(resultNode, context.getSkippedTests(), Status.SKIP, categories); buildTestNodes(resultNode, context.getPassedTests(), Status.PASS, categories); } 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() { //文件夹不存在的话进行创建 File reportDir = new File(OUTPUT_FOLDER); if (!reportDir.exists() && !reportDir.isDirectory()) { reportDir.mkdir(); } // 生成SparkReporter报告,v4版本废弃了原htmlreport,改为sparkReporter // ExtentSparkReporter sparkReporter = new ExtentSparkReporter(OUTPUT_FOLDER + FILE_NAME); sparkReporter = new ExtentSparkReporter(OUTPUT_FOLDER + FILE_NAME); sparkReporter.config().setDocumentTitle(REPORT_TITLE); sparkReporter.config().setReportName(REPORT_TITLE); sparkReporter.config().setEncoding("UTF-8"); sparkReporter.config().setTheme(Theme.STANDARD); sparkReporter.config().setCSS("css-string"); sparkReporter.config().setJS("js-string"); sparkReporter.config().setTimeStampFormat("yyyy-MM-dd HH:mm:ss"); sparkReporter.config().setTheme(Theme.STANDARD); extent = new ExtentReports(); extent.attachReporter(sparkReporter); extent.setReportUsesManualConfiguration(true); } private void buildTestNodes(ExtentTest extenttest, IResultMap tests, Status status, String[] categories) { //存在父节点时,获取父节点的标签 // String[] categories = new String[0]; // if (extenttest != null) { // List<Attribute> 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 testNode; if (tests.size() > 0) { //调整用例排序,按时间排序 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) { String name = ""; String testName = result.getTestName(); // 获取testName //如果有参数,则使用参数的toString组合代替报告中的name // Object[] parameters = result.getParameters(); // if (parameters.length > 0) { // for (Object param : parameters) { // name += param.toString(); // } // if (name.length() > 50) { // name = name.substring(0, 49) + "..."; // } // } if (!StringUtils.isEmpty(testName)) { name = testName; } else { // 使用方法名称当做用例名称 name = result.getMethod().getMethodName(); } if (extenttest == null) { testNode = extent.createTest(name); } else { //作为子节点进行创建时,设置标签,便于报告检索。 testNode = extenttest.createNode(name).assignCategory(categories); } // 设置用例的描述信息 String description = result.getMethod().getDescription(); if (!StringUtils.isEmpty(description)) { testNode.getModel().setDescription(description); } for (String group : result.getMethod().getGroups()) testNode.assignCategory(group); List<String> outputList = Reporter.getOutput(result); for (String output : outputList) { //将用例的log输出报告中 testNode.debug(output); } // 有异常则报错,没有则显示Test passed if (result.getThrowable() != null) { testNode.log(status, result.getThrowable()); } else { testNode.log(status, "Test " + status.toString().toLowerCase() + "ed!" + testNode.getModel().getDescription()); } testNode.getModel().setStartTime(getTime(result.getStartMillis())); testNode.getModel().setEndTime(getTime(result.getEndMillis())); } } } private Date getTime(long millis) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(millis); return calendar.getTime(); } }
报告