Permalink
Browse files

Added primitive/initial support for junit xml result output.

  • Loading branch information...
1 parent 1d16d85 commit d0322f765f11c63ff3ac6840bef67ab7499246df jstanton committed Nov 24, 2012
Showing with 162 additions and 1 deletion.
  1. +4 −0 lib/cucumber/cli/configuration.js
  2. +1 −0 lib/cucumber/listener.js
  3. +155 −0 lib/cucumber/listener/junit_formatter.js
  4. +2 −1 package.json
@@ -9,6 +9,9 @@ var Configuration = function(argv) {
var formatter;
var format = argumentParser.getFormat();
switch(format) {
+ case Configuration.JUNIT_FORMAT_NAME:
+ formatter = Cucumber.Listener.JunitFormatter();
+ break;
case Configuration.JSON_FORMAT_NAME:
formatter = Cucumber.Listener.JsonFormatter();
break;
@@ -70,6 +73,7 @@ var Configuration = function(argv) {
};
return self;
};
+Configuration.JUNIT_FORMAT_NAME = "junit";
Configuration.JSON_FORMAT_NAME = "json";
Configuration.PRETTY_FORMAT_NAME = "pretty";
Configuration.PROGRESS_FORMAT_NAME = "progress";
View
@@ -36,6 +36,7 @@ Listener.EVENT_HANDLER_NAME_SUFFIX = 'Event';
Listener.Formatter = require('./listener/formatter');
Listener.PrettyFormatter = require('./listener/pretty_formatter');
Listener.ProgressFormatter = require('./listener/progress_formatter');
+Listener.JunitFormatter = require('./listener/junit_formatter');
Listener.JsonFormatter = require('./listener/json_formatter');
Listener.StatsJournal = require('./listener/stats_journal');
Listener.SummaryFormatter = require('./listener/summary_formatter');
@@ -0,0 +1,155 @@
+var JunitFormatter = function (options) {
+ var Cucumber = require('../../cucumber');
+ var builder = require('xmlbuilder');
+
+ //var failedScenarioLogBuffer = "";
+ //var undefinedStepLogBuffer = "";
+ //var failedStepResults = Cucumber.Type.Collection();
+ var testSuitesStartTime,testSuiteStartTime, testCaseStartTime;
+ var testSuites, testSuite, testCase;
+ var testSuitesTotalTestCount, testSuiteTotalTestCount;
+ var testSuitesErrorTestCount, testSuiteErrorTestCount;
+ var testSuitesFailureTestCount, testSuiteFailureTestCount;
+ var feature;
+
+ var statsJournal = Cucumber.Listener.StatsJournal();
+
+ var self = Cucumber.Listener.Formatter(options);
+
+ var parentHear = self.hear;
+ self.hear = function hear(event, callback) {
+ statsJournal.hear(event, function () {
+ parentHear(event, callback);
+ });
+ };
+/*
+ self.handleStepResultEvent = function handleStepResult(event, callback) {
+ console.log('handleStepResultEvent');
+ callback();
+ };
+
+ self.handleUndefinedStepResult = function handleUndefinedStepResult(stepResult) {
+ console.log('handleUndefinedStepResult');
+ };
+
+ self.handleFailedStepResult = function handleFailedStepResult(stepResult) {
+ console.log('handleFailedStepResult');
+ };
+*/
+
+ self.handleBeforeScenarioEvent = function handleBeforeScenarioEvent(event, callback) {
+ //console.log('handleBeforeScenarioEvent');
+ testCaseStartTime = self.getTime();
+ callback();
+ };
+
+
+ self.handleAfterScenarioEvent = function handleAfterScenarioEvent(event, callback) {
+ //console.log('handleAfterScenarioEvent');
+ self.buildTestCase(event, callback);
+ };
+
+ self.handleBeforeFeaturesEvent = function handleBeforeFeaturesEvent(event, callback) {
+ //console.log('handleBeforeFeaturesEvent');
+ testSuites = builder.create('testsuites');
+ testSuitesStartTime = self.getTime();
+ testSuitesTotalTestCount = testSuitesErrorTestCount = testSuitesFailureTestCount = 0;
+
+ callback();
+ };
+ self.handleAfterFeaturesEvent = function handleAfterFeaturesEvent(event, callback) {
+ //console.log('handleAfterFeaturesEvent');
+ // update totals
+ testSuites.att('time', self.getTimeDiff(testSuitesStartTime, self.getTime()));
+ testSuites.att('tests', testSuitesTotalTestCount);
+ testSuites.att('errors', testSuitesErrorTestCount);
+ testSuites.att('failures', testSuitesFailureTestCount);
+
+ // log result
+ self.logXML();
+ callback();
+ };
+ self.handleBeforeFeatureEvent = function handleBeforeFeatureEvent(event, callback) {
+ //console.log('handleBeforeFeatureEvent');
+ testSuite = testSuites.element('testsuite');
+ testSuiteStartTime = self.getTime();
+ testSuiteTotalTestCount = testSuiteErrorTestCount = testSuiteFailureTestCount = 0;
+ feature = event.getPayloadItem('feature');
+
+ callback();
+ };
+
+ self.handleAfterFeatureEvent = function handleAfterFeatureEvent(event, callback) {
+ //console.log('handleAfterFeatureEvent');
+ var feature = event.getPayloadItem('feature');
+ //console.log(feature.getName());
+ testSuite.att('name', feature.getName());
+ testSuite.att('time', self.getTimeDiff(testSuiteStartTime, self.getTime()));
+ testSuite.att('tests', testSuiteTotalTestCount);
+ testSuite.att('errors', testSuiteErrorTestCount);
+ testSuite.att('failures', testSuiteFailureTestCount);
+
+ callback();
+ };
+
+ self.logXML = function logXML(){
+ //console.log('logXML');
+ // log results
+ self.log(testSuites.end({ pretty: true}));
+ };
+
+ self.buildTestCase = function(event, callback){
+ scenario = event.getPayloadItem('scenario');
+
+ var testCase = testSuite.element('testcase');
+
+ // set name
+ testCase.att('name', scenario.getName());
+ // set classname/feature
+ testCase.att('classname', feature.getName());
+ // set time
+ testCase.att('time', self.getTimeDiff(testCaseStartTime, self.getTime()));
+
+ // increment the total counters
+ testSuitesTotalTestCount ++;
+ testSuiteTotalTestCount ++;
+
+ var name = scenario.getName();
+ var uri = scenario.getUri();
+ var line = scenario.getLine();
+
+ //console.log(scenario);
+ var text = uri + ":" + line + " # Scenario: " + name + "";
+ // handle errors
+ if(statsJournal.isCurrentScenarioPending() || statsJournal.isCurrentScenarioUndefined()){
+
+ // increment the error counters
+ testSuitesErrorTestCount ++;
+ testSuiteErrorTestCount ++;
+
+ // create the error element
+ testCase.element('error', {message:"Scenario: "+name+" ERROR"}, text);
+
+ // handle failures
+ } else if (statsJournal.isCurrentScenarioFailing()) {
+
+ // increment the failure counters
+ testSuitesFailureTestCount ++;
+ testSuiteFailureTestCount ++;
+
+ // create the failure element
+ testCase.element('failure', {message:"Scenario: "+name+" FAILED"}, text);
+ }
+ callback();
+
+ };
+ self.getTime = function getTime(){
+ return (new Date).getTime();
+ };
+ self.getTimeDiff = function getTimeDiff(before, after){
+ return (after - before)/1000;
+ };
+
+ return self;
+};
+module.exports = JunitFormatter;
View
@@ -54,7 +54,8 @@
"mkdirp": "0.3.3",
"cucumber-html": "0.2.2",
"walkdir": "0.0.4",
- "coffee-script": "1.4.0"
+ "coffee-script": "1.4.0",
+ "xmlbuilder":"0.4.2"
},
"scripts": {
"test": "./bin/cucumber.js && jasmine-node spec"

0 comments on commit d0322f7

Please sign in to comment.