Created
November 14, 2011 02:33
-
-
Save eriwen/1363104 to your computer and use it in GitHub Desktop.
JUnit XML output for QUnit tests compatible with PhantomJS 1.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
Ha! Actually, I've had that around for a couple months now, but I
wanted a separate place for it for the talks I'm giving at RichWeb.
Guess what I'm trying to say is: "you'll find more goodies like that
in all of my GitHub JS projects, I just haven't announced or blogged
about them".
Have fun!
Cheers,
Eric
##
Eric M. Wendelin - http://eriwen.com - @eriwen
…On Sun, Nov 13, 2011 at 10:07 PM, Andrés Botero ***@***.*** wrote:
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!
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1363104
Hi,
This looks like what I'm looking for. Can you please paste some examples on how to implement this?
Thanks,
Cas
I'm also asking me, how to use this?
simply include it in your test/index.html
But how to configure jenkins ? (I'm using grunt)
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.
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
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!