zoukankan      html  css  js  c++  java
  • Mac Eclipse+Maven+TestNg+ReportNg 生成测试报告

    1. TestNG 是java 的单元测试框架,功能很强大,很方便,但是自动生成的测试报告有待改善,可以使用TestNg 自带的TestNG_xslt更改TestNG报告的样式,这里主要讲解ReportNg,美化下TestNG 的报告

      TestNg(TestNg官网):
      http://testng.org/doc/index.html

      ReportNg(ReportNg官网):
      http://reportng.uncommons.org/

      mavenTestNg(在Maven下配置TestNg):
      http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html

      mavenReportNg(在Maven下配置ReportNg 需要翻墙了):
      https://solidsoft.wordpress.com/2011/01/23/better-looking-html-test-reports-for-testng-with-reportng-maven-guide/

      ReportNg 官方提供的可以使用Ant Build 项目,同样也可使用maven,Gradle,这是使用maven


      1. 首先确定已经建好了maven 项目,已经添加了TestNg类,生成了testNG.xml,pom.xml

      2. 在项目下建一个res文件夹用来统一存放我们的testNg.xml文件,方便运行不同的testNg.xml (使用maven运行的时候,只需要在Pom.xml修改引用不同的testNg.xml即可)
        结构如下:
        这里写图片描述

      3. 修改maven 的Pom文件如下:

        <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>MavenTestNg</groupId>
          <artifactId>MavenTestNg</artifactId>
          <version>0.0.1-SNAPSHOT</version>
        
          <!-- maven 运行测试name -->
           <name>Report_Test</name>
           <url>http://maven.apache.org</url>
        
        
           <!-- maven 引用远程库 -->
             <repositories>
            <repository>
                <id>java-net</id>
                <url>http://download.java.net/maven/2</url>
            </repository>
            </repositories>
        
        
        
           <!-- maven 参数配置,这里引用不同的testng.xml -->
            <properties>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <xmlFileName>testng.xml</xmlFileName>
            </properties>
        
        
          <!-- maven 引用依赖不同的jar -->
          <dependencies>
        
            <!-- 依赖testNg -->
            <dependency>
              <groupId>org.testng</groupId>
              <artifactId>testng</artifactId>
              <version>6.8.8</version>
              <scope>test</scope>
            </dependency>
        
            <!-- 依赖reportNg 关联testNg-->
            <dependency>
                <groupId>org.uncommons</groupId>
                <artifactId>reportng</artifactId>
                <version>1.1.4</version>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.testng</groupId>
                        <artifactId>testng</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        
            <!-- 依赖Guice -->
            <dependency>
                <groupId>com.google.inject</groupId>
                <artifactId>guice</artifactId>
                <version>3.0</version>
                <scope>test</scope>
            </dependency>
          </dependencies>
        
        
        
        
          <build>
          <plugins>
                <!-- 添加插件 关联testNg.xml -->
                <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-surefire-plugin</artifactId>
                      <version>2.17</version>
                      <configuration>
                      <suiteXmlFiles>
                          <suiteXmlFile>res/${xmlFileName}</suiteXmlFile>
                      </suiteXmlFiles>
                      </configuration>
                </plugin> 
        
               <!-- 添加插件,添加ReportNg的监听器,修改最后的TestNg的报告 -->
               <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.5</version>
                    <configuration>
                        <properties>
                            <property>
                                <name>usedefaultlisteners</name>
                                <value>false</value>
                            </property>
                            <property>
                                <name>listener</name>                      
                                <value>org.uncommons.reportng.HTMLReporter,  org.uncommons.reportng.JUnitXMLReporter</value>
                            </property>
                        </properties>
                        <workingDirectory>target/</workingDirectory>
                        <forkMode>always</forkMode>
                    </configuration>
                </plugin>
          </plugins>
        </build>
        </project>

        这样基本就可以了,修改完pom文件,会看到jar已经依赖完成
        这里写图片描述

      4. testNg.xml 不需要修改

      5. 之后右键运行pom.xml,选择maven test 即可
      6. 之后查看控制台如图:
        这里写图片描述
      7. 使用maven 插件运行之后,在target 里面查看测试报告,Html下的index.html 就是reportNg 生成的报告
        这里写图片描述

    2. 这样就可以完成了,除了ReportNg ,可以美化TestNg 的报告以外,还有testNG_xslt

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    【Python 开发】第三篇:python 实用小工具
    【Python 开发】第二篇 :Python安装
    【Linux 运维】 安装PHP工具Composer
    【shell 每日一练6】初始化安装Mysql并修改密码
    【zabbix 监控】第三章 创建主机组和主机
    【Docker】第一篇 Docker的初始化安装部署
    【shell 练习5】编写简单的多级菜单
    【zabbix 监控】第二章 安装测试被监控主机
    【zabbix 监控】第一章 zabbix的安装配置
    1数组和字符串题解
  • 原文地址:https://www.cnblogs.com/xinleishare/p/4793558.html
Copyright © 2011-2022 走看看