需求: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:添加下面红色字体部分

 ...
public Response(String formatString) {
Format format; if ("html".equalsIgnoreCase(formatString)) {
format = Format.HTML;
} else if ("xml".equalsIgnoreCase(formatString)) {
format = Format.XML;
} else if ("junit".equalsIgnoreCase(formatString)) {
format = Format.JUNIT;
} else if ("text".equalsIgnoreCase(formatString)) {
format = Format.TEXT;
} else if ("json".equalsIgnoreCase(formatString)) {
14 format = Format.JSON;
15 }
else {
format = Format.HTML;
}
setContentType(format.getContentType());
} public Response(String format, int status) {
this(format);
this.status = status;
} public boolean isXmlFormat() {
return Format.XML.contentType.equals(contentType);
} public boolean isHtmlFormat() {
return Format.HTML.contentType.equals(contentType);
} public boolean isTextFormat() {
return Format.TEXT.contentType.equals(contentType);
} public boolean isJunitFormat() {
return Format.JUNIT.contentType.equals(contentType);
} public boolean isJsonFormat() {
43 return Format.JSON.contentType.equals(contentType);
44 }
...

fitnesse.responders.run.SuiteResponder.java:添加下面红色字体部分

 ...

  private void createMainFormatter() {
if (response.isXmlFormat()) {
mainFormatter = newXmlFormatter();
} else if (response.isTextFormat()) {
mainFormatter = newTextFormatter();
} else if (response.isJunitFormat()) {
mainFormatter = newJunitFormatter();
} else if(response.isJsonFormat()){
11 mainFormatter = newJsonFormatter();
12 }
else {
mainFormatter = newHtmlFormatter();
}
} .... protected BaseFormatter newTextFormatter() {
return new TestTextFormatter(response);
} protected BaseFormatter newJunitFormatter() {
return new JunitReFormatter(context, page, response.getWriter(), getSuiteHistoryFormatter());
} protected BaseFormatter newJsonFormatter() {
29 return new JsonReFormatter(context, page, response.getWriter(), getSuiteHistoryFormatter());
30 }

protected BaseFormatter newHtmlFormatter() {
return new SuiteHtmlFormatter(page, response.getWriter());
}
...

fitnesse.reporting.history.JsonReFormatter.java:添加该文件

 import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.Writer; import fitnesse.FitNesseContext;
import fitnesse.reporting.BaseFormatter;
import fitnesse.wiki.WikiPage; import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.xml.sax.SAXException; /**
*
* Format test results as Json report. This responder returns an alternate
* format of the test history.
*/
public class JsonReFormatter extends BaseFormatter implements Closeable { private final FitNesseContext context;
private final Writer writer;
private final SuiteHistoryFormatter historyFormatter; public JsonReFormatter(FitNesseContext context, WikiPage page, Writer writer, SuiteHistoryFormatter historyFormatter) {
super(page);
this.context = context;
this.writer = writer;
this.historyFormatter = historyFormatter;
} @Override
public void close() throws IOException {
historyFormatter.close(); // read file based on historyFormatter time-stamp
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("formatter", this);
velocityContext.put("suiteExecutionReport", historyFormatter.getSuiteExecutionReport());
VelocityEngine velocityEngine = context.pageFactory.getVelocityEngine();
Template template = velocityEngine.getTemplate("suiteJson.vm");
template.merge(velocityContext, writer);
writer.close();
} @Override
public int getErrorCount() {
return historyFormatter.getErrorCount();
} TestExecutionReport makeTestExecutionReport(File file) throws IOException, SAXException, InvalidReportException {
return new TestExecutionReport(file);
} }

fitnesse.resources.templates.suiteJson.vm:添加该文件

 #set( $String = "" )
#macro( format $s )$String.format("%.3f", $s)#end
#set($suiteTotalRunTimeSeconds = $suiteExecutionReport.totalRunTimeInMillis / 1000.0 )
{"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":[
#set($failure_count = $suiteExecutionReport.finalCounts.wrong)
#set($error_count = $suiteExecutionReport.finalCounts.exceptions)
#set($all_count = $failure_count+$error_count)
#foreach ($reference in $suiteExecutionReport.pageHistoryReferences)
#set($classname = $formatter.getClassName($reference))
#set($runTimeSeconds = $reference.RunTimeInMillis / 1000.0 )
#if($reference.testSummary.exceptions > 0 || $reference.testSummary.wrong > 0 )
{"name":"#escape($reference.pageName)","assertions":"$reference.testSummary.right","time":"#format($runTimeSeconds)",
#set($all_count = $all_count - )
#if($suiteExecutionReport.finalCounts.wrong > 0)
"failure_message":"$reference.testSummary.wrong errors",
#end
#if($reference.testSummary.exceptions > 0)
"error_message":"$reference.testSummary.exceptions exceptions",
#end
"system_out":"$reference.pageName?pageHistory&resultDate=$reference.resultDate"}
#if($all_count > 0)
,
#end
#end
#end
]}

