美文网首页
使用reportng打造美观的测试报告

使用reportng打造美观的测试报告

作者: 好多可乐 | 来源:发表于2020-04-24 00:28 被阅读0次

    在做自动化测试的时候,我们需要通过测试报告来展示验证结果。但是目前主流的testng报告比较不美观,所以这里推荐使用reportng来打造测试报告,下面是使用方法。

    一. 执行单个测试套件(需手动执行,不推荐)

    1,添加pom文件
     <dependency>
      <groupId>org.uncommons</groupId>
      <artifactId>reportng</artifactId>
      <version>1.1.4</version>
      <scope>test</scope>
     </dependency>
    
    2,在testng.xml添加监听器

    我们需要生成的测试报告是html格式的,所以我们使用org.uncommons.reportng.HTMLReporter来今天testng的单元测试用例的执行情况,然后把结果生成测试报告。

    1. 在项目的任何位置新建一个testng.xml的文件
    2. 添加测试case(完整路径)
    3. 添加监听器
    4. 选择xml文件,右键执行
    5. 到文件夹查看测试报告
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    <!--套件测试-->
    <suite name="test">
        <test name="reportng_config_test">
            <classes>
                <class name="com.kola.interface_auto.test.lesson53.testCase.TestCase"/>
            </classes>
        </test>
        <listeners>
            <listener class-name="org.uncommons.reportng.HTMLReporter"></listener>
        </listeners>
    
    </suite>
    

    ps:添加监听器执行脚本过程中可能会Injector类找不到
    解决方法:

    1. 添加依赖库
     <dependency>
       <groupId>com.google.inject</groupId>
       <artifactId>guice</artifactId>
       <version>3.0</version>
     </dependency>
    
    1. 更换稍高其他版本的testng

    执行完毕后,我们可以在本地的test-output文件夹查看测试报告

    本地文件路径.png

    打开如下:


    测试报告详情.png

    但是这个有个弊端,只能执行单个测试套件,如果我们需要执行多个,就不能处理了

    二. 执行多个测试套件

    为了后期继承Jenkins等自动化平台,我们必须保证脚本可以通过命令来执行。
    实现方式:集成maven的surefire插件。surefire插件用于maven的test阶段,以执行单元测试,集成后我们就可以通过maven命令--maven test来调用脚本执行了。

    1,添加pom文件

    ps:<suiteXmlFile>reportng_config.xml</suiteXmlFile>这里有几个套件就放几个,要注意别写错,要不然会找不到

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.7.1</version>
                    <configuration>
                        <systemPropertyVariables>
                            <org.uncommons.reportng.escape-output>false</org.uncommons.reportng.escape-output>
                        </systemPropertyVariables>
                        <testFailureIgnore>true</testFailureIgnore>
                        <argLine>
                            -Dfile.encoding=UTF-8
                        </argLine>
                        <suiteXmlFiles>
                            <suiteXmlFile>reportng_config.xml</suiteXmlFile>
                            <!--相对路径这样写:<suiteXmlFile>src/main/resources/testng.xml</suiteXmlFile>  -->
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
    
    2. 右键run maven-test

    可以在本地的文件目录下查看测试报告


    image.png

    相关文章

      网友评论

          本文标题:使用reportng打造美观的测试报告

          本文链接:https://www.haomeiwen.com/subject/lyvdvhtx.html