1 package com.reporter.main; 2 3 import java.io.BufferedWriter; 4 import java.io.FileWriter; 5 import java.io.Writer; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Properties; 9 import org.apache.velocity.Template; 10 import org.apache.velocity.VelocityContext; 11 import org.apache.velocity.app.VelocityEngine; 12 import org.testng.IReporter; 13 import org.testng.IResultMap; 14 import org.testng.ISuite; 15 import org.testng.ISuiteResult; 16 import org.testng.ITestContext; 17 import org.testng.ITestResult; 18 import org.testng.xml.XmlSuite; 19 20 public class GenerateReporter implements IReporter { 21 @Override 22 public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, 23 String outputDirectory) { 24 // TODO Auto-generated method stub 25 try { 26 // 初始化并取得Velocity引擎 27 VelocityEngine ve = new VelocityEngine(); 28 Properties p = new Properties(); 29 //虽然不懂为什么这样设置,但结果是好的.可以用了 30 p.setProperty("resource.loader", "class"); 31 p.setProperty("class.resource.loader.class","org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); 32 ve.init(p); 33 Template t = ve.getTemplate("com/reporter/VMmodel/overview.vm"); 34 VelocityContext context = new VelocityContext(); 35 36 for (ISuite suite : suites) { 37 Map<String, ISuiteResult> suiteResults = suite.getResults(); 38 for (ISuiteResult suiteResult : suiteResults.values()) { 39 ReporterData data = new ReporterData(); 40 ITestContext testContext = suiteResult.getTestContext(); 41 // 把数据填入上下文 42 context.put("overView", data.testContext(testContext));//测试结果汇总信息 43 //ITestNGMethod[] allTests = testContext.getAllTestMethods();//所有的测试方法 44 //Collection<ITestNGMethod> excludeTests = testContext.getExcludedMethods();//未执行的测试方法 45 IResultMap passedTests = testContext.getPassedTests();//测试通过的测试方法 46 IResultMap failedTests = testContext.getFailedTests();//测试失败的测试方法 47 IResultMap skippedTests = testContext.getSkippedTests();//测试跳过的测试方法 48 49 context.put("pass", data.testResults(passedTests, ITestResult.SUCCESS)); 50 context.put("fail", data.testResults(failedTests, ITestResult.FAILURE)); 51 context.put("skip", data.testResults(skippedTests, ITestResult.FAILURE)); 52 53 54 55 } 56 } 57 // 输出流 58 <span style="white-space:pre"> </span>//Writer writer = new BufferedWriter(new FileWriter("report.html")); 59 <span style="white-space:pre"> </span>OutputStream out=new FileOutputStream("report.html"); 60 <span style="white-space:pre"> </span>Writer writer = new BufferedWriter(new OutputStreamWriter(out,"utf-8"));//解决乱码问题 61 // 转换输出 62 t.merge(context, writer); 63 //System.out.println(writer.toString()); 64 writer.flush(); 65 } catch (Exception e) { 66 // TODO Auto-generated catch block 67 e.printStackTrace(); 68 } 69 } 70 71 72 } |
6.测试报告模板,文件后辍为vm,比如overview.vm,内容差不多就是一个html文件,(要漂亮报告找前端。。。)
1 <?xml version="1.0" encoding="utf-8" ?> 2 3 <head> 4 <title>test</title> 5 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 6 <meta name="description" content="TestNG unit test results." /> 7 8 </head> 9 <body> 10 11 <h1>Test</h1> 12 <table border="1"> 13 <tr> 14 <th>OverView........</th> 15 <th colspan="6" class="header suite"> 16 <div > 17 <a href="http://www.baidu.com">aaa</a> 18 </div> 19 </th> 20 </tr> 21 <tr class="columnHeadings"> 22 <td> </td> 23 <th>all</th> 24 <th>excluded</th> 25 <th>passed</th> 26 <th>faild</th> 27 <th>skipped</th> 28 <th>duration(S)</th> 29 <th>passration</th> 30 <th>alltestMethod</th> 31 <th>excluedMethod</th> 32 </tr> 33 34 <tr> 35 <td>TestResult</td> 36 <td>$overView.allTestsSize</td> 37 <td>$overView.excludeTestsSize</td> 38 <td>$overView.passedTestsSize</td> 39 <td>$overView.failedTestsSize</td> 40 <td>$overView.skippedTestsSize</td> 41 <td>$overView.testsTime</td> 42 <td>$overView.passPercent</td> 43 <td> 44 #foreach($p in $overView.allTestsMethod) 45 $p<br/> 46 #end 47 </td> 48 <td> 49 #foreach($e in $overView.excludeTestsMethod) 50 $e<br/> 51 #end 52 </td> 53 </tr> 54 </table> 55 <br/><br/> 56 <table border="1"> 57 <tr> 58 <th>PassTests.............</th> 59 <th colspan="6" class="header suite"> 60 <div > 61 <a href="http://www.baidu.com">aaa</a> 62 </div> 63 </th> 64 </tr> 65 <tr class="columnHeadings"> 66 <td> </td> 67 <th>testName</th> 68 <th>className</th> 69 <th>duration</th> 70 <th>params</th> 71 <th>description</th> 72 <th>output</th> 73 <th>dependMethod</th> 74 </tr> 75 76 #foreach( $p in $pass) 77 <tr> 78 <td>$velocityCount</td> 79 <td>${p.testName} 80 #if(${p.description}) 81 (${p.description}) 82 #end</td> 83 <td>$p.className</td> 84 <td>$p.duration</td> 85 <td>$!p.params</td> 86 <td>$!p.description</td> 87 <td> 88 #foreach($o in $p.output) 89 $o<br/> 90 #end 91 </td> 92 <td>$p.dependMethod</td> 93 <td>$!p.throwable</td> 94 <td> 95 #if($p.throwable ) 96 #foreach($o in $p.stackTrace) 97 $o<br/> 98 #end 99 #end 100 </td> 101 #end 102 </tr> 103 104 </table> 105 <br/> 106 107 108 <br/><br/> 109 110 <table border="1"> 111 <tr> 112 <th>FailedTests...............</th> 113 <th colspan="6" class="header suite"> 114 <div > 115 <a href="http://www.baidu.com">aaa</a> 116 </div> 117 </th> 118 </tr> 119 <tr class="columnHeadings"> 120 <td> </td> 121 <th>testName</th> 122 <th>className</th> 123 <th>duration</th> 124 <th>params</th> 125 <th>description</th> 126 <th>output</th> 127 <th>dependMethod</th> 128 <th>throwable</th> 129 <th>stackTrace</th> 130 </tr> 131 132 #foreach( $p in $fail) 133 <tr> 134 <td>$velocityCount</td> 135 <td>$p.testName</td> 136 <td>$p.className</td> 137 <td>$p.duration</td> 138 <td>$!p.params</td> 139 <td>$!p.description</td> 140 <td> 141 #foreach($o in $p.output) 142 $o<br/> 143 #end 144 </td> 145 <td>$p.dependMethod</td> 146 <td>$p.throwable</td> 147 <td> 148 #if($p.throwable ) 149 #foreach($o in $p.stackTrace) 150 $o<br/> 151 #end 152 #end 153 </td> 154 #end 155 </tr> 156 157 158 </table> 159 160 <br/><br/> 161 162 163 164 </body> 165 </html> |
7.测试报告,新建一个java项目,在测试类中添加监听器
@Listeners({com.reporter.main.GenerateReporter.class})
或者在testng.xml添加监听器
<listener class-name="com.reporter.main.GenerateReporter" />
1 import org.testng.Assert; 2 import org.testng.Reporter; 3 import org.testng.annotations.Listeners; 4 import org.testng.annotations.Parameters; 5 import org.testng.annotations.Test; 6 7 @Listeners({com.reporter.main.GenerateReporter.class}) 8 public class test { 9 @Test 10 public void a(){ 11 Reporter.log("<a href='http://www.baidu.com' target='blank'>baidu.com</a>"); 12 System.out.println("111"); 13 } 14 @Test(enabled=false,dependsOnMethods="a") 15 @Parameters("param") 16 public void b(String s){ 17 Assert.assertEquals(s,"dataxml"); 18 19 } 20 @Test(enabled=true,dependsOnMethods="e") 21 public void c(){ 22 Assert.assertEquals(2,2); 23 } 24 @Test(description="测试方法 DDD") 25 public void d() { 26 Reporter.log("DDDDDDDDDD"); 27 Reporter.log("AAAAAAAAAAAA"); 28 System.out.println("Verify.verifyEquals(2,2)"); 29 Assert.assertEquals(2,2); 30 } 31 @Test(description="98788",groups="test",invocationCount=1,dependsOnMethods="d") 32 public void e() { 33 Reporter.log("EEEEEEEEEEEEEEEEE"); 34 Assert.assertEquals(1,2); 35 System.out.println("Verify.verifyEquals(2,2)"); 36 } 37 } |
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。