给Fitnesse添加json格式报告的更多相关文章

  1. JS学习笔记(3)--json格式数据的添加,删除及排序方法

    这篇文章主要介绍了json格式数据的添加,删除及排序方法,结合实例形式分析了针对一维数组与二维数组的json格式数据进行增加.删除与排序的实现技巧,需要的朋友可以参考下   本文实例讲述了json格式 ...

  2. WebApi返回Json格式字符串

    WebApi返回json格式字符串, 在网上能找到好几种方法, 其中有三种普遍的方法, 但是感觉都不怎么好. 先贴一下, 网上给的常用方法吧. 方法一:(改配置法) 找到Global.asax文件,在 ...

  3. JSON格式序列化与反序列化(List、XML)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  4. (Spring4 json入门)Spring4+SpringMVC+页面数据发送与接收(json格式)

    jar包(Maven仓库): Spring4 jar包(Maven仓库): 在测试过程中我查看了网上的一些教程,但是那些教程都是在Spring3环境下的,Spring3和Spring4解析json需要 ...

  5. MVC学习系列6--使用Ajax加载分部视图和Json格式的数据

    Ajax的应用在平时的工作中,很是常见,这篇文章,完全是为了,巩固复习. 我们先看看不使用json格式返回分部视图: 先说需求吧: 我有两个实体,一个是出版商[Publisher],一个是书[Book ...

  6. 【转】如何把Json格式字符写进text文件中

    http://www.cnblogs.com/insus/p/4306640.html http://json2csharp.chahuo.com/ 本篇一步一步学习怎样把显示于网页的json格式的字 ...

  7. webapi返回json格式优化

    一.设置webapi返回json格式 在App_Start下的WebApiConfig的注册函数Register中添加下面这代码 config.Formatters.Remove(config.For ...

  8. Json格式转换

    验证Json格式可以进入 http://json.cn/ json简单说就是javascript中的对象和数组,所以这两种结构就是对象和数组两种结构,通过这两种结构可以表示各种复杂的结构1.对象:对象 ...

  9. ajax访问服务器返回json格式

    使用ajax访问服务器返回多条数据,比如返回一个表中的所有数据,页面该如何处理呢?如何获取数据呢?一直不会用ajax返回json格式,今天研究了下,分享给大家~ 首先需要引用服务,点击项目右键,添加引 ...

随机推荐

  1. 我的Android进阶之旅------>Android图片处理(Matrix,ColorMatrix)

    本文转载于:http://www.cnblogs.com/leon19870907/articles/1978065.html 在编程中有时候需要对图片做特殊的处理,比如将图片做出黑白的,或者老照片的 ...

  2. ruby 正则表达式

    Ruby学习笔记-正则表达式 Posted on 2011-11-29 17:55 Glen He 阅读(4998) 评论(0) 编辑 收藏 1.创建正则表达式 a) reg1 = /^[a-z]*$ ...

  3. idea生成可执行jar

    1.创建工程 ①使用idea新建一个maven工程. ②编辑工具逻辑代码 ③完成代码的编写后添加工具调用的main方法以接收参数 至此代码编辑过程已经基本完成 ④在maven管理依赖的时候使用idea ...

  4. 5.JavaScript优化及导航菜单背后的秘密

    JavaScript优化及导航菜单背后的秘密 伍星 学习目标1.进一步了解前端优化 学习如何编写良好的 JavaScirpt2.通过导航的学习,了解JavaScirpt的应用 JavaScript在用 ...

  5. Android Weekly Notes Issue #276

    September 24th, 2017 Android Weekly Issue #276 本期内容包括LifeCycle与Architecture的相关文章,以及新的JSON解析库Moshi的介绍 ...

  6. git创建项目的两种方式

    场景1: 将本地内容推送给远程库 1.创建版本库 git init 将此目录转换为git可管理的仓库 git config --global user.name "xx" 或 gi ...

  7. <JAVA图像学习笔记>关于Graphics/Graphics2D以及简单的几何图像制作(一个简单钟表的实现)

    题外话:正好赶上OperatingSystem的作业要做一个模拟线程/进程调度的问题,决定用JAVA实现才发现这些内容和之前学过的GUI制作是两码事儿- -b 通过学习java.swing库的Acti ...

  8. dotnet core on Linux 环境搭建及入门demo

    首先感谢张善友大大提供的腾讯云实验室链接(https://www.qcloud.com/developer/labs/list). 以下是整个搭建过程及简单demo实例 1.搭建 .NET Core ...

  9. Hadoop- Hadoop详解

    首先所有知识以官网为准,所有的内容在官网上都有展示,所有的变动与改进,新增内容都以官网为准.hadoop.apache.org Hadoop是一个开源的可拓展的分布式并行处理计算平台,利用服务器集群根 ...

  10. html5 手写的canvas实现

    试用支持canvas的浏览器,无JS依赖,运用新的HTML5技术DrawBoard.renderDrawer('myHandWrite',{  penColor:'#FF0000',  penWidt ...