Skip to content

Instantly share code, notes, and snippets.

@noonat
Forked from tj/jspec.junit.js
Created November 14, 2009 18:45
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 noonat/234693 to your computer and use it in GitHub Desktop.
Save noonat/234693 to your computer and use it in GitHub Desktop.
JUnit XML output for JSpec
/*
Output:
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="core.events" tests="2" assertions="2" failures="0" specs="2">
<testcase name="should be an instance of QueuedEventPool" assertions="1"/>
<testcase name="should have the correct name" assertions="1"/>
</testsuite>
...
</testsuites>
*/
(function(){
JSpec.include({
name: 'JUnit',
formatters: {
JUnitXml: function(results, options) {
var w = new java.io.FileWriter('jspec.xml');
w.write('<?xml version="1.0" encoding="UTF-8"?>\n');
w.write('<testsuites>\n');
JSpec.each(results.allSuites, function(suite) {
var attribs = {
name: suite.description,
tests: suite.specs.length,
assertions: 0,
failures: 0,
specs: 0};
var content = JSpec.inject(suite.specs, '', function(content, spec) {
attribs.assertions += spec.assertions.length;
attribs.failures += spec.passed() ? 0 : 1;
attribs.specs += 1;
return content +' <testcase name="'+spec.description+'" assertions="'+spec.assertions.length+'"/>\n';
});
w.write(' <testsuite');
for (var key in attribs) {
w.write(' ' + key + '="' + attribs[key] + '"');
}
w.write('>\n');
w.write(content);
w.write(' </testsuite>\n');
});
w.write('</testsuites>\n');
w.close();
quit(results.stats.failures);
}
}
});
})();
...
function runSuites() {
JSpec
.exec('spec.js')
.run({ formatter : JSpec.formatters.JUnitXML })
.report();
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment