修改testng源码,添加beforeMethod和afterMethod中的日志到test中(可以不改源码,废弃)

在使用testng生成报告的时候,只会记录test方法中的日志,但是一般会在beforeMethod、beforeTest、afterMethod、afterTest中做一下数据的处理,这里面的日志没办法在test中显示。查看了testng的源码,发现suite中的getAllInvokedMethods方法会返回所有调用过的方法,包括test、after、before等。拿到了所有方法执行的结果,就可以进行处理,把beforeMethod、beforeTest、afterMethod、afterTest中的数据整合到对应的test中。

第一步:下载testng源码到本地:

git clone -b testng-6.8.9 git@github.com:cbeust/testng.git

修改接口ITestResult和他的实现类TestResult代码如下:

在ITestResult添加代码:

List<String> log = new ArrayList<String>();

  void setLog(List<String> log);
  List<String> getLog();

在TestResult中添加代码:

private List<String> log;
public void setLog(List<String> log) {
    this.log = log;
  }

  public List<String> getLog() {
    return log;
  }

 使用maven打包,要把依赖打进去,所以在pom中添加:

<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 clean

mvn package

生成需要的jar包

第二步:

把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);
}
}
}
}
 

 

posted @ 2019-01-18 15:22  弘文馆校书  阅读(950)  评论(2编辑  收藏  举报