Skip to content

Instantly share code, notes, and snippets.

@eriwen
Created November 14, 2011 02:33
Show Gist options
  • Save eriwen/1363104 to your computer and use it in GitHub Desktop.
Save eriwen/1363104 to your computer and use it in GitHub Desktop.
JUnit XML output for QUnit tests compatible with PhantomJS 1.3
var module, moduleStart, testStart, testCases = [],
current_test_assertions = [];
console.log('<?xml version="1.0" encoding="UTF-8"?>');
console.log('<testsuites name="testsuites">');
QUnit.begin = function() {
// That does not work when invoked in PhantomJS
}
QUnit.moduleStart = function(context) {
moduleStart = new Date();
module = context.name;
testCases = [];
}
QUnit.moduleDone = function(context) {
// context = { name, failed, passed, total }
var xml = '\t<testsuite name="' + context.name + '" errors="0" failures="' + context.failed + '" tests="' + context.total + '" time="' + (new Date() - moduleStart) / 1000 + '"';
if (testCases.length) {
xml += '>\n';
for (var i = 0, l = testCases.length; i < l; i++) {
xml += testCases[i];
}
xml += '\t</testsuite>';
} else {
xml += '/>\n';
}
console.log(xml);
}
QUnit.testStart = function() {
testStart = new Date();
}
QUnit.testDone = function(result) {
// result = { name, failed, passed, total }
var xml = '\t\t<testcase classname="' + module + '" name="' + result.name + '" time="' + (new Date() - testStart) / 1000 + '"';
if (result.failed) {
xml += '>\n';
for (var i = 0; i < current_test_assertions.length; i++) {
xml += "\t\t\t" + current_test_assertions[i];
}
xml += '\t\t</testcase>\n';
} else {
xml += '/>\n';
}
current_test_assertions = [];
testCases.push(xml);
};
QUnit.log = function(details) {
//details = { result , actual, expected, message }
if (details.result) {
return;
}
var message = details.message || "";
if (details.expected) {
if (message) {
message += ", ";
}
message = "expected: " + details.expected + ", but was: " + details.actual;
}
var xml = '<failure type="failed" message="' + message + '"/>\n'
current_test_assertions.push(xml);
};
QUnit.done = function(result) {
console.log('</testsuites>');
return result.failed > 0 ? 1 : 0;
};
@anbotero
Copy link

I was just reading your post, and some others, and just wrote again looking for “qunit junit xml”, and this gist appeared as fourth result.

Marvelous! Time to test!

@eriwen
Copy link
Author

eriwen commented Nov 14, 2011 via email

Copy link

ghost commented Apr 25, 2012

Hi,

This looks like what I'm looking for. Can you please paste some examples on how to implement this?

Thanks,
Cas

@jjoe64
Copy link

jjoe64 commented Apr 25, 2012

I'm also asking me, how to use this?

@jjoe64
Copy link

jjoe64 commented Apr 25, 2012

simply include it in your test/index.html

But how to configure jenkins ? (I'm using grunt)

@jjoe64
Copy link

jjoe64 commented Apr 25, 2012

ok I got it.
you need to create a script for phantomjs that opens your test.html. the console-output of your test.html must be catched. That must be parsed from jenkins.

@martindrapeau
Copy link

This is great. However do note that variables moduleStart and testStart have the same names as QUnit functions. Had to rename them to properly work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment