TestNg的IReporter接口的使用

IReporter接口是干嘛的?就是让用户自定义报告的,很多人想要自定义报告,于是乎找各种插件,比如什么testng-xslt啊,reportng啊,各种配置,最后出来的结果,还不能定制化,但为什么不能自已定制一份呢?testng的IReporter接口就接供了这样的功能,我们只需要实现这个接口,并加上监听,就能拿到所有的信息了,你可以把这些信息存数据库,或者自已生成一个html,亦或者写在EXCEL里啊等等,都可以,下面给出IReporter接口的实现方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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 xmlSuites, List suites, String outputDirectory) {
         List list =  new  ArrayList();
         for  (ISuite suite : suites) {          
             Map 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 listTestResult(IResultMap resultMap){
         Set results = resultMap.getAllResults();    
         return  new  ArrayList(results);
     }
     
     private  void  sort(List list){
         Collections.sort(list,  new  Comparator() {
             @Override
             public  int  compare(ITestResult r1, ITestResult r2) {
                 if (r1.getStartMillis()>r2.getStartMillis()){
                     return  1 ;
                 } else {
                     return  - 1 ;
                 }              
             }
         });
     }
     
     private  void  outputResult(List 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);
     }
 
}

 代码简单易懂,下面给一个测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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" ); 
     }
 
}

 运行后,大家打开test-output文件夹,看是不是多了个test.txt的文件夹。截图如下:

你可能感兴趣的:(TestNg的IReporter接口的使用)