testng生成报告ReportNG美化测试报告

摘要:
testng生成报告ReportNG美化测试报告testng生成报告ReportNG美化测试报告ReportNG是一个配合TestNG运行case后自动帮你在test-output文件内生成一个相对较为美观的测试报告!--Suite--˃4,然后运行testNG.XML,再看test-output文件夹里面的html文件下面的index.html报错信息:你自己System.out的东西都可以写到这里:如果你的报告是乱码,那么你不要急,方法在下面:在使用ReportNG替换TestNG自带报告时如果报告中含中文,则会乱码,很是不爽,所以把ReportNG的源码下载下来调试。

testng生成报告ReportNG美化测试报告

testng生成报告ReportNG美化测试报告

ReportNG 是一个配合TestNG运行case后自动帮你在test-output文件内生成一个相对较为美观的测试报告!
ReportNG 里面Log 是不支持中文的,我改过ReportNG.jar源码,具体方法看最下面,也可以找我直接要jar!
话不多说直接上

环境准备:
1,你需要这些架包

testng生成报告ReportNG美化测试报告第1张

解释:有人会问现在ReportNG版本不是1.1.4吗?为什么我这里是1.1.5呢,这里是因为我改过这里面的源码,(为什么要改源码?后面告诉你)
修复ReportNG中文乱码问题包下载地址:地址
2,先写一个简单的case,比如打开百度,下面这个代码看上去不难吧!这是第二步前提是你能运行它

packageTest;
importorg.junit.After;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
importorg.openqa.selenium.firefox.FirefoxProfile;
importorg.testng.annotations.Test;
public classcase1 {
    WebDriver driver;
    @Test
    public void Open() throwsInterruptedException {
        System.setProperty("webdriver.friefox.bin","C:\Program Files\Mozilla Firefox\friefox.exe");
        FirefoxProfile fp = newFirefoxProfile();
        WebDriver driver = newFirefoxDriver(fp);
        driver.get("http://www.baidu.com");
        driver.findElement(By.id("kw")).sendKeys("testerhome");
    }
    @Test
    public void Open() throwsInterruptedException {
        System.setProperty("webdriver.friefox.bin","C:\Program Files\Mozilla Firefox\friefox.exe");
        FirefoxProfile fp = newFirefoxProfile();
        WebDriver driver = newFirefoxDriver(fp);
        driver.get("http://www.baidu.com");
        driver.findElement(By.id("kw")).sendKeys("testerhome");
        Reporter.log("测试1通过");
    }
    @Test
    public void Open1() throwsInterruptedException {
        System.setProperty("webdriver.friefox.bin","C:\Program Files\Mozilla Firefox\friefox.exe");
        FirefoxProfile fp = newFirefoxProfile();
        WebDriver driver = newFirefoxDriver(fp);
        driver.get("http://www.baidu.com");
        driver.findElement(By.id("kw")).sendKeys("testerhome");
        Reporter.log("测试2通过");
    }
    @Test
    public void Open2() throwsInterruptedException {
        System.setProperty("webdriver.friefox.bin","C:\Program Files\Mozilla Firefox\friefox.exe");
        FirefoxProfile fp = newFirefoxProfile();
        WebDriver driver = newFirefoxDriver(fp);
        driver.get("http://www.baidu.com");
        driver.findElement(By.id("k1w")).sendKeys("testerhome");
        Reporter.log("测试3通过");
    }
    @After
    public void quit() throwsInterruptedException {
        driver.quit();
    }
}

3,配置testNG.xml,这个文件是testNG的配置文件,

<?xml version="1.0" encoding="UTF-8"?>
<suite name="test" parallel="true">
<test name="test" preserver-order="true">
<classes>
//你也可以多个
<class name="包名.case名字"/>
<class name="包名.case名字"/>
<class name="包名.case名字"/>
</classes>
<listeners>
//这是你需要加的东西
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>
</test> <!-- Test -->
</suite> <!-- Suite -->

