TestNG与ExtentReport单元测试导出报告文档

TestNG与ExtentReport集成

目录

1 通过实现ITestListener的方法添加Reporter log
  1.1 MyTestListener设置
  1.2 输出结果
2 TestNG与ExtentReporter集成
  2.1 项目结构
  2.2 MyExtentReportListener设置
  2.3 单多Suite、Test组合测试
    2.3.1 单Suite单Test
    2.3.2 单Suite多Test
    2.3.3 多Suite

源代码:interface-test-framework.zip

1 通过实现ITestListener的方法添加Reporter log

TestNG的Listener列表

TestNG提供了一组预定义的Listener Java接口,这些接口全部继承自TestNG的 ITestNGListener接口。用户创建这些接口的实现类,并把它们加入到 TestNG 中,TestNG便会在测试运行的不同时刻调用这些类中的接口方法:

  • IExecutionListener   监听TestNG运行的启动和停止。
  • IAnnotationTransformer 注解转换器,用于TestNG测试类中的注解。
  • ISuiteListener 测试套件监听器,监听测试套件的启动和停止。
  • ITestListener  测试方法执行监听。
  • IConfigurationListener 监听配置方法相关的接口。
  • IMethodInterceptor 拦截器,调整测试方法的执行顺序。
  • IInvokedMethodListener 测试方法拦截监听,用于获取被TestNG调用的在Method的Before 和After方法监听器。该方法只会被配置和测试方法调用。
  • IHookable 若测试类实现了该接口,当@Test方法被发现时,它的run()方法将会被调用来替代@Test方法。这个测试方法通常在IHookCallBack的callback之上调用,比较适用于需要JASS授权的测试类。
  • IReporter 实现该接口可以生成一份测试报告。

本文将着重介绍最常用到的两个Listener ITestListener与Ireporter接口。

1.1 MyTestListener设置

通过实现ITestListener的方法,添加Reporter.log

MyTestListener.java

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.Reporter;

public class MyTestListener implements ITestListener {
    //用例执行结束后,用例执行成功时调用
    public void onTestSuccess(ITestResult tr) {
        logTestEnd(tr, "Success");
    }

    //用例执行结束后,用例执行失败时调用
    public void onTestFailure(ITestResult tr) {
        logTestEnd(tr, "Failed");
    }

    //用例执行结束后,用例执行skip时调用
    public void onTestSkipped(ITestResult tr) {
        logTestEnd(tr, "Skipped");
    }

    //每次方法失败但是已经使用successPercentage进行注释时调用,并且此失败仍保留在请求的成功百分比之内。
    public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {
        logTestEnd(tr, "FailedButWithinSuccessPercentage");
    }

    //每次调用测试@Test之前调用
    public void onTestStart(ITestResult result) {
        logTestStart(result);
    }

    //在测试类被实例化之后调用,并在调用任何配置方法之前调用。
    public void onStart(ITestContext context) {
        return;
    }

    //在所有测试运行之后调用,并且所有的配置方法都被调用
    public void onFinish(ITestContext context) {
        return;
    }

    // 在用例执行结束时,打印用例的执行结果信息
    protected void logTestEnd(ITestResult tr, String result) {
        Reporter.log(String.format("-------------Result: %s-------------", result), true);
    }

    // 在用例开始时,打印用例的一些信息,比如@Test对应的方法名,用例的描述等等
    protected void logTestStart(ITestResult tr) {
        Reporter.log(String.format("-------------Run: %s.%s---------------", tr.getTestClass().getName(), tr.getMethod().getMethodName()), true);
        Reporter.log(String.format("用例描述: %s, 优先级: %s", tr.getMethod().getDescription(), tr.getMethod().getPriority()),true);
        return;
    }
}

1.2 输出结果

SmkDemo1.java

import com.demo.listener.MyTestListener;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners({MyTestListener.class})
public class SmkDemo1 {
    @Test(description="测testPass11的描述",priority = 1,groups = {"分组1"})
    public void testPass11(){
        Reporter.log("Test11的第一步",true);
        Assert.assertEquals(1,1);
    }
}

输出界面显示如下

图1 log to 输出界面

2 TestNG与ExtentReporter集成

2.1 项目结构

图2 项目结构

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demo</groupId>
    <artifactId>interface-test-framework</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- 测试报告插件和testng的结合 -->
        <dependency>
            <groupId>com.vimalselvam</groupId>
            <artifactId>testng-extentsreport</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!-- extentreports测试报告插件 -->
        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>3.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.1.0</version>
        </dependency>
    </dependencies>
</project>

2.2 MyExtentReportListener设置

MyExtentReporterListener.java

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.ResourceCDN;
import com.aventstack.extentreports.Status;
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.*;

public class MyExtentReporterListener implements IReporter {
    //生成的路径以及文件名
    private static final String OUTPUT_FOLDER = "test-output/";
    private static final String FILE_NAME = "report.html";

