整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

一款很酷炫的自动化测试报告框架 ExtentReport

、引言

在本文中,我将详细介绍如何将 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); 

具体代码如下:

ublic 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 方法

重写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 的空指针错误,需做判空处理。

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>QA-接口自动化测试报告</documentTitle>

        <!-- report name - displayed at top-nav -->
        <reportName>QA-接口自动化测试报告</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>

四、测试报告

1、HTML Resport 示例

2、Email Report 示例

五、工程目录

源码地址:

  • https://github.com/zuozewei/blog-example/tree/master/Java-api-test/05-testreport/springboot-extentreport-demo1

介:

最新简约美观的网址网站引导页HTML源码 带一言 随机大图

图片:

  1. 于 HTML 性能:在编写 HTML 时,我们应该尽可能地使用最少的 HTML 标签来构建页面,这样可以减少页面的加载时间,提高页面的性能。同时,也要注意在 HTML 中嵌套的深度不要太深,尽量减少嵌套的层级,避免重复加载数据。
  2. 标签的作用:标签用于定义文档中的不同部分,比如头部、主体、尾部等。在编写 HTML 时,我们应该尽可能地使用标签来组织页面内容,这样可以提高代码的可读性和维护性。
  3. 标签的嵌套:在 HTML 中,标签之间可以嵌套使用,这样可以提高代码的复用性和可维护性。但是,在嵌套时要注意层级关系,不要过深,否则会影响页面的性能。
  4. 盒模型:盒模型是指一个元素在浏览器中占据的空间大小,包括内容、内边距和边框等部分。盒模型包含以下几个关键部分:
  • content area(内容区域)
  • padding(内边距)
  • border(边框)
  • margin(外边距)
  1. 合理使用 CSS 样式:在编写 HTML 时,我们应该尽可能地使用简洁、规范的 CSS 样式来布局页面内容。这样可以减少页面的加载时间,提高页面的性能。同时,也要注意不要使用过多的 CSS 属性和类,否则会影响页面的渲染效果和性能。
  2. 合理使用媒体查询:媒体查询是一种用于控制不同设备上显示不同样式的技术。在编写 HTML 时,我们可以使用媒体查询来根据设备的屏幕大小、分辨率等信息来调整页面的布局和样式。
  3. 压缩代码:在编写 HTML 时,我们可以使用压缩工具来压缩代码,这样可以减少页面的大小和加载时间,提高页面的性能。
  4. 使用工具:在编写 HTML 时,我们可以使用一些工具来帮助我们优化代码,比如开发者工具、浏览器开发者工具等。这些工具可以帮助我们快速定位代码中的问题和瓶颈,提高开发效率。
  5. 避免使用 Iframe
  6. 避免空链接属性
  7. 避免节点深层级嵌套
  8. 缩减 HTML 文档大小:提高下载速度最显而易见的方式就是减少文件的大小,特别是压缩内嵌在 HTML 文档中的 JavaScript 和

CSS 代码,这能使得页面体积大幅精简。除此之外减少 HTML 文档大小还可以采取下面几种方法:1.删掉 HTM 文档对执行结果无影响的空格空行和注释避免 Table 布局;2.使用 HTML5;3.显式指定文档字符集。

13.显式设置图片的宽高

14.避免脚本阻塞加载:

当浏览器在解析常规的 script 标签时,它需要等待 script 下载完毕,再解析执行,而后续的 HTML 代码只能等待。为了避免阻塞加载,应把脚步放到文档的末尾,如把 script 标签插入在 body 结束标签之前。

#挑战30天在头条写日记#