testng生成報告ReportNG美化測試報告
testng生成報告ReportNG美化測試報告
ReportNG 是一個配合TestNG運行case后自動幫你在test-output文件內生成一個相對較為美觀的測試報告!
ReportNG 里面Log 是不支持中文的,我改過ReportNG.jar源碼,具體方法看最下面,也可以找我直接要jar!
話不多說直接上
環境准備:
1,你需要這些架包
解釋:有人會問現在ReportNG版本不是1.1.4嗎?為什么我這里是1.1.5呢,這里是因為我改過這里面的源碼,(為什么要改源碼?后面告訴你)
修復ReportNG中文亂碼問題包下載地址:地址
2,先寫一個簡單的case,比如打開百度,下面這個代碼看上去不難吧!這是第二步前提是你能運行它
package Test; import org.junit.After; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.testng.annotations.Test; public class case1 { WebDriver driver; @Test public void Open() throws InterruptedException { System.setProperty("webdriver.friefox.bin","C:\\Program Files\\Mozilla Firefox\\friefox.exe"); FirefoxProfile fp = new FirefoxProfile(); WebDriver driver = new FirefoxDriver(fp); driver.get("http://www.baidu.com"); driver.findElement(By.id("kw")).sendKeys("testerhome"); } @Test public void Open() throws InterruptedException { System.setProperty("webdriver.friefox.bin","C:\\Program Files\\Mozilla Firefox\\friefox.exe"); FirefoxProfile fp = new FirefoxProfile(); WebDriver driver = new FirefoxDriver(fp); driver.get("http://www.baidu.com"); driver.findElement(By.id("kw")).sendKeys("testerhome"); Reporter.log("測試1通過"); } @Test public void Open1() throws InterruptedException { System.setProperty("webdriver.friefox.bin","C:\\Program Files\\Mozilla Firefox\\friefox.exe"); FirefoxProfile fp = new FirefoxProfile(); WebDriver driver = new FirefoxDriver(fp); driver.get("http://www.baidu.com"); driver.findElement(By.id("kw")).sendKeys("testerhome"); Reporter.log("測試2通過"); } @Test public void Open2() throws InterruptedException { System.setProperty("webdriver.friefox.bin","C:\\Program Files\\Mozilla Firefox\\friefox.exe"); FirefoxProfile fp = new FirefoxProfile(); WebDriver driver = new FirefoxDriver(fp); driver.get("http://www.baidu.com"); driver.findElement(By.id("k1w")).sendKeys("testerhome"); Reporter.log("測試3通過"); } @After public void quit() throws InterruptedException { 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
報錯信息:
你自己System.out的東西都可以寫到這里:


如果你的報告是亂碼,那么你不要急,方法在下面:
在使用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(); } }
這樣生成的報告就不會亂碼了。