    private ExtentReports extent;

    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());
                suiteTest = extent.createTest(suite.getName());
            }
            boolean createSuiteResultNode = false;
            if(result.size()>1){
                createSuiteResultNode=true;
            }
            for (ISuiteResult r : result.values()) {
                ExtentTest resultNode=null;
                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;
                }
                String[] categories=new String[1];
                if(resultNode != null){
                    resultNode.getModel().setName(suite.getName()+"."+r.getTestContext().getName());
                    if(resultNode.getModel().hasCategory()){
                        resultNode.assignCategory(r.getTestContext().getName());
                    }else{
//                        resultNode.assignCategory(suite.getName(),r.getTestContext().getName());
                        categories[0]=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));
                }
                buildTestNodes(resultNode,categories,context.getFailedTests(), Status.FAIL);
                buildTestNodes(resultNode,categories,context.getSkippedTests(), Status.SKIP);
                buildTestNodes(resultNode,categories,context.getPassedTests(), Status.PASS);


            }
            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();
        }
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
        // 设置静态文件的DNS
        //解决cdn访问不了的问题
        htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);

        htmlReporter.config().setDocumentTitle("api自动化测试报告");
        htmlReporter.config().setReportName("api自动化测试报告");
        htmlReporter.config().setChartVisibilityOnOpen(true);
        htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
        htmlReporter.config().setTheme(Theme.STANDARD);
        htmlReporter.config().setCSS(".node.level-1  ul{ display:none;} .node.level-1.active ul{display:block;}");
        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);
        extent.setReportUsesManualConfiguration(true);
    }

    private void buildTestNodes(ExtentTest extenttest, String[] categories, IResultMap tests, Status status) {
//        //存在父节点时,获取父节点的标签
//        String[] categories=new String[0];
//        if(extenttest != null ){
//            List<TestAttribute> 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 test;

        if (tests.size() > 0) {
            //调整用例排序,按时间排序
            Set<ITestResult> treeSet = new TreeSet<ITestResult>(new Comparator<ITestResult>() {
                public int compare(ITestResult o1, ITestResult o2) {
                    return o1.getStartMillis()<o2.getStartMillis()?-1:1;
                }
            });
            treeSet.addAll(tests.getAllResults());
            for (ITestResult result : treeSet) {
                Object[] parameters = result.getParameters();
                String name="";
                //如果有参数,则使用参数的toString组合代替报告中的name
                for(Object param:parameters){
                    name+=param.toString();
                }
                if(name.length()>0){
                    if(name.length()>50){
                        name= name.substring(0,49)+"...";
                    }
                }else{
                    name = result.getMethod().getMethodName();
                }
                if(extenttest==null){
                    test = extent.createTest(name);
                }else{
                    //作为子节点进行创建时,设置同父节点的标签一致,便于报告检索。
                    test = extenttest.createNode(name).assignCategory(categories);
                }
                //test.getModel().setDescription(description.toString());
                //test = extent.createTest(result.getMethod().getMethodName());
                for (String group : result.getMethod().getGroups())
                    test.assignCategory(group);

                List<String> outputList = Reporter.getOutput(result);
                for(String output:outputList){
                    //将用例的log输出报告中
                    test.debug(output);
                }
                if (result.getThrowable() != null) {
                    test.log(status, result.getThrowable());
                }
                else {
                    test.log(status, "Test " + status.toString().toLowerCase() + "ed");
                }

                test.getModel().setStartTime(getTime(result.getStartMillis()));
                test.getModel().setEndTime(getTime(result.getEndMillis()));
            }
        }
    }

    private Date getTime(long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return calendar.getTime();
    }
}

2.3 单多Suite、Test组合测试

2.3.1 单Suite单Test

testngSingleSuiteSingleTest.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SingleSuite">
    <test name="SingleTest" verbose="1" preserve-order="true" >
        <classes>
            <class name="com.demo.testcase.smoke.SmkDemo1">
            </class>
            <class name="com.demo.testcase.sit.SitDemo2">
            </class>
        </classes>
    </test>
    <!--配置监听器-->
    <listeners>
        <listener class-name="com.demo.listener.MyTestListener"/>
        <listener class-name="com.demo.listener.MyExtentReporterListener"/>
    </listeners>
</suite>

图3 单Suite单Test总览

图4 单Suite单Test分组

图5 单Suite单Test错误分组

图6 单Suite单Test Dashboard

2.3.2 单Suite多Test

testngSingleSuiteDoubleTest.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SingleSuite">
    <test name="DoubleTest1" verbose="1" preserve-order="true" >
        <classes>
            <class name="com.demo.testcase.smoke.SmkDemo1">
            </class>
        </classes>
    </test>
    <test name="DoubleTest2" verbose="1" preserve-order="true" >
        <classes>
            <class name="com.demo.testcase.sit.SitDemo2">
            </class>
        </classes>
    </test>
    <!--配置监听器-->
    <listeners>
        <listener class-name="com.demo.listener.MyTestListener"/>
        <listener class-name="com.demo.listener.MyExtentReporterListener"/>
    </listeners>
