在使用testng生成报告的时候,只会记录test方法中的日志,可是通常会在beforeMethod、beforeTest、afterMethod、afterTest中作一下数据的处理,这里面的日志没办法在test中显示。查看了testng的源码,发现suite中的getAllInvokedMethods方法会返回全部调用过的方法,包括test、after、before等。拿到了全部方法执行的结果,就能够进行处理,把beforeMethod、beforeTest、afterMethod、afterTest中的数据整合到对应的test中。java
第一步:下载testng源码到本地:git
git clone -b testng-6.8.9 git@github.com:cbeust/testng.gitgithub
修改接口ITestResult和他的实现类TestResult代码以下:maven
在ITestResult添加代码:ide
List<String> log = new ArrayList<String>(); void setLog(List<String> log); List<String> getLog();
在TestResult中添加代码:ui
private List<String> log; public void setLog(List<String> log) { this.log = log; } public List<String> getLog() { return log; }
使用maven打包,要把依赖打进去,因此在pom中添加:this
<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
mvn cleanspa
mvn package日志
生成须要的jar包code
第二步:
把jar包放到咱们的工程目录中(新建lib目录,放到lib下)
工程中pom添加,同时删掉原来有的testng依赖:
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.8</version> <scope>system</scope> <systemPath>${project.basedir}/lib/testng-6.8.8.jar</systemPath> </dependency>
第三步:如何使用
新建类实现IReporter接口:
package test.service;import org.testng.*;import org.testng.xml.XmlSuite;import java.util.*;public class CustomReporter implements IReporter{ @Override public void generateReport(List<XmlSuite> xmlSuite, List<ISuite> suites, String s) { List<ITestResult> list = new ArrayList<>(); List<IInvokedMethod> allInvokedMethodsList = new ArrayList<>(); for (ISuite suite : suites) { Map<String, ISuiteResult> suiteResults = suite.getResults(); List<IInvokedMethod> allInvokedMethods = suite.getAllInvokedMethods(); //排序 Collections.sort(allInvokedMethods, new Comparator<IInvokedMethod>() { @Override public int compare(IInvokedMethod o1, IInvokedMethod o2) { return Long.valueOf(o1.getTestResult().getStartMillis()).compareTo(Long.valueOf(o2.getTestResult().getStartMillis())); } }); } for(int i=0;i<allInvokedMethodsList.size();i++){ IInvokedMethod iInvokedMethod = allInvokedMethodsList.get(i); ITestResult itestResult = iInvokedMethod.getTestResult(); ITestNGMethod iTestNGMethod = itestResult.getMethod(); if(iTestNGMethod.isTest()){ if(i-1>=0){ ITestResult tmpTestResult = allInvokedMethodsList.get(i-1).getTestResult(); if(tmpTestResult.getMethod().isBeforeTestConfiguration() || tmpTestResult.getMethod().isBeforeMethodConfiguration()){ List<String> log = new ArrayList<>(); log.addAll(Reporter.getOutput(tmpTestResult)); log.addAll(Reporter.getOutput(itestResult)); itestResult.setLog(log); } } if(i+1<allInvokedMethodsList.size()){ ITestResult tmpTestResult = allInvokedMethodsList.get(i+1).getTestResult(); if(tmpTestResult.getMethod().isAfterClassConfiguration() || tmpTestResult.getMethod().isAfterMethodConfiguration()){ List<String> log = new ArrayList<>(); log.addAll(Reporter.getOutput(itestResult)); log.addAll(Reporter.getOutput(tmpTestResult)); itestResult.setLog(log); } } list.add(itestResult); } } }}