Use file name of test script as 'classname' in JUnit XML report
Showing
3 changed files
with
38 additions
and
19 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(); | 50 | this.exporter = require('xunit').create(casper); |
51 | this.running = false; | 51 | this.running = false; |
52 | this.suites = []; | 52 | this.suites = []; |
53 | this.options = utils.mergeObjects({ | 53 | this.options = utils.mergeObjects({ |
... | @@ -86,13 +86,13 @@ var Tester = function(casper, options) { | ... | @@ -86,13 +86,13 @@ var Tester = function(casper, options) { |
86 | eventName = 'success'; | 86 | eventName = 'success'; |
87 | style = 'INFO'; | 87 | style = 'INFO'; |
88 | this.testResults.passed++; | 88 | this.testResults.passed++; |
89 | this.exporter.addSuccess("unknown", message); | 89 | this.exporter.addSuccess(message); |
90 | } else { | 90 | } else { |
91 | eventName = 'fail'; | 91 | eventName = 'fail'; |
92 | status = this.options.failText; | 92 | status = this.options.failText; |
93 | style = 'RED_BAR'; | 93 | style = 'RED_BAR'; |
94 | this.testResults.failed++; | 94 | this.testResults.failed++; |
95 | this.exporter.addFailure("unknown", message, 'test failed', "assert"); | 95 | this.exporter.addFailure(message, 'test failed', "assert"); |
96 | } | 96 | } |
97 | this.emit(eventName, { | 97 | this.emit(eventName, { |
98 | message: message, | 98 | message: message, |
... | @@ -114,14 +114,14 @@ var Tester = function(casper, options) { | ... | @@ -114,14 +114,14 @@ var Tester = function(casper, options) { |
114 | eventName = "success"; | 114 | eventName = "success"; |
115 | casper.echo(this.colorize(this.options.passText, 'INFO') + ' ' + this.formatMessage(message)); | 115 | casper.echo(this.colorize(this.options.passText, 'INFO') + ' ' + this.formatMessage(message)); |
116 | this.testResults.passed++; | 116 | this.testResults.passed++; |
117 | this.exporter.addSuccess("unknown", message); | 117 | this.exporter.addSuccess(message); |
118 | } else { | 118 | } else { |
119 | eventName = "fail"; | 119 | eventName = "fail"; |
120 | casper.echo(this.colorize(this.options.failText, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING')); | 120 | casper.echo(this.colorize(this.options.failText, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING')); |
121 | this.comment(' got: ' + utils.serialize(testValue)); | 121 | this.comment(' got: ' + utils.serialize(testValue)); |
122 | this.comment(' expected: ' + utils.serialize(expected)); | 122 | this.comment(' expected: ' + utils.serialize(expected)); |
123 | this.testResults.failed++; | 123 | this.testResults.failed++; |
124 | this.exporter.addFailure("unknown", message, f("test failed; expected: %s; got: %s", expected, testValue), "assertEquals"); | 124 | this.exporter.addFailure(message, f("test failed; expected: %s; got: %s", expected, testValue), "assertEquals"); |
125 | } | 125 | } |
126 | this.emit(eventName, { | 126 | this.emit(eventName, { |
127 | message: message, | 127 | message: message, |
... | @@ -175,14 +175,14 @@ var Tester = function(casper, options) { | ... | @@ -175,14 +175,14 @@ var Tester = function(casper, options) { |
175 | eventName = "success"; | 175 | eventName = "success"; |
176 | casper.echo(this.colorize(this.options.passText, 'INFO') + ' ' + this.formatMessage(message)); | 176 | casper.echo(this.colorize(this.options.passText, 'INFO') + ' ' + this.formatMessage(message)); |
177 | this.testResults.passed++; | 177 | this.testResults.passed++; |
178 | this.exporter.addSuccess("unknown", message); | 178 | this.exporter.addSuccess(message); |
179 | } else { | 179 | } else { |
180 | eventName = "fail"; | 180 | eventName = "fail"; |
181 | casper.echo(this.colorize(this.options.failText, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING')); | 181 | casper.echo(this.colorize(this.options.failText, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING')); |
182 | this.comment(' subject: ' + subject); | 182 | this.comment(' subject: ' + subject); |
183 | this.comment(' pattern: ' + pattern.toString()); | 183 | this.comment(' pattern: ' + pattern.toString()); |
184 | this.testResults.failed++; | 184 | this.testResults.failed++; |
185 | this.exporter.addFailure("unknown", message, f("test failed; subject: %s; pattern: %s", subject, pattern.toString()), "assertMatch"); | 185 | this.exporter.addFailure(message, f("test failed; subject: %s; pattern: %s", subject, pattern.toString()), "assertMatch"); |
186 | } | 186 | } |
187 | this.emit(eventName, { | 187 | this.emit(eventName, { |
188 | message: message, | 188 | message: message, | ... | ... |
... | @@ -29,16 +29,18 @@ | ... | @@ -29,16 +29,18 @@ |
29 | */ | 29 | */ |
30 | 30 | ||
31 | var utils = require('utils'); | 31 | var utils = require('utils'); |
32 | var fs = require('fs'); | ||
32 | 33 | ||
33 | exports.create = function() { | 34 | exports.create = function(casper) { |
34 | return new XUnitExporter(); | 35 | return new XUnitExporter(casper); |
35 | }; | 36 | }; |
36 | 37 | ||
37 | /** | 38 | /** |
38 | * JUnit XML (xUnit) exporter for test results. | 39 | * JUnit XML (xUnit) exporter for test results. |
39 | * | 40 | * |
40 | */ | 41 | */ |
41 | XUnitExporter = function() { | 42 | XUnitExporter = function(casper) { |
43 | this._casper = casper | ||
42 | this._xml = utils.node('testsuite'); | 44 | this._xml = utils.node('testsuite'); |
43 | this._xml.toString = function() { | 45 | this._xml.toString = function() { |
44 | return this.outerHTML; // ouch | 46 | return this.outerHTML; // ouch |
... | @@ -52,9 +54,9 @@ exports.XUnitExporter = XUnitExporter; | ... | @@ -52,9 +54,9 @@ exports.XUnitExporter = XUnitExporter; |
52 | * @param String classname | 54 | * @param String classname |
53 | * @param String name | 55 | * @param String name |
54 | */ | 56 | */ |
55 | XUnitExporter.prototype.addSuccess = function(classname, name) { | 57 | XUnitExporter.prototype.addSuccess = function(name) { |
56 | this._xml.appendChild(utils.node('testcase', { | 58 | this._xml.appendChild(utils.node('testcase', { |
57 | classname: classname, | 59 | classname: generateClassName(this._casper), |
58 | name: name | 60 | name: name |
59 | })); | 61 | })); |
60 | }; | 62 | }; |
... | @@ -67,9 +69,9 @@ XUnitExporter.prototype.addSuccess = function(classname, name) { | ... | @@ -67,9 +69,9 @@ XUnitExporter.prototype.addSuccess = function(classname, name) { |
67 | * @param String message | 69 | * @param String message |
68 | * @param String type | 70 | * @param String type |
69 | */ | 71 | */ |
70 | XUnitExporter.prototype.addFailure = function(classname, name, message, type) { | 72 | XUnitExporter.prototype.addFailure = function(name, message, type) { |
71 | var fnode = utils.node('testcase', { | 73 | var fnode = utils.node('testcase', { |
72 | classname: classname, | 74 | classname: generateClassName(this._casper), |
73 | name: name | 75 | name: name |
74 | }); | 76 | }); |
75 | var failure = utils.node('failure', { | 77 | var failure = utils.node('failure', { |
... | @@ -81,6 +83,23 @@ XUnitExporter.prototype.addFailure = function(classname, name, message, type) { | ... | @@ -81,6 +83,23 @@ XUnitExporter.prototype.addFailure = function(classname, name, message, type) { |
81 | }; | 83 | }; |
82 | 84 | ||
83 | /** | 85 | /** |
86 | * Generates a value for 'classname' attribute of the JUnit XML report | ||
87 | * | ||
88 | * Uses the (relative) file name of the current casper script without file | ||
89 | * extension as classname. | ||
90 | * | ||
91 | * @params Casper | ||
92 | * @return String | ||
93 | */ | ||
94 | function generateClassName(casper) { | ||
95 | var script = casper.test.currentTestFile || phantom.casperScript || "unknown"; | ||
96 | if (script.indexOf(fs.workingDirectory) === 0) { | ||
97 | script = script.substring(fs.workingDirectory.length + 1); | ||
98 | } | ||
99 | return script.substring(0, script.lastIndexOf('.')); | ||
100 | } | ||
101 | |||
102 | /** | ||
84 | * Retrieves generated XML object - actually an HTMLElement. | 103 | * Retrieves generated XML object - actually an HTMLElement. |
85 | * | 104 | * |
86 | * @return HTMLElement | 105 | * @return HTMLElement | ... | ... |
1 | casper.test.comment('phantom.Casper.XUnitExporter'); | 1 | casper.test.comment('phantom.Casper.XUnitExporter'); |
2 | 2 | ||
3 | xunit = require('xunit').create(); | 3 | xunit = require('xunit').create(casper); |
4 | xunit.addSuccess('foo', 'bar'); | 4 | xunit.addSuccess('bar'); |
5 | casper.test.assertMatch(xunit.getXML(), /<testcase classname="foo" name="bar"/, 'XUnitExporter.addSuccess() adds a successful testcase'); | 5 | casper.test.assertMatch(xunit.getXML(), /<testcase classname="tests\/suites\/xunit" name="bar"/, 'XUnitExporter.addSuccess() adds a successful testcase'); |
6 | xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz'); | 6 | xunit.addFailure('baz', 'wrong', 'chucknorriz'); |
7 | casper.test.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'XUnitExporter.addFailure() adds a failed testcase'); | 7 | casper.test.assertMatch(xunit.getXML(), /<testcase classname="tests\/suites\/xunit" name="baz"><failure type="chucknorriz">wrong/, 'XUnitExporter.addFailure() adds a failed testcase'); |
8 | 8 | ||
9 | casper.test.done(); | 9 | casper.test.done(); | ... | ... |
-
Please register or sign in to post a comment