</suite>

图7 单Suite多Test总览

图8 单Suite多Test分组

2.3.3 多Suite

testngDoubleSuite.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="DoubleSuite">
    <suite-files>
        <suite-file path="testngSingleSuiteSingleTest.xml"/>
        <suite-file path="testngSingleSuiteDoubleTest.xml"/>
    </suite-files>
    <!--配置监听器-->
    <listeners>
        <listener class-name="com.demo.listener.MyTestListener"/>
        <listener class-name="com.demo.listener.MyExtentReporterListener"/>
    </listeners>
</suite>

图9 多Suite总览1

图10 多Suite总览2

图11 多Suite分组

参考

[1] testng框架Listener介绍及测试结果的收集

[2] TestNG执行的日志ITestListener与结果IReporter

[3] TestNG执行的日志ITestListener与结果IReporter

[4] TestNg Beginner’s Guide–阅后总结之Textng.xml

[5] TestNg Beginner’s Guide–阅后总结之TestNg注解

ExtentReports 另一种方法

引言

在走进Java接口测试之测试框架TestNG 中我们详细介绍了 TestNG 的各种用法, 在本文中,我将详细介绍如何将 ExtentReports 测试报告与TestNG集成。

ExtentReports 简介

主要特点:

  • 生成的报告简洁美观
  • 生成的单html方便 Jenkins 集成发邮件
  • 自带集中展示历史报告的服务端
  • 支持 Java 和 .Net

TestNG 原生报告有点丑,信息整理有点乱。ExtentReports 是用于替换TestNG 原生报告。当然也可以使用 ReportNg,个人偏好 ExtentReports 样式。

官网已经给了很多demo了,大家可以参考练习。

官网:http://extentreports.com/

客户端:

https://github.com/anshooarora/extentreports-java/commits/master

服务端:https://github.com/anshooarora/extentx

Step-1:添加 Maven 依赖包

引入pom.xml

<!--引入extentreports相关包-->
        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>3.1.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.vimalselvam</groupId>
            <artifactId>testng-extentsreport</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.relevantcodes</groupId>
            <artifactId>extentreports</artifactId>
            <version>2.41.2</version>
        </dependency>
        <!--引入testng测试框架-->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>compile</scope>
        </dependency>

Step-2:重写 ExtentTestNgFormatter 类

主要基于以下两项原因:

  • 支持报告中展示更多状态类型的测试结果,例如:成功、失败、警告、跳过等。
  • 因为不支持cdn.rawgit.com访问,故替css访问方式。
创建 MyExtentTestNgFormatter 类

下载 ExtentReportes 源码,找到 ExtentTestNgFormatter 类,Listener 目录下创建 MyExtentTestNgFormatter.java 类直接继承 ExtentTestNgFormatter 类。

public class MyExtentTestNgFormatter extends ExtentTestNgFormatter {
解决CDN无法访问

构造方法加入

htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);

具体代码如下:

public MyExtentTestNgFormatter() {
      setInstance(this);
      testRunnerOutput = new ArrayList<>();
      String reportPathStr = System.getProperty("reportPath");
      File reportPath;

      try {
          reportPath = new File(reportPathStr);
      } catch (NullPointerException e) {
          reportPath = new File(TestNG.DEFAULT_OUTPUTDIR);
      }

      if (!reportPath.exists()) {
          if (!reportPath.mkdirs()) {
              throw new RuntimeException("Failed to create output run directory");
          }
      }

      File reportFile = new File(reportPath, "report.html");
      File emailReportFile = new File(reportPath, "emailable-report.html");

      htmlReporter = new ExtentHtmlReporter(reportFile);
      EmailReporter emailReporter = new EmailReporter(emailReportFile);
      reporter = new ExtentReports();
      //        如果cdn.rawgit.com访问不了,可以设置为:ResourceCDN.EXTENTREPORTS或者ResourceCDN.GITHUB
      htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
      reporter.attachReporter(htmlReporter, emailReporter);
  }
重写 onstart 方法

新建一个类名为MyReporter,一个静态ExtentTest的引用。

Listener 包下 MyReporter.java

public class MyReporter { public static ExtentTest report; }

MyExtentTestNgFormatter.java

public void onStart(ITestContext iTestContext) {
        ISuite iSuite = iTestContext.getSuite();
        ExtentTest suite = (ExtentTest) iSuite.getAttribute(SUITE_ATTR);
        ExtentTest testContext = suite.createNode(iTestContext.getName());
        // 将MyReporter.report静态引用赋值为testContext。
        // testContext是@Test每个测试用例时需要的。report.log可以跟随具体的测试用例。另请查阅源码。
        MyReporter.report = testContext;
        iTestContext.setAttribute("testContext", testContext);
    }
自定义配置

