优化testng报告

1、在maven中的pom.xml中添加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>

<dependency>
       <groupId>com.google.inject</groupId>
       <artifactId>guice</artifactId>
       <version>4.0</version>
       <scope>test</scope>
 </dependency>

2、在tesgng.xml中添加listeners标签

<?xml version="1.0" encoding="UTF-8"?>
<suite name="自测demo" parallel="false">
    <parameter name="filePath" value="d:\app_testcase.xlsx"/>
    <parameter name="filePath2" value="d:\test01.xlsx"/>
  <test name="接口自动化">

    <listeners>
        <listener class-name = "org.uncommons.reportng.HTMLReporter"/>
        <listener class-name = "org.uncommons.reportng.JUnitXMLReporter"/>
    </listeners>

    <classes>
      <class name="com.api.run.TestRun"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

3、查看测试报告在test-output--->html---->index.html

 




来源:https://www.jianshu.com/p/e6fba73b2dd2


后记:

如果需要在报告出现自己输出的东西

在case里面使用下面的语句:

import org.testng.Reporter;


Reporter.log("测试1通过");

若中文乱码见下方操作:

如果你的报告是乱码,那么你不要急,方法在下面:

在使用ReportNG替换TestNG自带报告时如果报告中含中文,则会乱码,很是不爽,所以把ReportNG的源码下载下来调试。

原来以为是velocity模板的问题,结果对比发现模板没有任何问题,再通过跟踪生成报告过程的代码发现是在将模板文件替换后输出到页面时未转码导致的,修改方法如下:

修改AbstractReporter中的generateFile这个方法中的代码如下:
原来的代码是这样的:

复制代码
protected void generateFile(File file,  String templateName,  VelocityContext context) throws Exception{
        Writer writer = new BufferedWriter(new FileWriter(file));
        try
        {
            Velocity.mergeTemplate(classpathPrefix + templateName,
                                   ENCODING,
                                   context,
                                   writer);
            writer.flush();
        }
        finally
        {
            writer.close();
        }
    }
复制代码

修改成下面这样,然后编译好新的jar文件

复制代码
protected void generateFile(File file,  String templateName,  VelocityContext context) throws Exception{
        //Writer writer = new BufferedWriter(new FileWriter(file)); 
        //encoding to utf-8
        OutputStream out=new FileOutputStream(file);
        Writer writer = new BufferedWriter(new OutputStreamWriter(out,"utf-8"));
        try
        {
            Velocity.mergeTemplate(classpathPrefix + templateName,ENCODING,context,writer);

            writer.flush();
        }
        finally
        {
            writer.close();
        }
    }
复制代码

 这样生成的报告就不会乱码了。

参考: https://www.cnblogs.com/111testing/p/6980599.html

原文地址:https://www.cnblogs.com/kaibindirver/p/12156499.html