Commit 1d116e5c 1d116e5c18f3538a23fbb3ac9c77a8f29818a4c1 by Nicolas Perriault

closes #42 - Use file name of test script as 'classname' in JUnit XML report (@mpeltonen)

1 parent c2294706
......@@ -47,7 +47,7 @@ var Tester = function(casper, options) {
}
this.currentTestFile = null;
this.exporter = require('xunit').create(casper);
this.exporter = require('xunit').create();
this.running = false;
this.suites = [];
this.options = utils.mergeObjects({
......@@ -69,8 +69,13 @@ var Tester = function(casper, options) {
casper.test.done();
});
this.on('fail', function(details) {
this.testResults.failures.push(details);
this.on('success', function(success) {
this.exporter.addSuccess(success.file, success.message);
});
this.on('fail', function(failure) {
this.exporter.addFailure(failure.file, failure.message, failure.details || "test failed", failure.type || "unknown");
this.testResults.failures.push(failure);
});
// methods
......@@ -86,15 +91,15 @@ var Tester = function(casper, options) {
eventName = 'success';
style = 'INFO';
this.testResults.passed++;
this.exporter.addSuccess(message);
} else {
eventName = 'fail';
status = this.options.failText;
style = 'RED_BAR';
this.testResults.failed++;
this.exporter.addFailure(message, 'test failed', "assert");
}
this.emit(eventName, {
type: "assert",
details: "test failed",
message: message,
file: this.currentTestFile
});
......@@ -114,17 +119,17 @@ var Tester = function(casper, options) {
eventName = "success";
casper.echo(this.colorize(this.options.passText, 'INFO') + ' ' + this.formatMessage(message));
this.testResults.passed++;
this.exporter.addSuccess(message);
} else {
eventName = "fail";
casper.echo(this.colorize(this.options.failText, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING'));
this.comment(' got: ' + utils.serialize(testValue));
this.comment(' expected: ' + utils.serialize(expected));
this.testResults.failed++;
this.exporter.addFailure(message, f("test failed; expected: %s; got: %s", expected, testValue), "assertEquals");
}
this.emit(eventName, {
type: "assertEquals",
message: message,
details: f("test failed; expected: %s; got: %s", expected, testValue),
file: this.currentTestFile
});
};
......@@ -175,17 +180,17 @@ var Tester = function(casper, options) {
eventName = "success";
casper.echo(this.colorize(this.options.passText, 'INFO') + ' ' + this.formatMessage(message));
this.testResults.passed++;
this.exporter.addSuccess(message);
} else {
eventName = "fail";
casper.echo(this.colorize(this.options.failText, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING'));
this.comment(' subject: ' + subject);
this.comment(' pattern: ' + pattern.toString());
this.testResults.failed++;
this.exporter.addFailure(message, f("test failed; subject: %s; pattern: %s", subject, pattern.toString()), "assertMatch");
}
this.emit(eventName, {
type: "assertMatch",
message: message,
details: f("test failed; subject: %s; pattern: %s", subject, pattern.toString()),
file: this.currentTestFile
});
};
......@@ -463,7 +468,7 @@ var Tester = function(casper, options) {
}
if (exit === true) {
console.log('exiting with status: ' + exitStatus);
casper.exit(~~exitStatus);
casper.exit(exitStatus);
}
};
......
......@@ -31,16 +31,15 @@
var utils = require('utils');
var fs = require('fs');
exports.create = function(casper) {
return new XUnitExporter(casper);
exports.create = function() {
return new XUnitExporter();
};
/**
* JUnit XML (xUnit) exporter for test results.
*
*/
XUnitExporter = function(casper) {
this._casper = casper
XUnitExporter = function() {
this._xml = utils.node('testsuite');
this._xml.toString = function() {
return this.outerHTML; // ouch
......@@ -49,29 +48,29 @@ XUnitExporter = function(casper) {
exports.XUnitExporter = XUnitExporter;
/**
* Adds a successful test result
* Adds a successful test result.
*
* @param String classname
* @param String name
*/
XUnitExporter.prototype.addSuccess = function(name) {
XUnitExporter.prototype.addSuccess = function(classname, name) {
this._xml.appendChild(utils.node('testcase', {
classname: generateClassName(this._casper),
classname: generateClassName(classname),
name: name
}));
};
/**
* Adds a failed test result
* Adds a failed test result.
*
* @param String classname
* @param String name
* @param String message
* @param String type
*/
XUnitExporter.prototype.addFailure = function(name, message, type) {
XUnitExporter.prototype.addFailure = function(classname, name, message, type) {
var fnode = utils.node('testcase', {
classname: generateClassName(this._casper),
classname: generateClassName(classname),
name: name
});
var failure = utils.node('failure', {
......@@ -83,20 +82,21 @@ XUnitExporter.prototype.addFailure = function(name, message, type) {
};
/**
* Generates a value for 'classname' attribute of the JUnit XML report
* Generates a value for 'classname' attribute of the JUnit XML report.
*
* Uses the (relative) file name of the current casper script without file
* extension as classname.
*
* @params Casper
* @param String classname
* @return String
*/
function generateClassName(casper) {
var script = casper.test.currentTestFile || phantom.casperScript || "unknown";
if (script.indexOf(fs.workingDirectory) === 0) {
script = script.substring(fs.workingDirectory.length + 1);
}
return script.substring(0, script.lastIndexOf('.'));
function generateClassName(classname) {
var script = classname || phantom.casperScript;
if (script.indexOf(fs.workingDirectory) === 0) {
script = script.substring(fs.workingDirectory.length + 1);
return script.substring(0, script.lastIndexOf('.'));
}
return classname;
}
/**
......
casper.test.comment('phantom.Casper.XUnitExporter');
xunit = require('xunit').create(casper);
xunit.addSuccess('bar');
casper.test.assertMatch(xunit.getXML(), /<testcase classname="tests\/suites\/xunit" name="bar"/, 'XUnitExporter.addSuccess() adds a successful testcase');
xunit.addFailure('baz', 'wrong', 'chucknorriz');
casper.test.assertMatch(xunit.getXML(), /<testcase classname="tests\/suites\/xunit" name="baz"><failure type="chucknorriz">wrong/, 'XUnitExporter.addFailure() adds a failed testcase');
xunit = require('xunit').create();
xunit.addSuccess('foo', 'bar');
casper.test.assertMatch(xunit.getXML(), /<testcase classname="foo" name="bar"/, 'XUnitExporter.addSuccess() adds a successful testcase');
xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz');
casper.test.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'XUnitExporter.addFailure() adds a failed testcase');
casper.test.done();
// named classname
xunit.addSuccess(require('fs').workingDirectory + '/plop.js', 'It worked');
casper.test.assertMatch(xunit.getXML(), /<testcase classname="plop" name="It worked"/, 'XUnitExporter.addSuccess() handles class name');
casper.test.done();
\ No newline at end of file
......