测试报告默认是在工程根目录下创建 test-output/ 文件夹下,名为 report.htmlemailable-report.html。可根据各自需求在构造方法中修改。

public MyExtentTestNgFormatter() {
        setInstance(this);
        testRunnerOutput = new ArrayList<>();
        // reportPath报告路径
        String reportPathStr = System.getProperty("reportPath");
        File reportPath;

        try {
            reportPath = new File(reportPathStr);
        } catch (NullPointerException e) {
            reportPath = new File(TestNG.DEFAULT_OUTPUTDIR);
        }

        if (!reportPath.exists()) {
            if (!reportPath.mkdirs()) {
                throw new RuntimeException("Failed to create output run directory");
            }
        }
        // 报告名report.html
        File reportFile = new File(reportPath, "report.html");
        // 邮件报告名emailable-report.html
        File emailReportFile = new File(reportPath, "emailable-report.html");

        htmlReporter = new ExtentHtmlReporter(reportFile);
        EmailReporter emailReporter = new EmailReporter(emailReportFile);
        reporter = new ExtentReports();
        reporter.attachReporter(htmlReporter, emailReporter);
    }
report.log

report.log 支持多种玩法

// 根据状态不同添加报告。型如警告 MyReporter.report.log(Status.WARNING, "接口耗时(ms):" + String.valueOf(time));

直接从TestClass 中运行时会报 MyReporter.report 的空指针错误,需做判空处理。

