TestNG官网:https://testng.org/doc/documentation-main.html#introduction
TestNG博客:https://www.jianshu.com/p/8a729de618b6
一、接口自动化测试框架结构
ExtentTestNGIReporterListener代码如下:
package com.wang.edu.listener;
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.*;
/**
* 参考博客:https://testerhome.com/topics/8134
*1、使用TestNg的Report监听器,不嵌入具体执行代码,仅需在配置文件中新增监听器即可。
*2、报告文件生成路径为test-output/index.html。(可在代码中修改)
*3、一个suite且一个test配置的情况下,会将执行的用例(method)作为一级节点生成报告。
*4、一个suite且多个test配置的情况下,会将每个test配置作为一级节点,执行用例(method)为对应的子节点
*5、多个suite的情况下,将suite作为一级节点,test配置为二级节点,执行用例(method)为对应的三级节点。(如果suite下只有一个test配置,则不会生成二级节点,直接把执行的用例(method)生成在第二节点中)
*6、代码中使用Report.log("xxx")会将log展示在报告中对应的执行用例(method)中。
*7、自动将suite以及test配置的名字作为执行用例(method)的标签。
*8、如果用例(method)有参数,则会将调用参数的toString()方法作为用例(method)的名字在报告中显示。
*9、已经对执行用例进行按时间排序。(但是多个suite按时间的排序不知道咋处理,求指教。)
*/
/**
* IReporter接口:用户自定义测试报告,我们只需要实现IReporter并添加监听
* 就可以拿到所有测试信息
*/
public class ExtentTestNGIReporterListener 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;
}
//统计suite下的成功、失败、跳过的总用例数
int suiteFailSize=0;
int suitePassSize=0;
int suiteSkipSize=0;
ExtentTest suiteTest=null;
//存在多个suite的情况下,在报告中将同一个一个suite的测试结果归