Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xunit reporter #179

Merged
merged 4 commits into from
Dec 28, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/reporters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ exports.Spec = require('./spec');
exports.Progress = require('./progress');
exports.Landing = require('./landing');
exports.JSONStream = require('./json-stream');
exports.XUnit = require('./xunit')
91 changes: 91 additions & 0 deletions lib/reporters/xunit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
var Base = require('./base');
var utils = require('../utils');

/**
* Expose `XUnit`.
*/

exports = module.exports = XUnit;

function XUnit(runner) {
Base.call(this, runner);
var stats = this.stats;
var tests = [];
var self = this;

runner.on('test end', function(test) {
tests.push(test);
});

runner.on('end', function() {
console.error(tag('testsuite', {
name: 'Mocha Tests',
tests: stats.tests,
failures: stats.failures,
skip: (stats.tests - stats.failures - stats.passes),
timestamp: (new Date).toUTCString(),
time: stats.duration / 1000.0
}, false));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should output on test pass / failure instead of one chunk at the end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I couldn't figure out how to do that.

The XUnit spec defines a block at the start of the XML that has attributes showing the # of passed/failed/skipped tests. How can I output that at the beginning and then output the individual test results in "test end", "pass", "fail" handlers... if I don't know how many test have failed until the end of the test run?

That's why I had to go with one big dump at the end of the run. Any ideas?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhh, if that's the case nvm

var _i, _len;
for (_i = 0; _len = tests.length, _i < _len; _i++) {
processTest(tests[_i]);
}

console.error(end('testsuite'));
});
}

function processTest(test) {
var attributes = {
classname: test.fullTitle(),
name: test.title,
time: test.duration / 1000.0
};

if (test.failed) {
console.error( tag('testcase', attributes, false, failedTestTag(test)) );
} else if (test.pending) {
console.error( tag('testcase', attributes, false, pendingTestTag(test)) );
} else {
console.error( tag('testcase', attributes, true) );
}
}

function pendingTestTag(test) {
return tag('skipped', { message: utils.escape(test.err.stack || test.err) }, true);
}

function failedTestTag(test) {
var str = test.err;

// <=IE7 stringifies to [Object Error]. Since it can be overloaded, we
// check for the result of the stringifying.
if ('[object Error]' == str) str = test.err.message;

var attributes = { message: utils.escape(str) };
return tag('failure', attributes, false, cdata(test.err.stack));
}

function tag(name, attribs, single, content) {
var tag;
var end = single ? ' />' : '>'
var strAttr = [];
for (var attr in attribs) {
attribs.hasOwnProperty(attr) && strAttr.push(attr + '="' + utils.escape(attribs[attr]) + '"');
}

tag = '<' + name + (strAttr.length? ' ' + strAttr.join(' ') : '' ) + end;
tag = content? (tag + content + '</' + name + end) : tag;
return tag;
}

function end(name) {
return '</' + name + '>';
}

function cdata(data) {
return '<![CDATA[' + utils.escape(data) + ']]>';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jkrall! Any reason to wrap already escaped data inside a CDATA tag? Thanks much!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @jkrall

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry... I can't remember. This was over 4 years ago!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries – thanks for replying though! :)

}

XUnit.prototype.__proto__ = Base.prototype;