完整代码
package com.ruoyi.listener;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.ResourceCDN;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.vimalselvam.testng.EmailReporter;
import com.vimalselvam.testng.NodeName;
import com.vimalselvam.testng.SystemInfo;
import com.vimalselvam.testng.listener.ExtentTestNgFormatter;
import org.testng.*;
import org.testng.xml.XmlSuite;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MyExtentTestNgFormatter extends ExtentTestNgFormatter {
    private static final String REPORTER_ATTR = "extentTestNgReporter";
    private static final String SUITE_ATTR = "extentTestNgSuite";
    private ExtentReports reporter;
    private List<String> testRunnerOutput;
    private Map<String, String> systemInfo;
    private ExtentHtmlReporter htmlReporter;

    private static ExtentTestNgFormatter instance;

    public MyExtentTestNgFormatter() {
        setInstance(this);
        testRunnerOutput = new ArrayList<>();
        // reportPath 报告路径
        String reportPathStr = System.getProperty("reportPath");
        File reportPath;

        try {
            reportPath = new File(reportPathStr);
        } catch (NullPointerException e) {
            reportPath = new File(TestNG.DEFAULT_OUTPUTDIR);
        }

        if (!reportPath.exists()) {
            if (!reportPath.mkdirs()) {
                throw new RuntimeException("Failed to create output run directory");
            }
        }
        //  报告名report.html
        File reportFile = new File(reportPath, "report.html");
		// 邮件报告名emailable-report.html
        File emailReportFile = new File(reportPath, "emailable-report.html");

        htmlReporter = new ExtentHtmlReporter(reportFile);
        EmailReporter emailReporter = new EmailReporter(emailReportFile);
        reporter = new ExtentReports();
	//  如果cdn.rawgit.com访问不了,可以设置为:ResourceCDN.EXTENTREPORTS或者ResourceCDN.GITHUB
        htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
        reporter.attachReporter(htmlReporter, emailReporter);
    }

    /**
     * Gets the instance of the {@link ExtentTestNgFormatter}
     *
     * @return The instance of the {@link ExtentTestNgFormatter}
     */
    public static ExtentTestNgFormatter getInstance() {
        return instance;
    }

    private static void setInstance(ExtentTestNgFormatter formatter) {
        instance = formatter;
    }

    /**
     * Gets the system information map
     *
     * @return The system information map
     */
    public Map<String, String> getSystemInfo() {
        return systemInfo;
    }

    /**
     * Sets the system information
     *
     * @param systemInfo The system information map
     */
    public void setSystemInfo(Map<String, String> systemInfo) {
        this.systemInfo = systemInfo;
    }

    public void onStart(ISuite iSuite) {
        if (iSuite.getXmlSuite().getTests().size() > 0) {
            ExtentTest suite = reporter.createTest(iSuite.getName());
            String configFile = iSuite.getParameter("report.config");

            if (!Strings.isNullOrEmpty(configFile)) {
                htmlReporter.loadXMLConfig(configFile);
            }

            String systemInfoCustomImplName = iSuite.getParameter("system.info");
            if (!Strings.isNullOrEmpty(systemInfoCustomImplName)) {
                generateSystemInfo(systemInfoCustomImplName);
            }

            iSuite.setAttribute(REPORTER_ATTR, reporter);
            iSuite.setAttribute(SUITE_ATTR, suite);
        }
    }

    private void generateSystemInfo(String systemInfoCustomImplName) {
        try {
            Class<?> systemInfoCustomImplClazz = Class.forName(systemInfoCustomImplName);
            if (!SystemInfo.class.isAssignableFrom(systemInfoCustomImplClazz)) {
                throw new IllegalArgumentException("The given system.info class name <" + systemInfoCustomImplName +
                        "> should implement the interface <" + SystemInfo.class.getName() + ">");
            }

            SystemInfo t = (SystemInfo) systemInfoCustomImplClazz.newInstance();
            setSystemInfo(t.getSystemInfo());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            throw new IllegalStateException(e);
        }
    }

    public void onFinish(ISuite iSuite) {
    }


    public void onTestStart(ITestResult iTestResult) {
        MyReporter.setTestName(iTestResult.getName());
    }

    public void onTestSuccess(ITestResult iTestResult) {

    }

    public void onTestFailure(ITestResult iTestResult) {

    }

    public void onTestSkipped(ITestResult iTestResult) {

    }

    public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {

    }

    public void onStart(ITestContext iTestContext) {
        ISuite iSuite = iTestContext.getSuite();
        ExtentTest suite = (ExtentTest) iSuite.getAttribute(SUITE_ATTR);
        ExtentTest testContext = suite.createNode(iTestContext.getName());
		// 自定义报告
		// 将MyReporter.report 静态引用赋值为 testContext。
		// testContext 是 @Test每个测试用例时需要的。report.log可以跟随具体的测试用例。另请查阅源码。
        MyReporter.report = testContext;
        iTestContext.setAttribute("testContext", testContext);
    }

    public void onFinish(ITestContext iTestContext) {
        ExtentTest testContext = (ExtentTest) iTestContext.getAttribute("testContext");
        if (iTestContext.getFailedTests().size() > 0) {
            testContext.fail("Failed");
        } else if (iTestContext.getSkippedTests().size() > 0) {
            testContext.skip("Skipped");
        } else {
            testContext.pass("Passed");
        }
    }

    public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
        if (iInvokedMethod.isTestMethod()) {
            ITestContext iTestContext = iTestResult.getTestContext();
            ExtentTest testContext = (ExtentTest) iTestContext.getAttribute("testContext");
            ExtentTest test = testContext.createNode(iTestResult.getName(), iInvokedMethod.getTestMethod().getDescription());
            iTestResult.setAttribute("test", test);
        }
    }

    public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
        if (iInvokedMethod.isTestMethod()) {
            ExtentTest test = (ExtentTest) iTestResult.getAttribute("test");
            List<String> logs = Reporter.getOutput(iTestResult);
            for (String log : logs) {
                test.info(log);
            }

            int status = iTestResult.getStatus();
            if (ITestResult.SUCCESS == status) {
                test.pass("Passed");
            } else if (ITestResult.FAILURE == status) {
                test.fail(iTestResult.getThrowable());
            } else {
                test.skip("Skipped");
            }

            for (String group : iInvokedMethod.getTestMethod().getGroups()) {
                test.assignCategory(group);
            }
        }
    }

    /**
     * Adds a screen shot image file to the report. This method should be used only in the configuration method
     * and the {@link ITestResult} is the mandatory parameter
     *
     * @param iTestResult The {@link ITestResult} object
     * @param filePath    The image file path
     * @throws IOException {@link IOException}
     */
    public void addScreenCaptureFromPath(ITestResult iTestResult, String filePath) throws IOException {
        ExtentTest test = (ExtentTest) iTestResult.getAttribute("test");
        test.addScreenCaptureFromPath(filePath);
    }

    /**
     * Adds a screen shot image file to the report. This method should be used only in the
     * {@link org.testng.annotations.Test} annotated method
     *
     * @param filePath The image file path
     * @throws IOException {@link IOException}
     */
    public void addScreenCaptureFromPath(String filePath) throws IOException {
        ITestResult iTestResult = Reporter.getCurrentTestResult();
        Preconditions.checkState(iTestResult != null);
        ExtentTest test = (ExtentTest) iTestResult.getAttribute("test");
        test.addScreenCaptureFromPath(filePath);
    }

    /**
     * Sets the test runner output
     *
     * @param message The message to be logged
     */
    public void setTestRunnerOutput(String message) {
        testRunnerOutput.add(message);
    }

    public void generateReport(List<XmlSuite> list, List<ISuite> list1, String s) {
        if (getSystemInfo() != null) {
            for (Map.Entry<String, String> entry : getSystemInfo().entrySet()) {
                reporter.setSystemInfo(entry.getKey(), entry.getValue());
            }
        }
        reporter.setTestRunnerOutput(testRunnerOutput);
        reporter.flush();
    }

    /**
     * Adds the new node to the test. The node name should have been set already using {@link NodeName}
     */
    public void addNewNodeToTest() {
        addNewNodeToTest(NodeName.getNodeName());
    }

    /**
     * Adds the new node to the test with the given node name.
     *
     * @param nodeName The name of the node to be created
     */
    public void addNewNodeToTest(String nodeName) {
        addNewNode("test", nodeName);
    }

    /**
     * Adds a new node to the suite. The node name should have been set already using {@link NodeName}
     */
    public void addNewNodeToSuite() {
        addNewNodeToSuite(NodeName.getNodeName());
    }

    /**
     * Adds a new node to the suite with the given node name
     *
     * @param nodeName The name of the node to be created
     */
    public void addNewNodeToSuite(String nodeName) {
        addNewNode(SUITE_ATTR, nodeName);
    }

    private void addNewNode(String parent, String nodeName) {
        ITestResult result = Reporter.getCurrentTestResult();
        Preconditions.checkState(result != null);
        ExtentTest parentNode = (ExtentTest) result.getAttribute(parent);
        ExtentTest childNode = parentNode.createNode(nodeName);
        result.setAttribute(nodeName, childNode);
    }

    /**
     * Adds a info log message to the node. The node name should have been set already using {@link NodeName}
     *
     * @param logMessage The log message string
     */
    public void addInfoLogToNode(String logMessage) {
        addInfoLogToNode(logMessage, NodeName.getNodeName());
    }

    /**
     * Adds a info log message to the node
     *
     * @param logMessage The log message string
     * @param nodeName   The name of the node
     */
    public void addInfoLogToNode(String logMessage, String nodeName) {
        ITestResult result = Reporter.getCurrentTestResult();
        Preconditions.checkState(result != null);
        ExtentTest test = (ExtentTest) result.getAttribute(nodeName);
        test.info(logMessage);
    }

    /**
     * Marks the node as failed. The node name should have been set already using {@link NodeName}
     *
     * @param t The {@link Throwable} object
     */
    public void failTheNode(Throwable t) {
        failTheNode(NodeName.getNodeName(), t);
    }

    /**
     * Marks the given node as failed
     *
     * @param nodeName The name of the node
     * @param t        The {@link Throwable} object
     */
    public void failTheNode(String nodeName, Throwable t) {
        ITestResult result = Reporter.getCurrentTestResult();
        Preconditions.checkState(result != null);
        ExtentTest test = (ExtentTest) result.getAttribute(nodeName);
        test.fail(t);
    }

    /**
     * Marks the node as failed. The node name should have been set already using {@link NodeName}
     *
     * @param logMessage The message to be logged
     */
    public void failTheNode(String logMessage) {
        failTheNode(NodeName.getNodeName(), logMessage);
    }

    /**
     * Marks the given node as failed
     *
     * @param nodeName   The name of the node
     * @param logMessage The message to be logged
     */
    public void failTheNode(String nodeName, String logMessage) {
        ITestResult result = Reporter.getCurrentTestResult();
        Preconditions.checkState(result != null);
        ExtentTest test = (ExtentTest) result.getAttribute(nodeName);
        test.fail(logMessage);
    }
}

