Skip to content

Commit 59a7b57

Browse files
committed
respect junit xsd (issue #90)
1 parent 49c957e commit 59a7b57

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

tests/testsuite.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,25 @@ def test_junitxml_properties(self):
305305
suite.properties = dict(key='value')
306306
self._test_xmlrunner(suite)
307307

308+
def test_junitxml_xsd_validation(self):
309+
suite = unittest.TestSuite()
310+
suite.addTest(self.DummyTest('test_fail'))
311+
suite.addTest(self.DummyTest('test_pass'))
312+
suite.properties = dict(key='value')
313+
outdir = BytesIO()
314+
runner = xmlrunner.XMLTestRunner(
315+
stream=self.stream, output=outdir, verbosity=self.verbosity,
316+
**self.runner_kwargs)
317+
runner.run(suite)
318+
outdir.seek(0)
319+
output = outdir.read()
320+
# poor man's schema validation; see issue #90
321+
i_properties = output.index('<properties>'.encode('utf8'))
322+
i_system_out = output.index('<system-out>'.encode('utf8'))
323+
i_system_err = output.index('<system-err>'.encode('utf8'))
324+
i_testcase = output.index('<testcase'.encode('utf8'))
325+
self.assertTrue(i_properties < i_testcase < i_system_out < i_system_err)
326+
308327
def test_xmlrunner_elapsed_times(self):
309328
self.runner_kwargs['elapsed_times'] = False
310329
suite = unittest.TestSuite()

xmlrunner/result.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def _report_testsuite_properties(xml_testsuite, xml_document, properties):
312312

313313
_report_testsuite_properties = staticmethod(_report_testsuite_properties)
314314

315-
def _report_testsuite(suite_name, tests, xml_document, parentElement,
315+
def _report_testsuite(suite_name, suite, tests, xml_document, parentElement,
316316
properties):
317317
"""
318318
Appends the testsuite section to the XML document.
@@ -335,6 +335,9 @@ def _report_testsuite(suite_name, tests, xml_document, parentElement,
335335
_XMLTestResult._report_testsuite_properties(
336336
testsuite, xml_document, properties)
337337

338+
for test in tests:
339+
_XMLTestResult._report_testcase(suite, test, testsuite, xml_document)
340+
338341
systemout = xml_document.createElement('system-out')
339342
testsuite.appendChild(systemout)
340343

@@ -447,10 +450,8 @@ def generate_reports(self, test_runner):
447450

448451
# Build the XML file
449452
testsuite = _XMLTestResult._report_testsuite(
450-
suite_name, tests, doc, parentElement, self.properties
453+
suite_name, suite, tests, doc, parentElement, self.properties
451454
)
452-
for test in tests:
453-
_XMLTestResult._report_testcase(suite, test, testsuite, doc)
454455
xml_content = doc.toprettyxml(
455456
indent='\t',
456457
encoding=test_runner.encoding

0 commit comments

Comments
 (0)