Skip to content

Instantly share code, notes, and snippets.

@drslump
Created December 7, 2009 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drslump/251025 to your computer and use it in GitHub Desktop.
Save drslump/251025 to your computer and use it in GitHub Desktop.
/*
JSpec module which adds a new formatter (JSpec.formatters.jUnit) to generate an
xml document compatible with Hudson CI jUnit result browser.
(c) 2009 Iván -DrSlump- Montes <drslump _at_ pollinimini.net
Public Domain
*/
JSpec.include({
'formatters': {
jUnit: function(results, options) {
function callIterator(callback, a, b) {
return callback.length == 1 ? callback(b) : callback(a, b)
}
function each(object, callback) {
if (typeof object == 'string') object = object.split(' ')
for (key in object)
if (object.hasOwnProperty(key))
callIterator(callback, key, object[key])
}
var out = {};
out.$name = 'testsuites';
out.$childs = [];
each(results.allSuites, function(suite) {
if (suite.ran) {
var testsuite = {
$name: 'testsuite',
$childs: [],
name: suite.description,
tests: suite.specs.length,
assertions: 0,
failures: 0
};
each(suite.specs, function(spec){
var testcase = {
$name: 'testcase',
$childs: [],
name: spec.description,
assertions: spec.assertions.length
};
each(spec.assertions, function(assertion){
if (!assertion.passed) {
testcase.$childs.push({
$name: 'failure',
type: assertion.message
});
testsuite.failures++;
}
testsuite.assertions++;
});
testsuite.$childs.push(testcase);
});
out.$childs.push(testsuite);
}
});
function obj2xml(obj)
{
var xml = '<' + obj.$name + ' ';
for (var k in obj) if (obj.hasOwnProperty(k) && k.charAt(0) !== '$') {
xml += k + '="' + (''+obj[k]).replace(/&/g, '&amp;').replace(/"/g, '&quot;') + '" ';
}
if (obj.$childs && obj.$childs.length) {
xml += '>' + '\n';
for (var i=0; i<obj.$childs.length; i++) {
xml += obj2xml(obj.$childs[i]);
}
xml += '</' + obj.$name + '>' + '\n';
} else {
xml += '/>' + '\n';
}
return xml;
}
print('<?xml version="1.0" encoding="utf-8"?>\n');
print(obj2xml(out));
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment