首页 > 代码库 > 给Fitnesse添加json格式报告

给Fitnesse添加json格式报告

需求: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     } else if ("xml".equalsIgnoreCase(formatString)) {
 8       format = Format.XML;
 9     } else if ("junit".equalsIgnoreCase(formatString)) {
10       format = Format.JUNIT;
11     } else if ("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   }
20 
21   public Response(String format, int status) {
22     this(format);
23     this.status = status;
24   }
25 
26   public boolean isXmlFormat() {
27     return Format.XML.contentType.equals(contentType);
28   }
29 
30   public boolean isHtmlFormat() {
31     return Format.HTML.contentType.equals(contentType);
32   }
33 
34   public boolean isTextFormat() {
35     return Format.TEXT.contentType.equals(contentType);
36   }
37 
38   public boolean isJunitFormat() {
39         return Format.JUNIT.contentType.equals(contentType);
40   }
41   
42   public 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     } else if (response.isTextFormat()) {
 7       mainFormatter = newTextFormatter();
 8     } else if (response.isJunitFormat()) {
 9       mainFormatter = newJunitFormatter();
10     } else if(response.isJsonFormat()){
11       mainFormatter = newJsonFormatter();
12     }else {
13       mainFormatter = newHtmlFormatter();
14     }
15   }
16 
17 ....
18 
19 protected BaseFormatter newTextFormatter() {
20     return new TestTextFormatter(response);
21   }
22 
23   protected BaseFormatter newJunitFormatter() {
24     return new JunitReFormatter(context, page, response.getWriter(), getSuiteHistoryFormatter());
25   }
26   
27 
28   protected BaseFormatter newJsonFormatter() {
29         return new JsonReFormatter(context, page, response.getWriter(), getSuiteHistoryFormatter());
30       }
31 
32   protected BaseFormatter newHtmlFormatter() {
33     return new 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 {
21 
22   private final FitNesseContext context;
23   private final Writer writer;
24   private final SuiteHistoryFormatter historyFormatter;
25 
26   public JsonReFormatter(FitNesseContext context, WikiPage page, Writer writer, SuiteHistoryFormatter historyFormatter) {
27     super(page);
28     this.context = context;
29     this.writer = writer;
30     this.historyFormatter = historyFormatter;
31   }
32 
33   @Override
34   public void close() throws IOException {
35     historyFormatter.close();
36 
37     // read file based on historyFormatter time-stamp
38     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   }
46 
47   @Override
48   public int getErrorCount() {
49     return historyFormatter.getErrorCount();
50   }
51 
52   TestExecutionReport makeTestExecutionReport(File file) throws IOException, SAXException, InvalidReportException {
53     return new TestExecutionReport(file);
54   }
55 
56 
57 }

 

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 ]}

 

给Fitnesse添加json格式报告