4,然后运行testNG.XML ,再看test-output文件夹 里面的 html文件下面的index.html

testng生成报告ReportNG美化测试报告第2张

报错信息:

testng生成报告ReportNG美化测试报告第3张

你自己System.out的东西都可以写到这里:

testng生成报告ReportNG美化测试报告第4张
testng生成报告ReportNG美化测试报告第5张
testng生成报告ReportNG美化测试报告第6张

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

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

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

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

protected voidgenerateFile(File file,  String templateName,  VelocityContext context) throwsException{
        Writer writer = new BufferedWriter(newFileWriter(file));
        try
        {
            Velocity.mergeTemplate(classpathPrefix +templateName,
                                   ENCODING,
                                   context,
                                   writer);
            writer.flush();
        }
        finally
        {
            writer.close();
        }
    }

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

protected voidgenerateFile(File file,  String templateName,  VelocityContext context) throwsException{
        //Writer writer = new BufferedWriter(new FileWriter(file)); 
        //encoding to utf-8
        OutputStream out=newFileOutputStream(file);
        Writer writer = new BufferedWriter(new OutputStreamWriter(out,"utf-8"));
        try
        {
            Velocity.mergeTemplate(classpathPrefix +templateName,ENCODING,context,writer);
            writer.flush();
        }
        finally
        {
            writer.close();
        }
    }

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

免责声明:文章转载自《testng生成报告ReportNG美化测试报告》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇vue-cli查看打包后的dist文件统计学习方法 李航---第5章 决策树下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

matlab 工具之各种降维方法工具包,下载及使用教程,有PCA, LDA, 等等。。。

最近跑深度学习,提出的feature是4096维的,放到我们的程序里,跑得很慢,很慢。。。。 于是,一怒之下,就给他降维处理了,但是matlab 自带的什么pca( ), princomp( )函数,搞不清楚怎么用的,表示不大明白,下了一个软件包: 名字:Matlab Toolbox for Dimensionality Reduction 链接:http...

Webpack的使用

webpack的使用 在项目中安装和配置webpack 1、运行npm install webpack webpack-cli -D命令 安装webpack相关的包 2、在项目的根目录中,创建webpack.config.js的webpack 配置文件 3、在webpack 的配置文件中初始化如下基本配置  module.exports={     mod...

使用gpg来加密数据

一、数据的加密方式 数据加密有三种方式: 1、对称加密(算法有:DES、AES、3DES、)加密和解密使用同一个密钥 2、非对称加密(RSA、DSA、ELGamal等等)一共四把钥匙,用公钥加密数据,只能使用与之配对的私钥解密;反之亦然 3、单项加密(md5  sha1 sha2 sha128 sha256 sha512等)算出数据的hash值,当数据发生...

MongoDB update数据语法

在前面的文章“mongodb 查询的语法”里,我介绍了Mongodb的常用查询语法,Mongodb的update操作也有点复杂,我结合自己的使用经验,在这里介绍一下,给用mongodb的朋友看看,也方便以后自己用到的时候查阅:注:在这篇文章及上篇文章内讲的语法介绍都是在mongodb shell环境内的,和真正运用语言编程(如java,php等)使用时,在...

Ubuntu 环境变量

环境变量配置文件 在Ubuntu中有如下几个文件可以设置环境变量 1、/etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行。 2、/etc/environment:在登录时操作系统使用的第二个文件,系统在读取你自己的profile前,设置环境文件的环境变量。 3、~...

假设检验(Hypothesis Testing)

假设检验的定义 假设检验:先对总体参数提出某种假设,然后利用样本数据判断假设是否成立。在逻辑上,假设检验采用了反证法,即先提出假设,再通过适当的统计学方法证明这个假设基本不可能是真的。(说“基本”是因为统计得出的结果来自于随机样本,结论不可能是绝对的,所以我们只能根据概率上的一些依据进行相关的判断。) 假设检验依据的是小概率思想,即小概率事件在一次试验中基...