closes #42 - Use file name of test script as 'classname' in JUnit XML report (@mpeltonen)
Showing
3 changed files
with
43 additions
and
34 deletions
... | @@ -47,7 +47,7 @@ var Tester = function(casper, options) { | ... | @@ -47,7 +47,7 @@ var Tester = function(casper, options) { |
47 | } | 47 | } |
48 | 48 | ||
49 | this.currentTestFile = null; | 49 | this.currentTestFile = null; |
50 | this.exporter = require('xunit').create(casper); | 50 | this.exporter = require('xunit').create(); |
51 | this.running = false; | 51 | this.running = false; |
52 | this.suites = []; | 52 | this.suites = []; |
53 | this.options = utils.mergeObjects({ | 53 | this.options = utils.mergeObjects({ |
... | @@ -69,8 +69,13 @@ var Tester = function(casper, options) { | ... | @@ -69,8 +69,13 @@ var Tester = function(casper, options) { |
69 | casper.test.done(); | 69 | casper.test.done(); |
70 | }); | 70 | }); |
71 | 71 | ||
72 | this.on('fail', function(details) { | 72 | this.on('success', function(success) { |
73 | this.testResults.failures.push(details); | 73 | this.exporter.addSuccess(success.file, success.message); |
74 | }); | ||
75 | |||
76 | this.on('fail', function(failure) { | ||
77 | this.exporter.addFailure(failure.file, failure.message, failure.details || "test failed", failure.type || "unknown"); | ||
78 | this.testResults.failures.push(failure); | ||
74 | }); | 79 | }); |
75 | 80 | ||
76 | // methods | 81 | // methods |
... | @@ -86,15 +91,15 @@ var Tester = function(casper, options) { | ... | @@ -86,15 +91,15 @@ var Tester = function(casper, options) { |
86 | eventName = 'success'; | 91 | eventName = 'success'; |
87 | style = 'INFO'; | 92 | style = 'INFO'; |
88 | this.testResults.passed++; | 93 | this.testResults.passed++; |
89 | this.exporter.addSuccess(message); | ||
90 | } else { | 94 | } else { |
91 | eventName = 'fail'; | 95 | eventName = 'fail'; |
92 | status = this.options.failText; | 96 | status = this.options.failText; |
93 | style = 'RED_BAR'; | 97 | style = 'RED_BAR'; |
94 | this.testResults.failed++; | 98 | this.testResults.failed++; |
95 | this.exporter.addFailure(message, 'test failed', "assert"); | ||
96 | } | 99 | } |
97 | this.emit(eventName, { | 100 | this.emit(eventName, { |
101 | type: "assert", | ||
102 | details: "test failed", | ||
98 | message: message, | 103 | message: message, |
99 | file: this.currentTestFile | 104 | file: this.currentTestFile |
100 | }); | 105 | }); |
... | @@ -114,17 +119,17 @@ var Tester = function(casper, options) { | ... | @@ -114,17 +119,17 @@ var Tester = function(casper, options) { |
114 | eventName = "success"; | 119 | eventName = "success"; |
115 | casper.echo(this.colorize(this.options.passText, 'INFO') + ' ' + this.formatMessage(message)); | 120 | casper.echo(this.colorize(this.options.passText, 'INFO') + ' ' + this.formatMessage(message)); |
116 | this.testResults.passed++; | 121 | this.testResults.passed++; |
117 | this.exporter.addSuccess(message); | ||
118 | } else { | 122 | } else { |
119 | eventName = "fail"; | 123 | eventName = "fail"; |
120 | casper.echo(this.colorize(this.options.failText, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING')); | 124 | casper.echo(this.colorize(this.options.failText, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING')); |
121 | this.comment(' got: ' + utils.serialize(testValue)); | 125 | this.comment(' got: ' + utils.serialize(testValue)); |
122 | this.comment(' expected: ' + utils.serialize(expected)); | 126 | this.comment(' expected: ' + utils.serialize(expected)); |
123 | this.testResults.failed++; | 127 | this.testResults.failed++; |
124 | this.exporter.addFailure(message, f("test failed; expected: %s; got: %s", expected, testValue), "assertEquals"); | ||
125 | } | 128 | } |
126 | this.emit(eventName, { | 129 | this.emit(eventName, { |
130 | type: "assertEquals", | ||
127 | message: message, | 131 | message: message, |
132 | details: f("test failed; expected: %s; got: %s", expected, testValue), | ||
128 | file: this.currentTestFile | 133 | file: this.currentTestFile |
129 | }); | 134 | }); |
130 | }; | 135 | }; |
... | @@ -175,17 +180,17 @@ var Tester = function(casper, options) { | ... | @@ -175,17 +180,17 @@ var Tester = function(casper, options) { |
175 | eventName = "success"; | 180 | eventName = "success"; |
176 | casper.echo(this.colorize(this.options.passText, 'INFO') + ' ' + this.formatMessage(message)); | 181 | casper.echo(this.colorize(this.options.passText, 'INFO') + ' ' + this.formatMessage(message)); |
177 | this.testResults.passed++; | 182 | this.testResults.passed++; |
178 | this.exporter.addSuccess(message); | ||
179 | } else { | 183 | } else { |
180 | eventName = "fail"; | 184 | eventName = "fail"; |
181 | casper.echo(this.colorize(this.options.failText, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING')); | 185 | casper.echo(this.colorize(this.options.failText, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING')); |
182 | this.comment(' subject: ' + subject); | 186 | this.comment(' subject: ' + subject); |
183 | this.comment(' pattern: ' + pattern.toString()); | 187 | this.comment(' pattern: ' + pattern.toString()); |
184 | this.testResults.failed++; | 188 | this.testResults.failed++; |
185 | this.exporter.addFailure(message, f("test failed; subject: %s; pattern: %s", subject, pattern.toString()), "assertMatch"); | ||
186 | } | 189 | } |
187 | this.emit(eventName, { | 190 | this.emit(eventName, { |
191 | type: "assertMatch", | ||
188 | message: message, | 192 | message: message, |
193 | details: f("test failed; subject: %s; pattern: %s", subject, pattern.toString()), | ||
189 | file: this.currentTestFile | 194 | file: this.currentTestFile |
190 | }); | 195 | }); |
191 | }; | 196 | }; |
... | @@ -463,7 +468,7 @@ var Tester = function(casper, options) { | ... | @@ -463,7 +468,7 @@ var Tester = function(casper, options) { |
463 | } | 468 | } |
464 | if (exit === true) { | 469 | if (exit === true) { |
465 | console.log('exiting with status: ' + exitStatus); | 470 | console.log('exiting with status: ' + exitStatus); |
466 | casper.exit(~~exitStatus); | 471 | casper.exit(exitStatus); |
467 | } | 472 | } |
468 | }; | 473 | }; |
469 | 474 | ... | ... |
... | @@ -31,16 +31,15 @@ | ... | @@ -31,16 +31,15 @@ |
31 | var utils = require('utils'); | 31 | var utils = require('utils'); |
32 | var fs = require('fs'); | 32 | var fs = require('fs'); |
33 | 33 | ||
34 | exports.create = function(casper) { | 34 | exports.create = function() { |
35 | return new XUnitExporter(casper); | 35 | return new XUnitExporter(); |
36 | }; | 36 | }; |
37 | 37 | ||
38 | /** | 38 | /** |
39 | * JUnit XML (xUnit) exporter for test results. | 39 | * JUnit XML (xUnit) exporter for test results. |
40 | * | 40 | * |
41 | */ | 41 | */ |
42 | XUnitExporter = function(casper) { | 42 | XUnitExporter = function() { |
43 | this._casper = casper | ||
44 | this._xml = utils.node('testsuite'); | 43 | this._xml = utils.node('testsuite'); |
45 | this._xml.toString = function() { | 44 | this._xml.toString = function() { |
46 | return this.outerHTML; // ouch | 45 | return this.outerHTML; // ouch |
... | @@ -49,29 +48,29 @@ XUnitExporter = function(casper) { | ... | @@ -49,29 +48,29 @@ XUnitExporter = function(casper) { |
49 | exports.XUnitExporter = XUnitExporter; | 48 | exports.XUnitExporter = XUnitExporter; |
50 | 49 | ||
51 | /** | 50 | /** |
52 | * Adds a successful test result | 51 | * Adds a successful test result. |
53 | * | 52 | * |
54 | * @param String classname | 53 | * @param String classname |
55 | * @param String name | 54 | * @param String name |
56 | */ | 55 | */ |
57 | XUnitExporter.prototype.addSuccess = function(name) { | 56 | XUnitExporter.prototype.addSuccess = function(classname, name) { |
58 | this._xml.appendChild(utils.node('testcase', { | 57 | this._xml.appendChild(utils.node('testcase', { |
59 | classname: generateClassName(this._casper), | 58 | classname: generateClassName(classname), |
60 | name: name | 59 | name: name |
61 | })); | 60 | })); |
62 | }; | 61 | }; |
63 | 62 | ||
64 | /** | 63 | /** |
65 | * Adds a failed test result | 64 | * Adds a failed test result. |
66 | * | 65 | * |
67 | * @param String classname | 66 | * @param String classname |
68 | * @param String name | 67 | * @param String name |
69 | * @param String message | 68 | * @param String message |
70 | * @param String type | 69 | * @param String type |
71 | */ | 70 | */ |
72 | XUnitExporter.prototype.addFailure = function(name, message, type) { | 71 | XUnitExporter.prototype.addFailure = function(classname, name, message, type) { |
73 | var fnode = utils.node('testcase', { | 72 | var fnode = utils.node('testcase', { |
74 | classname: generateClassName(this._casper), | 73 | classname: generateClassName(classname), |
75 | name: name | 74 | name: name |
76 | }); | 75 | }); |
77 | var failure = utils.node('failure', { | 76 | var failure = utils.node('failure', { |
... | @@ -83,20 +82,21 @@ XUnitExporter.prototype.addFailure = function(name, message, type) { | ... | @@ -83,20 +82,21 @@ XUnitExporter.prototype.addFailure = function(name, message, type) { |
83 | }; | 82 | }; |
84 | 83 | ||
85 | /** | 84 | /** |
86 | * Generates a value for 'classname' attribute of the JUnit XML report | 85 | * Generates a value for 'classname' attribute of the JUnit XML report. |
87 | * | 86 | * |
88 | * Uses the (relative) file name of the current casper script without file | 87 | * Uses the (relative) file name of the current casper script without file |
89 | * extension as classname. | 88 | * extension as classname. |
90 | * | 89 | * |
91 | * @params Casper | 90 | * @param String classname |
92 | * @return String | 91 | * @return String |
93 | */ | 92 | */ |
94 | function generateClassName(casper) { | 93 | function generateClassName(classname) { |
95 | var script = casper.test.currentTestFile || phantom.casperScript || "unknown"; | 94 | var script = classname || phantom.casperScript; |
96 | if (script.indexOf(fs.workingDirectory) === 0) { | 95 | if (script.indexOf(fs.workingDirectory) === 0) { |
97 | script = script.substring(fs.workingDirectory.length + 1); | 96 | script = script.substring(fs.workingDirectory.length + 1); |
98 | } | 97 | return script.substring(0, script.lastIndexOf('.')); |
99 | return script.substring(0, script.lastIndexOf('.')); | 98 | } |
99 | return classname; | ||
100 | } | 100 | } |
101 | 101 | ||
102 | /** | 102 | /** | ... | ... |
1 | casper.test.comment('phantom.Casper.XUnitExporter'); | 1 | casper.test.comment('phantom.Casper.XUnitExporter'); |
2 | 2 | ||
3 | xunit = require('xunit').create(casper); | 3 | xunit = require('xunit').create(); |
4 | xunit.addSuccess('bar'); | 4 | xunit.addSuccess('foo', 'bar'); |
5 | casper.test.assertMatch(xunit.getXML(), /<testcase classname="tests\/suites\/xunit" name="bar"/, 'XUnitExporter.addSuccess() adds a successful testcase'); | 5 | casper.test.assertMatch(xunit.getXML(), /<testcase classname="foo" name="bar"/, 'XUnitExporter.addSuccess() adds a successful testcase'); |
6 | xunit.addFailure('baz', 'wrong', 'chucknorriz'); | 6 | xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz'); |
7 | casper.test.assertMatch(xunit.getXML(), /<testcase classname="tests\/suites\/xunit" name="baz"><failure type="chucknorriz">wrong/, 'XUnitExporter.addFailure() adds a failed testcase'); | 7 | casper.test.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'XUnitExporter.addFailure() adds a failed testcase'); |
8 | 8 | ||
9 | casper.test.done(); | 9 | // named classname |
10 | xunit.addSuccess(require('fs').workingDirectory + '/plop.js', 'It worked'); | ||
11 | casper.test.assertMatch(xunit.getXML(), /<testcase classname="plop" name="It worked"/, 'XUnitExporter.addSuccess() handles class name'); | ||
12 | |||
13 | casper.test.done(); | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or sign in to post a comment