class MyReporter {
    public static ExtentTest report;
    private static String testName;

    public static String getTestName() {
        return testName;
    }

    public static void setTestName(String testName) {
        MyReporter.testName = testName;
    }
}

Step-3:配置监听

在测试集合 testng.xml 文件中导入 Listener 监听类。

<listeners> 
    <listener class-name="com.zuozewei.extentreportdemo.listener.MyExtentTestNgFormatter"/> 
    </listeners>

Step-4:配置报告

extent reporters支持报告的配置。目前支持的配置内容有title、主题等。 先在 src/resources/目录下添加 config/report/extent-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<extentreports>
    <configuration>
        <timeStampFormat>yyyy-MM-dd HH:mm:ss</timeStampFormat>
        <!-- report theme -->
        <!-- standard, dark 个人喜好暗色 -->
        <theme>dark</theme>

        <!-- document encoding -->
        <!-- defaults to UTF-8 -->
        <encoding>UTF-8</encoding>

        <!-- protocol for script and stylesheets -->
        <!-- defaults to https -->
        <protocol>https</protocol>

        <!-- title of the document -->
        <documentTitle>接口自动化测试报告</documentTitle>

        <!-- report name - displayed at top-nav -->
        <reportName>接口自动化测试报告</reportName>

        <!-- report headline - displayed at top-nav, after reportHeadline -->
        <reportHeadline>接口自动化测试报告</reportHeadline>

        <!-- global date format override -->
        <!-- defaults to yyyy-MM-dd -->
        <dateFormat>yyyy-MM-dd</dateFormat>

        <!-- global time format override -->
        <!-- defaults to HH:mm:ss -->
        <timeFormat>HH:mm:ss</timeFormat>

        <!-- custom javascript -->
        <scripts>
            <![CDATA[
        $(document).ready(function() {

        });
      ]]>
        </scripts>

        <!-- custom styles -->
        <styles>
            <![CDATA[

      ]]>
        </styles>
    </configuration>
</extentreports>

Step-5:配置系统

config下新建 MySystemInfo类继承 SystemInfo 接口

