给Fitnesse添加json格式报告
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了给Fitnesse添加json格式报告,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5887字,纯文字阅读大概需要9分钟。
内容图文
![给Fitnesse添加json格式报告](/upload/InfoBanner/zyjiaocheng/1131/3f56d89d4d7c4e7caa9b9e16a9d6e4b1.jpg)
需求:fitnesse自带xml、junit、html格式报告,现在需要添加json格式的报告,且报告中只展示执行错误的用例信息
修改文件:
fitnesse.http.Response.java
fitnesse.responders.run.SuiteResponder.java
添加文件:
fitnesse.reporting.history.JsonReFormatter.java
fitnesse.resources.templates.suiteJson.vm
fitnesse.http.Response.java:添加下面红色字体部分
1 ... 2 public Response(String formatString) { 3 Format format; 4 5 if ("html".equalsIgnoreCase(formatString)) { 6 format = Format.HTML; 7 } elseif ("xml".equalsIgnoreCase(formatString)) { 8 format = Format.XML; 9 } elseif ("junit".equalsIgnoreCase(formatString)) { 10 format = Format.JUNIT; 11 } elseif ("text".equalsIgnoreCase(formatString)) { 12 format = Format.TEXT; 13 } else if ("json".equalsIgnoreCase(formatString)) { 14 format = Format.JSON; 15 }else { 16 format = Format.HTML; 17 } 18 setContentType(format.getContentType()); 19 } 2021public Response(String format, int status) { 22this(format); 23this.status = status; 24 } 2526publicboolean isXmlFormat() { 27return Format.XML.contentType.equals(contentType); 28 } 2930publicboolean isHtmlFormat() { 31return Format.HTML.contentType.equals(contentType); 32 } 3334publicboolean isTextFormat() { 35return Format.TEXT.contentType.equals(contentType); 36 } 3738publicboolean isJunitFormat() { 39return Format.JUNIT.contentType.equals(contentType); 40 } 4142public boolean isJsonFormat() { 43 return Format.JSON.contentType.equals(contentType); 44 } 45 ...
fitnesse.responders.run.SuiteResponder.java:添加下面红色字体部分
1 ... 2 3 private void createMainFormatter() { 4 if (response.isXmlFormat()) { 5 mainFormatter = newXmlFormatter(); 6 } elseif (response.isTextFormat()) { 7 mainFormatter = newTextFormatter(); 8 } elseif (response.isJunitFormat()) { 9 mainFormatter = newJunitFormatter(); 10 } else if(response.isJsonFormat()){ 11 mainFormatter = newJsonFormatter(); 12 }else { 13 mainFormatter = newHtmlFormatter(); 14 } 15 } 1617.... 1819protected BaseFormatter newTextFormatter() { 20returnnew TestTextFormatter(response); 21 } 2223protected BaseFormatter newJunitFormatter() { 24returnnew JunitReFormatter(context, page, response.getWriter(), getSuiteHistoryFormatter()); 25 } 262728protected BaseFormatter newJsonFormatter() { 29 return new JsonReFormatter(context, page, response.getWriter(), getSuiteHistoryFormatter()); 30 } 3132protected BaseFormatter newHtmlFormatter() { 33returnnew SuiteHtmlFormatter(page, response.getWriter()); 34 } 35 ...
fitnesse.reporting.history.JsonReFormatter.java:添加该文件
1 import java.io.Closeable; 2 import java.io.File; 3 import java.io.IOException; 4 import java.io.Writer; 5 6 import fitnesse.FitNesseContext; 7 import fitnesse.reporting.BaseFormatter; 8 import fitnesse.wiki.WikiPage; 9 10 import org.apache.velocity.Template; 11 import org.apache.velocity.VelocityContext; 12 import org.apache.velocity.app.VelocityEngine; 13 import org.xml.sax.SAXException; 14 15 /** 16 * 17 * Format test results as Json report. This responder returns an alternate 18 * format of the test history. 19 */ 20 public class JsonReFormatter extends BaseFormatter implements Closeable { 2122privatefinal FitNesseContext context; 23privatefinal Writer writer; 24privatefinal SuiteHistoryFormatter historyFormatter; 2526public JsonReFormatter(FitNesseContext context, WikiPage page, Writer writer, SuiteHistoryFormatter historyFormatter) { 27super(page); 28this.context = context; 29this.writer = writer; 30this.historyFormatter = historyFormatter; 31 } 3233 @Override 34publicvoid close() throws IOException { 35 historyFormatter.close(); 3637// read file based on historyFormatter time-stamp38 VelocityContext velocityContext = new VelocityContext(); 39 velocityContext.put("formatter", this); 40 velocityContext.put("suiteExecutionReport", historyFormatter.getSuiteExecutionReport()); 41 VelocityEngine velocityEngine = context.pageFactory.getVelocityEngine(); 42 Template template = velocityEngine.getTemplate("suiteJson.vm"); 43 template.merge(velocityContext, writer); 44 writer.close(); 45 } 4647 @Override 48publicint getErrorCount() { 49return historyFormatter.getErrorCount(); 50 } 5152 TestExecutionReport makeTestExecutionReport(File file) throws IOException, SAXException, InvalidReportException { 53returnnew TestExecutionReport(file); 54 } 555657 }
fitnesse.resources.templates.suiteJson.vm:添加该文件
1 #set( $String = "" ) 2 #macro( format $s )$String.format("%.3f", $s)#end 3 #set($suiteTotalRunTimeSeconds = $suiteExecutionReport.totalRunTimeInMillis / 1000.0 ) 4 {"testsuite_name":"#escape($suiteExecutionReport.rootPath)","tests":"$suiteExecutionReport.pageHistoryReferences.size()","failures":"$suiteExecutionReport.finalCounts.wrong","disabled":"$suiteExecutionReport.finalCounts.ignores","errors":"$suiteExecutionReport.finalCounts.exceptions","time":"#format($suiteTotalRunTimeSeconds)","testcase":[ 5 #set($failure_count = $suiteExecutionReport.finalCounts.wrong) 6 #set($error_count = $suiteExecutionReport.finalCounts.exceptions) 7 #foreach ($reference in $suiteExecutionReport.pageHistoryReferences) 8 #set($classname = $formatter.getClassName($reference)) 9 #set($runTimeSeconds = $reference.RunTimeInMillis / 1000.0 ) 10 #if($reference.testSummary.exceptions > 0 || $reference.testSummary.wrong > 0 ) 11 {"name":"#escape($reference.pageName)","assertions":"$reference.testSummary.right","time":"#format($runTimeSeconds)", 12 #if($suiteExecutionReport.finalCounts.wrong > 0) 13 #set($failure_count = $failure_count - 1) 14 "failure_message":"$reference.testSummary.wrong errors", 15 #end 16 #if($reference.testSummary.exceptions > 0) 17 #set($error_count = $error_count - 1) 18 "error_message":"$reference.testSummary.exceptions exceptions", 19 #end 20 "system-out":"$reference.pageName?pageHistory&resultDate=$reference.resultDate"} 21 #if($failure_count > 0 || $error_count > 0) 22 , 23 #end 24 #end 25 #end 26 ]}
原文:http://www.cnblogs.com/moonpool/p/6230389.html
内容总结
以上是互联网集市为您收集整理的给Fitnesse添加json格式报告全部内容,希望文章能够帮你解决给Fitnesse添加json格式报告所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。