https://static.javadoc.io/org.testng/testng/6.13/org/testng/TestListenerAdapter.htmlhtml
TestListenerAdapter 一個簡單的ITestListener適配器,用於存儲全部運行的測試。 您能夠使用如下方法檢索這些結果:getPassedTests()getFailedTests()getSkippedTests() 若是您擴展此類以覆蓋任何這些方法,請記住若是您但願維護此測試列表,則調用它們的超級等效項。
IReporter 客戶端能夠實現此接口以生成報告。 它的方法generateReport()將在全部套件運行後調用,參數將提供在該運行期間發生的全部測試結果。
參考:java
https://www.cnblogs.com/relax-zw/p/9884009.html數據庫
http://bbs.51testing.com/thread-1161195-1-1.html app
IReporter接口是幹嗎的?就是讓用戶自定義報告的,不少人想要自定義報告,因而乎找各類插件,好比什麼testng-xslt啊,reportng啊,各類配置,最後出來的結果,還不能定製化,但爲何不能自已定 制一份呢?testng的IReporter接口就接供了這樣的功能,咱們只須要實現這個接口,並加上監聽,就 能拿到全部的信息了,你能夠把這些信息存數據庫,或者自已生成一個html,亦或者寫在EXCEL裏啊 等等,均可以,下面給出IReporter接口的實現方式: |
自定義報告ide
package com.demo; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Set; import org.testng.IReporter; import org.testng.IResultMap; import org.testng.ISuite; import org.testng.ISuiteResult; import org.testng.ITestContext; import org.testng.ITestResult; import org.testng.xml.XmlSuite; public class NewReport implements IReporter{ @Override public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) { List<ITestResult> list = new ArrayList<ITestResult>(); for (ISuite suite : suites) { Map<String, ISuiteResult> suiteResults = suite.getResults(); for (ISuiteResult suiteResult : suiteResults.values()) { ITestContext testContext = suiteResult.getTestContext(); IResultMap passedTests = testContext.getPassedTests(); IResultMap failedTests = testContext.getFailedTests(); IResultMap skippedTests = testContext.getSkippedTests(); IResultMap failedConfig = testContext.getFailedConfigurations(); list.addAll(this.listTestResult(passedTests)); list.addAll(this.listTestResult(failedTests)); list.addAll(this.listTestResult(skippedTests)); list.addAll(this.listTestResult(failedConfig)); } } this.sort(list); this.outputResult(list, outputDirectory+"/test.txt"); } private ArrayList<ITestResult> listTestResult(IResultMap resultMap){ Set<ITestResult> results = resultMap.getAllResults(); return new ArrayList<ITestResult>(results); } private void sort(List<ITestResult> list){ Collections.sort(list, new Comparator<ITestResult>() { @Override public int compare(ITestResult r1, ITestResult r2) { if(r1.getStartMillis()>r2.getStartMillis()){ return 1; }else{ return -1; } } }); } private void outputResult(List<ITestResult> list, String path){ try { BufferedWriter output = new BufferedWriter(new FileWriter(new File(path))); StringBuffer sb = new StringBuffer(); for (ITestResult result : list) { if(sb.length()!=0){ sb.append("\r\n"); } sb.append(result.getTestClass().getRealClass().getName()) .append(" ") .append(result.getMethod().getMethodName()) .append(" ") .append(this.formatDate(result.getStartMillis())) .append(" ") .append(result.getEndMillis()-result.getStartMillis()) .append("毫秒 ") .append(this.getStatus(result.getStatus())); } output.write(sb.toString()); output.flush(); output.close(); } catch (IOException e) { e.printStackTrace(); } } private String getStatus(int status){ String statusString = null; switch (status) { case 1: statusString = "SUCCESS"; break; case 2: statusString = "FAILURE"; break; case 3: statusString = "SKIP"; break; default: break; } return statusString; } private String formatDate(long date){ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return formatter.format(date); } }
生成測試報告測試
package com.demo; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Listeners; import org.testng.annotations.Test; @Listeners({com.demo.NewReport.class}) public class Test15 { @DataProvider public Object[][] dataProvider(){ return new Object[][]{{1},{2}}; } @Test(dataProvider="dataProvider") public void testAssert1(int a){ Assert.assertEquals(1, a); } @Test public void testAssert2(){ Assert.assertEquals("2", "2"); } }