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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
# Copyright (c) 2014 Intel Corporation
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# pylint: disable=missing-docstring
""" Tests for the backend package """
from __future__ import absolute_import, division, print_function
import os
try:
from lxml import etree
except ImportError:
import xml.etree.cElementTree as etree
import nose.tools as nt
from nose.plugins.skip import SkipTest
from framework import results, backends, grouptools, status
from . import utils
from .backends_tests import BACKEND_INITIAL_META
JUNIT_SCHEMA = os.path.join(os.path.dirname(__file__), 'schema', 'junit-7.xsd')
doc_formatter = utils.DocFormatter({'separator': grouptools.SEPARATOR})
_XML = """\
<?xml version='1.0' encoding='utf-8'?>
<testsuites>
<testsuite name="piglit" tests="1">
<testcase classname="piglit.foo.bar" name="a-test" status="pass" time="1.12345">
<system-out>this/is/a/command\nThis is stdout</system-out>
<system-err>this is stderr
pid: 1934
time start: 1.0
time end: 4.5
</system-err>
</testcase>
</testsuite>
</testsuites>
"""
def setup_module():
utils.set_compression('none')
def teardown_module():
utils.unset_compression()
class TestJunitNoTests(utils.StaticDirectory):
@classmethod
def setup_class(cls):
super(TestJunitNoTests, cls).setup_class()
test = backends.junit.JUnitBackend(cls.tdir)
test.initialize(BACKEND_INITIAL_META)
test.finalize()
cls.test_file = os.path.join(cls.tdir, 'results.xml')
@utils.no_error
def test_xml_well_formed(self):
"""backends.junit.JUnitBackend: initialize and finalize produce well formed xml
While it will produce valid XML, it cannot produc valid JUnit, since
JUnit requires at least one test to be valid
"""
etree.parse(self.test_file)
class TestJUnitSingleTest(TestJunitNoTests):
@classmethod
def setup_class(cls):
super(TestJUnitSingleTest, cls).setup_class()
cls.test_file = os.path.join(cls.tdir, 'results.xml')
result = results.TestResult()
result.time.end = 1.2345
result.result = 'pass'
result.out = 'this is stdout'
result.err = 'this is stderr'
result.command = 'foo'
result.pid = 1034
test = backends.junit.JUnitBackend(cls.tdir)
test.initialize(BACKEND_INITIAL_META)
with test.write_test(grouptools.join('a', 'test', 'group', 'test1')) as t:
t(result)
test.finalize()
def test_xml_well_formed(self):
"""backends.junit.JUnitBackend.write_test(): (once) produces well formed xml"""
super(TestJUnitSingleTest, self).test_xml_well_formed()
def test_xml_valid(self):
"""backends.junit.JUnitBackend.write_test(): (once) produces valid xml"""
utils.module_check('lxml')
schema = etree.XMLSchema(file=JUNIT_SCHEMA)
with open(self.test_file, 'r') as f:
nt.ok_(schema.validate(etree.parse(f)), msg='xml is not valid')
class TestJUnitMultiTest(TestJUnitSingleTest):
@classmethod
def setup_class(cls):
super(TestJUnitMultiTest, cls).setup_class()
result = results.TestResult()
result.time.end = 1.2345
result.result = 'pass'
result.out = 'this is stdout'
result.err = 'this is stderr'
result.command = 'foo'
result.pid = 1034
cls.test_file = os.path.join(cls.tdir, 'results.xml')
test = backends.junit.JUnitBackend(cls.tdir)
test.initialize(BACKEND_INITIAL_META)
with test.write_test(grouptools.join('a', 'test', 'group', 'test1')) as t:
t(result)
result.result = 'fail'
with test.write_test(
grouptools.join('a', 'different', 'test', 'group', 'test2')) as t:
t(result)
test.finalize()
def test_xml_well_formed(self):
"""backends.junit.JUnitBackend.write_test(): (twice) produces well formed xml"""
super(TestJUnitMultiTest, self).test_xml_well_formed()
def test_xml_valid(self):
"""backends.junit.JUnitBackend.write_test(): (twice) produces valid xml"""
super(TestJUnitMultiTest, self).test_xml_valid()
@doc_formatter
def test_junit_replace():
"""backends.junit.JUnitBackend.write_test(): '{separator}' is replaced with '.'"""
with utils.tempdir() as tdir:
result = results.TestResult()
result.time.end = 1.2345
result.result = 'pass'
result.out = 'this is stdout'
result.err = 'this is stderr'
result.command = 'foo'
test = backends.junit.JUnitBackend(tdir)
test.initialize(BACKEND_INITIAL_META)
with test.write_test(grouptools.join('a', 'test', 'group', 'test1')) as t:
t(result)
test.finalize()
test_value = etree.parse(os.path.join(tdir, 'results.xml')).getroot()
nt.assert_equal(test_value.find('.//testcase').attrib['classname'],
'piglit.a.test.group')
@utils.not_raises(etree.ParseError)
def test_junit_skips_bad_tests():
"""backends.junit.JUnitBackend: skips illformed tests"""
with utils.tempdir() as tdir:
result = results.TestResult()
result.time.end = 1.2345
result.result = 'pass'
result.out = 'this is stdout'
result.err = 'this is stderr'
result.command = 'foo'
test = backends.junit.JUnitBackend(tdir)
test.initialize(BACKEND_INITIAL_META)
with test.write_test(grouptools.join('a', 'test', 'group', 'test1')) as t:
t(result)
with open(os.path.join(tdir, 'tests', '1.xml'), 'w') as f:
f.write('bad data')
test.finalize()
class TestJUnitLoad(utils.StaticDirectory):
"""Methods that test loading JUnit results."""
__instance = None
@classmethod
def setup_class(cls):
super(TestJUnitLoad, cls).setup_class()
cls.xml_file = os.path.join(cls.tdir, 'results.xml')
with open(cls.xml_file, 'w') as f:
f.write(_XML)
cls.testname = grouptools.join('foo', 'bar', 'a-test')
@classmethod
def xml(cls):
if cls.__instance is None:
cls.__instance = backends.junit._load(cls.xml_file)
return cls.__instance
@utils.no_error
def test_no_errors(self):
"""backends.junit._load: Raises no errors for valid junit."""
self.xml()
def test_return_testrunresult(self):
"""backends.junit._load: returns a TestrunResult instance"""
nt.assert_is_instance(self.xml(), results.TestrunResult)
@doc_formatter
def test_replace_sep(self):
"""backends.junit._load: replaces '.' with '{separator}'"""
nt.assert_in(self.testname, self.xml().tests)
def test_testresult_instance(self):
"""backends.junit._load: replaces result with TestResult instance."""
nt.assert_is_instance(self.xml().tests[self.testname], results.TestResult)
def test_status_instance(self):
"""backends.junit._load: a status is found and loaded."""
nt.assert_is_instance(self.xml().tests[self.testname].result,
status.Status)
def test_time_start(self):
"""backends.junit._load: Time.start is loaded correctly."""
time = self.xml().tests[self.testname].time
nt.assert_is_instance(time, results.TimeAttribute)
nt.eq_(time.start, 1.0)
def test_time_end(self):
"""backends.junit._load: Time.end is loaded correctly."""
time = self.xml().tests[self.testname].time
nt.assert_is_instance(time, results.TimeAttribute)
nt.eq_(time.end, 4.5)
def test_command(self):
"""backends.junit._load: command is loaded correctly."""
test = self.xml().tests[self.testname].command
nt.assert_equal(test, 'this/is/a/command')
def test_out(self):
"""backends.junit._load: stdout is loaded correctly."""
test = self.xml().tests[self.testname].out
nt.assert_equal(test, 'This is stdout')
def test_err(self):
"""backends.junit._load: stderr is loaded correctly."""
test = self.xml().tests[self.testname].err
expected = ('this is stderr\n\n'
'pid: 1934\n'
'time start: 1.0\n'
'time end: 4.5\n'
' ')
nt.eq_(test, expected)
def test_totals(self):
"""backends.junit._load: Totals are calculated."""
nt.ok_(bool(self.xml()))
def test_pid(self):
"""backends.junit._load: pid is loaded correctly."""
test = self.xml().tests[self.testname].pid
nt.eq_(test, 1934)
@utils.no_error
def test_load_file(self):
"""backends.junit.load: Loads a file directly"""
backends.junit.REGISTRY.load(self.xml_file, 'none')
@utils.no_error
def test_load_dir(self):
"""backends.junit.load: Loads a directory"""
backends.junit.REGISTRY.load(self.tdir, 'none')
def test_load_file_name():
"""backends.junit._load: uses the filename for name if filename != 'results'
"""
with utils.tempdir() as tdir:
filename = os.path.join(tdir, 'foobar.xml')
with open(filename, 'w') as f:
f.write(_XML)
test = backends.junit.REGISTRY.load(filename, 'none')
nt.assert_equal(test.name, 'foobar')
def test_load_folder_name():
"""backends.junit._load: uses the folder name if the result is 'results'"""
with utils.tempdir() as tdir:
os.mkdir(os.path.join(tdir, 'a cool test'))
filename = os.path.join(tdir, 'a cool test', 'results.xml')
with open(filename, 'w') as f:
f.write(_XML)
test = backends.junit.REGISTRY.load(filename, 'none')
nt.assert_equal(test.name, 'a cool test')
@utils.test_in_tempdir
def test_load_default_name():
"""backends.junit._load: uses 'junit result' for name as fallback"""
with utils.tempdir() as tdir:
os.chdir(tdir)
filename = 'results.xml'
with open(filename, 'w') as f:
f.write(_XML)
test = backends.junit.REGISTRY.load(filename, 'none')
nt.assert_equal(test.name, 'junit result')
|