public class MySystemInfo implements SystemInfo {
    @Override
    public Map<String, String> getSystemInfo() {

        Map<String, String> systemInfo = new HashMap<>();
        systemInfo.put("测试人员", "zuozewei");

        return systemInfo;
    }
}

可用于添加系统信息,例如:db的配置信息,人员信息,环境信息等。根据项目实际情况添加。

至此,extentreports 美化报告完成。

Step-6:添加测试用例

public class TestMethodsDemo {

    @Test
    public void test1(){
        Assert.assertEquals(1,2);
    }

    @Test
    public void test2(){
        Assert.assertEquals(1,1);
    }


    @Test
    public void test3(){
        Assert.assertEquals("aaa","aaa");
    }


    @Test
    public void logDemo(){
        Reporter.log("这是故意写入的日志");
        throw new RuntimeException("故意运行时异常");
    }
}

Step-7:测试用例suite

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="测试demo" verbose="1" preserve-order="true">
    <parameter name="report.config" value="src/main/resources/report/extent-config.xml"/>
    <parameter name="system.info" value="com.zuozewei.extentreportdemo.config.MySystemInfo"/>

    <test name="测试demo" preserve-order="true">
        <classes>
            <class name="com.zuozewei.extentreportdemo.testCase.TestMethodsDemo"/>
        </classes>
    </test>

    <listeners>
        <listener class-name="com.zuozewei.extentreportdemo.listener.MyExtentTestNgFormatter"/>
    </listeners>
</suite>

测试报告

HTML Resport 示例

Email Report 示例

工程目录

本文源码:

https://github.com/7DGroup/Java-API-Test-Examples

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/401840.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

十七、多线程

一、目标 理解线程的概念掌握线程的创建和启动了解线程的状态掌握线程调度的常用方法掌握线程的同步理解线程安全的类型 二、进程、线程、多线程的理解 进程&#xff1a;应用程序的执行实例、有独立的内存空间和系统资源 线程&#xff1a;CPU调度和分派的基本单位、进程中执行运…

2023数据要素市场十大关键词

2023数据要素市场十大关键词 导读 2023年即将过去。一年之前&#xff0c;《中共中央国务院关于构建数据基础制度更好发挥数据要素作用的意见》&#xff08;简称“数据二十条”&#xff09;正式对外发布&#xff0c;为数据要素市场的建设举旗定向。 图片 2023年是“数据二十条…

抖店开通后的这些基础搭建,你了解吗?今天一文详解!

大家好&#xff0c;我是电商小布。 很多小伙伴在我们店铺开通后&#xff0c;接下来就会进行选品上架等工作。 但其实&#xff0c;在店铺刚开通时&#xff0c;小店的基础设置是并不完善的。 比如说&#xff1a;平台默认店铺是全地区包邮的。 想要让小店顺利运转&#xff0c;…

徐晓艺被波兰前总统布罗尼斯瓦夫·科莫罗夫斯基接见

2024年1月19日,科莫罗夫斯基阁下总统俱乐部全球主席总统有话说共同主席波兰第五任总统布罗尼斯瓦夫科莫罗夫斯基 Former President of Poland莅临北京丰台宴 科莫罗夫斯基总统阁下一生充满传奇,他的外交成就也颇为杰出,其中一项就是中波关系。他说:“我作为总统在2011年对华访…

vue3 toRefs之后的变量修改方法

上效果 修改值需要带上解构之前的对象名obj&#xff0c; changeName:()>{ // toRefs 解决后变量修改值方法&#xff1a; 解构前变量.字段新值 obj.name FEIFEI; } } 案例源码 <!DOCTYPE html> <html> <head><me…

【Azure 架构师学习笔记】- Azure Databricks (10) -- UC 使用

本文属于【Azure 架构师学习笔记】系列。 本文属于【Azure Databricks】系列。 接上文 【Azure 架构师学习笔记】- Azure Databricks (9) – UC权限 在前面的文章&#xff1a;【Azure 架构师学习笔记】- Azure Databricks (6) - 配置Unity Catalog中演示了如何配置一个UC。 本文…

【Vuforia+Unity】AR04-地面、桌面平面识别功能

不论你是否曾有过相关经验&#xff0c;只要跟随本文的步骤&#xff0c;你就可以成功地创建你自己的AR应用。 官方教程Ground Plane in Unity | Vuforia Library 这个功能很棒&#xff0c;但是要求也很不友好&#xff0c;只能支持部分移动设备&#xff0c;具体清单如下&#xf…

书生·浦语大模型实战营第六节课作业

基础作业 python run.py --datasets ceval_gen --hf-path /root/model/Shanghai_AI_Laboratory/internlm2-chat-7b/ --tokenizer-path /root/model/Shanghai_AI_Laboratory/internlm2-chat-7b/ --tokenizer-kwargs padding_sideleft truncationleft trust_remote_codeTrue --m…

栽花-第15届蓝桥第4次STEMA测评Scratch真题精选

