-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Xunit reporter #179
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)); | ||
|
||
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) + ']]>'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @jkrall! Any reason to wrap already escaped data inside a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ping @jkrall There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry... I can't remember. This was over 4 years ago! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No worries – thanks for replying though! :) |
||
} | ||
|
||
XUnit.prototype.__proto__ = Base.prototype; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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