一 在pom.xml文件中引入TestNg以及ExtentReport包
<dependencies>
<!--testNg引入--> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.10</version> </dependency>
<!--extentreports 包--> <!-- https://mvnrepository.com/artifact/com.aventstack/extentreports --> <dependency> <groupId>com.aventstack</groupId> <artifactId>extentreports</artifactId> <version>3.0.6</version> </dependency> <!-- https://mvnrepository.com/artifact/com.vimalselvam/testng-extentsreport --> <dependency> <groupId>com.vimalselvam</groupId> <artifactId>testng-extentsreport</artifactId> <version>1.3.1</version> </dependency>
</dependencies>
二 ExtentTestNGIReporterListener类实现监听,并能生成报告
//解决样式加载不出来的问题 public class ExtentTestNGIReporterListener implements IReporter { static SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); static String dataNow=format.format(new Date()); //生成的路径以及文件名 private static final String OUTPUT_FOLDER = "test-output/"; private static final String FILE_NAME = "测试报告.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的测试