自动化测试报告(ReportNG)手把手教你

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

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


解释:有人会问现在ReportNG版本不是1.1.4吗?为什么我这里是1.1.5呢,这里是因为我改过这里面的源码,(为什么要改源码?后面告诉你)

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;publicclasscase1{WebDriverdriver;@TestpublicvoidOpen()throwsInterruptedException{System.setProperty("webdriver.friefox.bin","C:\\Program Files\\Mozilla Firefox\\friefox.exe");FirefoxProfilefp=newFirefoxProfile();WebDriverdriver=newFirefoxDriver(fp);driver.get("http://www.baidu.com");driver.findElement(By.id("kw")).sendKeys("testerhome");}@TestpublicvoidOpen()throwsInterruptedException{System.setProperty("webdriver.friefox.bin","C:\\Program Files\\Mozilla Firefox\\friefox.exe");FirefoxProfilefp=newFirefoxProfile();WebDriverdriver=newFirefoxDriver(fp);driver.get("http://www.baidu.com");driver.findElement(By.id("kw")).sendKeys("testerhome");Reporter.log("测试1通过");}@TestpublicvoidOpen1()throwsInterruptedException{System.setProperty("webdriver.friefox.bin","C:\\Program Files\\Mozilla Firefox\\friefox.exe");FirefoxProfilefp=newFirefoxProfile();WebDriverdriver=newFirefoxDriver(fp);driver.get("http://www.baidu.com");driver.findElement(By.id("kw")).sendKeys("testerhome");Reporter.log("测试2通过");}@TestpublicvoidOpen2()throwsInterruptedException{System.setProperty("webdriver.friefox.bin","C:\\Program Files\\Mozilla Firefox\\friefox.exe");FirefoxProfilefp=newFirefoxProfile();WebDriverdriver=newFirefoxDriver(fp);driver.get("http://www.baidu.com");driver.findElement(By.id("k1w")).sendKeys("testerhome");Reporter.log("测试3通过");}@Afterpublicvoidquit()throwsInterruptedException{driver.quit();}}



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

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

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

报错信息:


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

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

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

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

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

protectedvoidgenerateFile(Filefile,StringtemplateName,VelocityContextcontext)throwsException{Writerwriter=newBufferedWriter(newFileWriter(file));try{Velocity.mergeTemplate(classpathPrefix+templateName,ENCODING,context,writer);writer.flush();}finally{writer.close();}}

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

protectedvoidgenerateFile(Filefile,StringtemplateName,VelocityContextcontext)throwsException{//Writer writer = new BufferedWriter(new FileWriter(file));//encoding to utf-8OutputStreamout=newFileOutputStream(file);Writerwriter=newBufferedWriter(newOutputStreamWriter(out,"utf-8"));try{Velocity.mergeTemplate(classpathPrefix+templateName,ENCODING,context,writer);writer.flush();}finally{writer.close();}}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值