# HG changeset patch # User Siddharth Agarwal # Date 2017-06-06 20:10:55 # Node ID 3afe258fb0fe5d613a79ccdf8cb66553188b94e0 # Parent f75d0aa5dc83d7eab59b00e30a9b40aa8cfa7306 run-tests: factor out xunit write code into another method We're going to use this code to output an XUnit-formatted listing of tests. diff --git a/tests/run-tests.py b/tests/run-tests.py --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -1932,33 +1932,8 @@ class TextTestRunner(unittest.TextTestRu self.stream.writeln('Errored %s: %s' % (test.name, msg)) if self._runner.options.xunit: - with open(self._runner.options.xunit, 'wb') as xuf: - timesd = dict((t[0], t[3]) for t in result.times) - doc = minidom.Document() - s = doc.createElement('testsuite') - s.setAttribute('name', 'run-tests') - s.setAttribute('tests', str(result.testsRun)) - s.setAttribute('errors', "0") # TODO - s.setAttribute('failures', str(failed)) - s.setAttribute('skipped', str(skipped + ignored)) - doc.appendChild(s) - for tc in result.successes: - t = doc.createElement('testcase') - t.setAttribute('name', tc.name) - t.setAttribute('time', '%.3f' % timesd[tc.name]) - s.appendChild(t) - for tc, err in sorted(result.faildata.items()): - t = doc.createElement('testcase') - t.setAttribute('name', tc) - t.setAttribute('time', '%.3f' % timesd[tc]) - # createCDATASection expects a unicode or it will - # convert using default conversion rules, which will - # fail if string isn't ASCII. - err = cdatasafe(err).decode('utf-8', 'replace') - cd = doc.createCDATASection(err) - t.appendChild(cd) - s.appendChild(t) - xuf.write(doc.toprettyxml(indent=' ', encoding='utf-8')) + with open(self._runner.options.xunit, "wb") as xuf: + self._writexunit(result, xuf) if self._runner.options.json: jsonpath = os.path.join(self._runner._testdir, b'report.json') @@ -2055,6 +2030,36 @@ class TextTestRunner(unittest.TextTestRu cuser, csys, real, start, end = tdata[1:6] self.stream.writeln(cols % (start, end, cuser, csys, real, test)) + @staticmethod + def _writexunit(result, outf): + timesd = dict((t[0], t[3]) for t in result.times) + doc = minidom.Document() + s = doc.createElement('testsuite') + s.setAttribute('name', 'run-tests') + s.setAttribute('tests', str(result.testsRun)) + s.setAttribute('errors', "0") # TODO + s.setAttribute('failures', str(len(result.failures))) + s.setAttribute('skipped', str(len(result.skipped) + + len(result.ignored))) + doc.appendChild(s) + for tc in result.successes: + t = doc.createElement('testcase') + t.setAttribute('name', tc.name) + t.setAttribute('time', '%.3f' % timesd[tc.name]) + s.appendChild(t) + for tc, err in sorted(result.faildata.items()): + t = doc.createElement('testcase') + t.setAttribute('name', tc) + t.setAttribute('time', '%.3f' % timesd[tc]) + # createCDATASection expects a unicode or it will + # convert using default conversion rules, which will + # fail if string isn't ASCII. + err = cdatasafe(err).decode('utf-8', 'replace') + cd = doc.createCDATASection(err) + t.appendChild(cd) + s.appendChild(t) + outf.write(doc.toprettyxml(indent=' ', encoding='utf-8')) + class TestRunner(object): """Holds context for executing tests.