/
JUnitXmlResultWriterSpec.groovy
253 lines (220 loc) · 11 KB
/
JUnitXmlResultWriterSpec.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.internal.tasks.testing.junit.result
import org.gradle.api.internal.tasks.testing.BuildableTestResultsProvider
import org.gradle.api.internal.tasks.testing.results.DefaultTestResult
import org.gradle.integtests.fixtures.JUnitTestClassExecutionResult
import org.gradle.integtests.fixtures.TestResultOutputAssociation
import org.gradle.internal.SystemProperties
import spock.lang.Issue
import spock.lang.Specification
import spock.lang.Unroll
import static java.util.Collections.emptyList
import static org.gradle.api.tasks.testing.TestOutputEvent.Destination.StdErr
import static org.gradle.api.tasks.testing.TestOutputEvent.Destination.StdOut
import static org.gradle.api.tasks.testing.TestResult.ResultType.FAILURE
import static org.gradle.api.tasks.testing.TestResult.ResultType.SKIPPED
import static org.gradle.api.tasks.testing.TestResult.ResultType.SUCCESS
import static org.hamcrest.CoreMatchers.equalTo
class JUnitXmlResultWriterSpec extends Specification {
private provider = Mock(TestResultsProvider)
private options = new JUnitXmlResultOptions(false, false)
protected JUnitXmlResultWriter getGenerator() {
new JUnitXmlResultWriter("localhost", provider, options)
}
private startTime = 1353344968049
def "writes xml JUnit result"() {
TestClassResult result = new TestClassResult(1, "com.foo.FooTest", startTime)
result.add(new TestMethodResult(1, "some test", SUCCESS, 15, startTime + 25))
result.add(new TestMethodResult(2, "some test two", SUCCESS, 15, startTime + 30))
result.add(new TestMethodResult(3, "some failing test", FAILURE, 10, startTime + 40).addFailure("failure message", "[stack-trace]", "ExceptionType"))
result.add(new TestMethodResult(4, "some skipped test", SKIPPED, 10, startTime + 45))
provider.writeAllOutput(1, StdOut, _) >> { args -> args[2].write("1st output message\n2nd output message\n") }
provider.writeAllOutput(1, StdErr, _) >> { args -> args[2].write("err") }
when:
def xml = getXml(result)
then:
new JUnitTestClassExecutionResult(xml, "com.foo.FooTest", "com.foo.FooTest", TestResultOutputAssociation.WITH_SUITE)
.assertTestCount(4, 1, 1, 0)
.assertTestFailed("some failing test", equalTo('failure message'))
.assertTestsSkipped("some skipped test")
.assertTestsExecuted("some test", "some test two", "some failing test")
.assertStdout(equalTo("""1st output message
2nd output message
"""))
.assertStderr(equalTo("err"))
and:
xml == """<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="com.foo.FooTest" tests="4" skipped="1" failures="1" errors="0" timestamp="2012-11-19T17:09:28" hostname="localhost" time="0.045">
<properties/>
<testcase name="some test" classname="com.foo.FooTest" time="0.015"/>
<testcase name="some test two" classname="com.foo.FooTest" time="0.015"/>
<testcase name="some failing test" classname="com.foo.FooTest" time="0.01">
<failure message="failure message" type="ExceptionType">[stack-trace]</failure>
</testcase>
<testcase name="some skipped test" classname="com.foo.FooTest" time="0.01">
<skipped/>
</testcase>
<system-out><![CDATA[1st output message
2nd output message
]]></system-out>
<system-err><![CDATA[err]]></system-err>
</testsuite>
"""
}
def "writes results with empty outputs"() {
TestClassResult result = new TestClassResult(1, "com.foo.FooTest", startTime)
result.add(new TestMethodResult(1, "some test").completed(new DefaultTestResult(SUCCESS, startTime + 100, startTime + 300, 1, 1, 0, emptyList())))
_ * provider.writeAllOutput(_, _, _)
when:
def xml = getXml(result)
then:
xml == """<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="com.foo.FooTest" tests="1" skipped="0" failures="0" errors="0" timestamp="2012-11-19T17:09:28" hostname="localhost" time="0.3">
<properties/>
<testcase name="some test" classname="com.foo.FooTest" time="0.2"/>
<system-out><![CDATA[]]></system-out>
<system-err><![CDATA[]]></system-err>
</testsuite>
"""
}
def "encodes xml"() {
TestClassResult result = new TestClassResult(1, "com.foo.FooTest", startTime)
result.add(new TestMethodResult(1, "some \ud8d3\ude01 test", FAILURE, 200, 300).addFailure("<> encoded!\ud8d3\ude02", "<non ascii:\ud8d3\ude02 \u0302>", "<Exception\ud8d3\ude29>"))
provider.writeAllOutput(_, StdErr, _) >> { args -> args[2].write("with \ud8d3\ude31CDATA end token: ]]> some ascii: ż") }
provider.writeAllOutput(_, StdOut, _) >> { args -> args[2].write("with CDATA end token: ]]> some ascii: \ud8d3\udd20ż") }
when:
def xml = getXml(result)
then:
//attribute and text is encoded:
xml.contains('message="<> encoded!񄸂" type="<Exception񄸩>"><non ascii:񄸂 \u0302>')
//output encoded:
xml.contains('<system-out><![CDATA[with CDATA end token: ]]]]><![CDATA[> some ascii: ]]>񄴠<![CDATA[ż]]></system-out>')
xml.contains('<system-err><![CDATA[with ]]>񄸱<![CDATA[CDATA end token: ]]]]><![CDATA[> some ascii: ż]]></system-err>')
}
def "writes results with no tests"() {
TestClassResult result = new TestClassResult(1, "com.foo.IgnoredTest", startTime)
when:
def xml = getXml(result)
then:
xml == """<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="com.foo.IgnoredTest" tests="0" skipped="0" failures="0" errors="0" timestamp="2012-11-19T17:09:28" hostname="localhost" time="0.0">
<properties/>
<system-out><![CDATA[]]></system-out>
<system-err><![CDATA[]]></system-err>
</testsuite>
"""
}
def "can generate with output per test"() {
given:
options = new JUnitXmlResultOptions(true, false)
provider = new BuildableTestResultsProvider()
when:
def testClass = provider.testClassResult("com.Foo") {
stdout "class-out"
stderr "class-err"
testcase("m1") {
stderr " m1-err-1"
stdout " m1-out-1"
stdout " m1-out-2"
stderr " m1-err-2"
}
testcase("m2") {
stderr " m2-err-1"
stdout " m2-out-1"
stdout " m2-out-2"
stderr " m2-err-2"
}
}
then:
getXml(testClass) == """<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="com.Foo" tests="2" skipped="0" failures="0" errors="0" timestamp="1970-01-01T00:00:00" hostname="localhost" time="1.0">
<properties/>
<testcase name="m1" classname="com.Foo" time="1.0">
<system-out><![CDATA[ m1-out-1 m1-out-2]]></system-out>
<system-err><![CDATA[ m1-err-1 m1-err-2]]></system-err>
</testcase>
<testcase name="m2" classname="com.Foo" time="1.0">
<system-out><![CDATA[ m2-out-1 m2-out-2]]></system-out>
<system-err><![CDATA[ m2-err-1 m2-err-2]]></system-err>
</testcase>
<system-out><![CDATA[class-out]]></system-out>
<system-err><![CDATA[class-err]]></system-err>
</testsuite>
"""
}
def "can generate report with failed tests with no exception"() {
given:
TestClassResult result = new TestClassResult(1, "com.foo.FooTest", startTime)
result.add(new TestMethodResult(3, "some failing test", FAILURE, 10, startTime + 40))
when:
def xml = getXml(result)
then:
new JUnitTestClassExecutionResult(xml, "com.foo.FooTest", "com.foo.FooTest", TestResultOutputAssociation.WITH_SUITE)
.assertTestCount(1, 0, 1, 0)
.assertTestFailed("some failing test")
}
@Unroll
@Issue("gradle/gradle#11445")
def "writes #writtenName as class display name when #displayName is specified"() {
TestClassResult result = new TestClassResult(1, "com.foo.FooTest", displayName, startTime)
result.add(new TestMethodResult(1, "some test", "some test displayName", SUCCESS, 15, startTime + 25))
result.add(new TestMethodResult(2, "some test two", "some test two displayName", SUCCESS, 15, startTime + 30))
result.add(new TestMethodResult(3, "some failing test", "some failing test displayName", FAILURE, 10, startTime + 40).addFailure("failure message", "[stack-trace]", "ExceptionType"))
result.add(new TestMethodResult(4, "some skipped test", "some skipped test displayName", SKIPPED, 10, startTime + 45))
provider.writeAllOutput(1, StdOut, _) >> { args -> args[2].write("1st output message\n2nd output message\n") }
provider.writeAllOutput(1, StdErr, _) >> { args -> args[2].write("err") }
when:
def xml = getXml(result)
then:
new JUnitTestClassExecutionResult(xml, "com.foo.FooTest", writtenName, TestResultOutputAssociation.WITH_SUITE)
.assertTestCount(4, 1, 1, 0)
.assertTestFailed("some failing test displayName", equalTo('failure message'))
.assertTestsSkipped("some skipped test displayName")
.assertTestsExecuted("some test displayName", "some test two displayName", "some failing test displayName")
.assertStdout(equalTo("""1st output message
2nd output message
"""))
.assertStderr(equalTo("err"))
and:
xml == """<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="$writtenName" tests="4" skipped="1" failures="1" errors="0" timestamp="2012-11-19T17:09:28" hostname="localhost" time="0.045">
<properties/>
<testcase name="some test displayName" classname="com.foo.FooTest" time="0.015"/>
<testcase name="some test two displayName" classname="com.foo.FooTest" time="0.015"/>
<testcase name="some failing test displayName" classname="com.foo.FooTest" time="0.01">
<failure message="failure message" type="ExceptionType">[stack-trace]</failure>
</testcase>
<testcase name="some skipped test displayName" classname="com.foo.FooTest" time="0.01">
<skipped/>
</testcase>
<system-out><![CDATA[1st output message
2nd output message
]]></system-out>
<system-err><![CDATA[err]]></system-err>
</testsuite>
"""
where:
displayName | writtenName
'FooTest' | 'com.foo.FooTest'
'custom display name' | 'custom display name'
}
def getXml(TestClassResult result) {
def text = new ByteArrayOutputStream()
generator.write(result, text)
return text.toString("UTF-8").replace(SystemProperties.instance.lineSeparator, "\n")
}
}