[导读]&#xff1a;超平老师的《Scratch蓝桥杯真题解析100讲》已经全部完成&#xff0c;后续会不定期解读蓝桥杯真题&#xff0c;这是Scratch蓝桥杯真题解析第169讲。 第15届蓝桥杯第4次STEMA测评已于2024年1月28日落下帷幕&#xff0c;编程题一共有6题&#xff0c;分别如下&a…

HarmonyOS—添加/删除Module

Module是应用/服务的基本功能单元&#xff0c;包含了源代码、资源文件、第三方库及应用/服务配置文件&#xff0c;每一个Module都可以独立进行编译和运行。一个HarmonyOS应用/服务通常会包含一个或多个Module&#xff0c;因此&#xff0c;可以在工程中创建多个Module&#xff0…

什么是web组态?

一、web组态的定义和背景 在深入探讨之前&#xff0c;我们先回顾一下“组态”的定义。在工业自动化领域&#xff0c;组态软件是用于创建监控和数据采集&#xff08;SCADA&#xff09;系统的工具&#xff0c;它允许工程师构建图形界面&#xff0c;实现与各种设备和机器的数据交互…

性能全面提升!探索ONLYOFFICE最新8.0版:更快速、更强大,PDF表单编辑轻松搞定!

文章目录 PDF表单功能表单模板 屏幕朗读器功能EXCEL新增功能单变量求解图表向导数字排序 PPT 新增功能新增语言区域设置和优化插件界面 ONLYOFFICE 是由 Ascensio System SIA 推出的一款功能强大的办公套件&#xff0c;其中提供了适用于文本文档、表格以及演示文稿的在线编辑软…

通过盲注脚本复习sqllabs第46关order by 注入

在MySQL支持使用ORDER BY语句对查询结果集进行排序处理&#xff0c;使用ORDER BY语句不仅支持对单列数据的排序&#xff0c;还支持对数据表中多列数据的排序。语法格式如下 select * from 表名 order by 列名(或者数字) asc&#xff1b;升序(默认升序) select * from 表名 or…

win10系统secoclient连接服务器时,报错与对方建立连接超时,配置错误或网络故障

故障原因 secoclient连接时出现超时的故障&#xff0c;之前还是正常的&#xff0c;可能与最近的系统更新有关 解决方案 找到设备管理 找到网络适配器下的SVN adapter V1.0 禁用该适配器 进入C:\Windows\System32\drivers 找到SVNDrv.sys 把这个文件删除或者重命名一下…

解决docker中运行的jar包连不上前端程序

目录 检查端口映射 查看容器的 IP 地址 检查容器网络设置 防火墙和网络策略 前端程序配置 跨域资源共享 (CORS) 日志查看 连接问题通常涉及到网络配置和端口映射。确保你在 Docker 中运行的 JAR 包可以被前端程序访问&#xff0c;可以采取以下步骤来解决问题&#xff1a…

信钰证券|A股IPO失意后转道南下,内地企业成港股上市“主力军”

内地企业已经成为赴港上市的主力。 Wind数据闪现&#xff0c;本年以来到2月21日&#xff0c;在港股初度聆讯的19家公司中&#xff0c;作业地址在内地的有18家&#xff0c;只要一家作业地址在我国香港。此外&#xff0c;本年在港股上市的5家企业&#xff0c;首要作业地址也均在…

【计组】计算机体系结构

1.CPU的组成 1.1 运算器 算术逻辑单元&#xff08;ALU&#xff09;&#xff1a;逻辑运算累加寄存器&#xff08;AC&#xff09;&#xff1a;存储算数运算结果&#xff08;包括中间结果&#xff09;数据缓冲寄存器&#xff08;DR&#xff09;&#xff1a;临时存储从内存中读取…

星河做市基金会全球DAO社区启动,为数字货币市场注入新活力

2024年的数字货币市场即将迎来一次重要的历史性时刻 — 比特币减半&#xff0c;这四年一次的事件将成为全球数字资产市场的焦点&#xff0c;预示着新一轮的牛市浪潮即将到来。在这个关键时刻&#xff0c;星河做市基金会展现出其作为区块链行业领先市值管理公司的独特魅力。 GA…

JVM虚拟机结构

虚拟机结构图 从图中看出&#xff1a; JVM虚拟机主要有三大部分组成&#xff1a; 1. 类加载器 2. JVM运行时内存 3. 执行引擎 一、类加载器 类加载器主要用来加载字节码文件&#xff08;.class&#xff09;到内存中 二、内存结构 如图&#xff1a;可将内存分为两大部分&…

【MATLAB】mlptdenoise信号分解+FFT傅里叶频谱变换组合算法

有意向获取代码&#xff0c;请转文末观看代码获取方式~ 展示出图效果 1 mlptdenoise分解算法 MLPT denoise&#xff08;Maximum Likelihood Parameter-Tuned Denoise&#xff09;是一种基于小波变换的信号分解算法&#xff0c;它可以将信号分解为多个具有不同频率特性的小波分…
最新文章