ReportNG测试报告的定制修改
本文系轉載 !!!http://blog.csdn.net/qq744746842/article/details/49744647
前段時間在Testerhome上面看到了測試報告生成系列之——-如何用 testNG 生成測試報告?簡單的描述了一些測試報告的生成,接著有人在評論中回復說可以針對reportNg的測試報告做一些定制化的修改,并且還附上了一張截圖。這里我們看下修改后的效果圖
確實是比reportNg原生的測試報告好看多了。
,那下來我們就來看看如何去修改我們的reportNg
正文
我們先從github上拉下 reportNg的源代碼?reportng?, 我們先看下整體的目錄結構:
因為我們是要修改html的生成,所以說我們大部分要修改的內容都是在resources下進行。
reportng.properties?增加部分類表項?
這里我們直接在末尾添加
- 1
- 2
- 3
results.html.vm?修改結果的html,我們目前只修改fail的情況下。
#if ($failedTests.size() > 0)<table class="resultsTable"><tr><th colspan="5" class="header failed">$messages.getString("failedTests")</th></tr>#foreach ($testClass in $failedTests.keySet())<tr><td colspan="1" class="group">$testClass.name</td><td colspan="1" class="group">$messages.getString("duration")</td><td colspan="1" class="group">$messages.getString("log")</td><td colspan="1" class="group">$messages.getString("screenshot")</td></tr>#set ($classResults = $failedTests.get($testClass))#parse ("org/uncommons/reportng/templates/html/class-results.html.vm")#end</table>#end- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
class-results.html.vm?這里是修改具體的內容顯示
## Show logger output for the test.#set ($output = $utils.getTestOutput($testResult))#if ($output.size() > 0)<div class="testOutput">#foreach( $line in $output )#if ($meta.shouldEscapeOutput())$utils.escapeHTMLString($utils.removeImage($line))<br />#else$utils.removeImage($line)<br />#end#end</div>#end- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
修改以上部分
<td class="screenshot">#set ($output = $utils.getTestOutput($testResult))#if ($output.size() > 0)<div class="screenshotimage">#foreach( $line in $output )#if ($meta.shouldEscapeOutput())$utils.escapeHTMLString($utils.getImageString($line))<br />#else$utils.getImageString($line)<br />#end#end</div>#end</td>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
新增以上部分?
上面出現的兩個方法getImageString,removeImage。 就是提取含有img標簽的字符串和去除帶有img標簽的字符串
ReportNGUtils.java?新增兩個方法
public String getImageString(String s) {String regex = "(<img(.*?)/>)";Pattern pattern = Pattern.compile(regex);Matcher matcher = pattern.matcher(s);while (matcher.find()) {String group = matcher.group(1);//可根據實際情況多個圖片 全部一起returnreturn group;}return ""; }public String removeImage(String s) {return s.replaceAll("<img(.*?)/>",""); }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
以上就是reportNg修改源代碼的位置了
下來就是我們的測試代碼了。實際上很簡單 直接一個斷言,接著在用例結束的時候判斷結果是否失敗,是的話就截圖。
@Test public void careInterfaceSmoke(){Assert.assertEquals(1,2); }@AfterMethod(alwaysRun = true) public void afterMethod(ITestResult result) throws Exception {if (!result.isSuccess())catchExceptions(result); }public void catchExceptions(ITestResult result) {System.out.println("result" + result);String methodName = result.getName();System.out.println(methodName);if (!result.isSuccess()) {File file = new File("");Reporter.setCurrentTestResult(result);System.out.println(file.getAbsolutePath());Reporter.log(file.getAbsolutePath());String filePath = file.getAbsolutePath();filePath = filePath.replace("/opt/apache-tomcat-7.0.64/webapps","http://172.18.44.114:8080");Reporter.log("<img src='"+filePath+"/"+result.getName()+".jpg' hight='100' width='100'/>");int width = 100;int height = 100;String s = "這是一張測試圖片";File screenShotFile = new File(file.getAbsolutePath()+"/"+result.getName()+".jpg");Font font = new Font("Serif", Font.BOLD, 10);BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g2 = (Graphics2D)bi.getGraphics();g2.setBackground(Color.BLACK);g2.clearRect(0, 0, width, height);g2.setPaint(Color.RED);FontRenderContext context = g2.getFontRenderContext();Rectangle2D bounds = font.getStringBounds(s, context);double x = (width - bounds.getWidth()) / 2;double y = (height - bounds.getHeight()) / 2;double ascent = -bounds.getY();double baseY = y + ascent;g2.drawString(s, (int)x, (int)baseY);try {ImageIO.write(bi, "jpg", screenShotFile);} catch (IOException e) {e.printStackTrace();}- 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
catchExceptions方法中圖片只是為了測試生成了一張圖片,實際可以通過對應的測試框架的api方法進行截圖操作的。
以上就是基本的內容了,但是還一定要記得在pom.xml配置的時候增加一句
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.18.1</version><configuration><systemPropertyVariables><org.uncommons.reportng.escape-output>false</org.uncommons.reportng.escape-output> </systemPropertyVariables>....<configuration>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
上面配置的做用是為了報告中的顯示不會單純的顯示出html,而且能夠正確的加載出html,生成的報告結果就如下了。
總結
感覺上面的方法還是有點取巧的了,感覺真正的方法應該不是如此,不過實在是google了很久,都沒有相應的代碼。
總結
以上是生活随笔為你收集整理的ReportNG测试报告的定制修改的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 贷款违约预测带有真实银行数据的端到端ml
- 下一篇: dbm与mysql区别_dbm数据库