diff --git a/tests/testsuite.py b/tests/testsuite.py index 61ee686..55a0c55 100755 --- a/tests/testsuite.py +++ b/tests/testsuite.py @@ -26,10 +26,10 @@ def test_doctest_example(self): runner.run(suite) outdir.seek(0) output = outdir.read() - # note: the testcases generated by DocTestSuite are not attached to where the docstring is declared. - # In other words, it does not preserve classname/package information. - self.assertIn('name="twice"'.encode('utf8'), output) - self.assertIn('name="threetimes"'.encode('utf8'), output) + self.assertIn('classname="tests.doctest_example.Multiplicator" name="threetimes"' + .encode('utf8'), output) + self.assertIn('classname="tests.doctest_example" name="twice"' + .encode('utf8'), output) class XMLTestRunnerTestCase(unittest.TestCase): @@ -123,6 +123,21 @@ def test_basic_unittest_constructs(self): suite.addTest(self.DummyTest('test_error')) self._test_xmlrunner(suite) + def test_classnames(self): + suite = unittest.TestSuite() + suite.addTest(self.DummyTest('test_pass')) + suite.addTest(self.DummySubTest('test_subTest_pass')) + outdir = BytesIO() + stream = StringIO() + runner = xmlrunner.XMLTestRunner(stream=stream, output=outdir, verbosity=0) + runner.run(suite) + outdir.seek(0) + output = outdir.read() + self.assertIn('classname="tests.testsuite.DummyTest" name="test_pass"' + .encode('utf8'), output) + self.assertIn('classname="tests.testsuite.DummySubTest" name="test_subTest_pass"' + .encode('utf8'), output) + def test_xmlrunner_non_ascii(self): suite = unittest.TestSuite() suite.addTest(self.DummyTest('test_non_ascii_skip')) diff --git a/xmlrunner/result.py b/xmlrunner/result.py index 5b97fde..9b60988 100644 --- a/xmlrunner/result.py +++ b/xmlrunner/result.py @@ -315,7 +315,7 @@ def _report_testsuite_properties(xml_testsuite, xml_document, properties): _report_testsuite_properties = staticmethod(_report_testsuite_properties) - def _report_testsuite(suite_name, suite, tests, xml_document, parentElement, + def _report_testsuite(suite_name, tests, xml_document, parentElement, properties): """ Appends the testsuite section to the XML document. @@ -339,7 +339,7 @@ def _report_testsuite(suite_name, suite, tests, xml_document, parentElement, testsuite, xml_document, properties) for test in tests: - _XMLTestResult._report_testcase(suite, test, testsuite, xml_document) + _XMLTestResult._report_testcase(test, testsuite, xml_document) systemout = xml_document.createElement('system-out') testsuite.appendChild(systemout) @@ -389,14 +389,16 @@ def _createCDATAsections(xmldoc, node, text): _createCDATAsections = staticmethod(_createCDATAsections) - def _report_testcase(suite_name, test_result, xml_testsuite, xml_document): + def _report_testcase(test_result, xml_testsuite, xml_document): """ Appends a testcase section to the XML document. """ testcase = xml_document.createElement('testcase') xml_testsuite.appendChild(testcase) - testcase.setAttribute('classname', suite_name) + class_name = re.sub(r'^__main__.', '', test_result.id()) + class_name = class_name.rpartition('.')[0] + testcase.setAttribute('classname', class_name) testcase.setAttribute( 'name', _XMLTestResult._test_method_name(test_result.test_id) ) @@ -455,7 +457,7 @@ def generate_reports(self, test_runner): # Build the XML file testsuite = _XMLTestResult._report_testsuite( - suite_name, suite, tests, doc, parentElement, self.properties + suite_name, tests, doc, parentElement, self.properties ) xml_content = doc.toprettyxml( indent='\t',