Best Python code snippet using unittest-xml-reporting_python
xmlrunner.py
Source:xmlrunner.py  
...186        errors = len([1 for e in tests if e.outcome == _TestInfo.ERROR])187        testsuite.setAttribute('errors', str(errors))188        return testsuite189    _report_testsuite = staticmethod(_report_testsuite)190    def _report_testcase(suite_name, test_result, xml_testsuite, xml_document):191        "Appends a testcase section to the XML document."192        testcase = xml_document.createElement('testcase')193        xml_testsuite.appendChild(testcase)194        testcase.setAttribute('classname', str(suite_name))195        testcase.setAttribute('name', test_result.test_method.shortDescription()196                              or getattr(test_result.test_method, '_testMethodName',197                                         str(test_result.test_method)))198        testcase.setAttribute('time', '%.3f' % test_result.get_elapsed_time())199        if (test_result.outcome != _TestInfo.SUCCESS):200            elem_name = ('failure', 'error')[test_result.outcome-1]201            failure = xml_document.createElement(elem_name)202            testcase.appendChild(failure)203            failure.setAttribute('type', str(test_result.err[0].__name__))204            failure.setAttribute('message', str(test_result.err[1]))205            error_info = test_result.get_error_info()206            failureText = xml_document.createCDATAOrText(error_info)207            failure.appendChild(failureText)208    _report_testcase = staticmethod(_report_testcase)209    def _report_output(test_runner, xml_testsuite, xml_document, stdout, stderr):210        "Appends the system-out and system-err sections to the XML document."211        systemout = xml_document.createElement('system-out')212        xml_testsuite.appendChild(systemout)213        systemout_text = xml_document.createCDATAOrText(stdout)214        systemout.appendChild(systemout_text)215        systemerr = xml_document.createElement('system-err')216        xml_testsuite.appendChild(systemerr)217        systemerr_text = xml_document.createCDATAOrText(stderr)218        systemerr.appendChild(systemerr_text)219    _report_output = staticmethod(_report_output)220    def generate_reports(self, test_runner):221        "Generates the XML reports to a given XMLTestRunner object."222        all_results = self._get_info_by_testcase()223        if type(test_runner.output) == str and not \224            os.path.exists(test_runner.output):225            os.makedirs(test_runner.output)226        for suite, tests in all_results.items():227            doc = XMLDocument()228            # Build the XML file229            testsuite = _XMLTestResult._report_testsuite(suite, tests, doc)230            stdout, stderr = [], []231            for test in tests:232                _XMLTestResult._report_testcase(suite, test, testsuite, doc)233                if test.stdout:234                    stdout.extend(['*****************', test.get_description(), test.stdout])235                if test.stderr:236                    stderr.extend(['*****************', test.get_description(), test.stderr])237            _XMLTestResult._report_output(test_runner, testsuite, doc,238                                          '\n'.join(stdout), '\n'.join(stderr))239            xml_content = doc.toprettyxml(indent='\t')240            if type(test_runner.output) is str:241                report_file = open('%s%sTEST-%s.xml' % \242                    (test_runner.output, os.sep, suite), 'w')243                try:244                    report_file.write(xml_content)245                finally:246                    report_file.close()...__init__.py
Source:__init__.py  
...167        return test_id.split('.')[-1]168    169    _test_method_name = staticmethod(_test_method_name)170    171    def _report_testcase(suite_name, test_result, xml_testsuite, xml_document):172        "Appends a testcase section to the XML document."173        testcase = xml_document.createElement('testcase')174        xml_testsuite.appendChild(testcase)175        176        testcase.setAttribute('classname', suite_name)177        testcase.setAttribute('name', _XMLTestResult._test_method_name(test_result.test_method))178        testcase.setAttribute('time', '%.3f' % test_result.get_elapsed_time())179        180        if (test_result.outcome != _TestInfo.SUCCESS):181            elem_name = ('failure', 'error')[test_result.outcome-1]182            failure = xml_document.createElement(elem_name)183            testcase.appendChild(failure)184            185            failure.setAttribute('type', test_result.err[0].__name__)186            failure.setAttribute('message', str(test_result.err[1]))187            188            error_info = test_result.get_error_info()189            failureText = xml_document.createCDATASection(error_info)190            failure.appendChild(failureText)191    192    _report_testcase = staticmethod(_report_testcase)193    194    def _report_output(test_runner, xml_testsuite, xml_document):195        "Appends the system-out and system-err sections to the XML document."196        systemout = xml_document.createElement('system-out')197        xml_testsuite.appendChild(systemout)198        199        stdout = test_runner.stdout.getvalue()200        systemout_text = xml_document.createCDATASection(stdout)201        systemout.appendChild(systemout_text)202        203        systemerr = xml_document.createElement('system-err')204        xml_testsuite.appendChild(systemerr)205        206        stderr = test_runner.stderr.getvalue()207        systemerr_text = xml_document.createCDATASection(stderr)208        systemerr.appendChild(systemerr_text)209    210    _report_output = staticmethod(_report_output)211    212    def generate_reports(self, test_runner):213        "Generates the XML reports to a given XMLTestRunner object."214        from xml.dom.minidom import Document215        all_results = self._get_info_by_testcase()216        217        if type(test_runner.output) == str and not \218            os.path.exists(test_runner.output):219            os.makedirs(test_runner.output)220        221        for suite, tests in all_results.items():222            doc = Document()223            224            # Build the XML file225            testsuite = _XMLTestResult._report_testsuite(suite, tests, doc)226            for test in tests:227                _XMLTestResult._report_testcase(suite, test, testsuite, doc)228            _XMLTestResult._report_output(test_runner, testsuite, doc)229            xml_content = doc.toprettyxml(indent='\t')230            231            if type(test_runner.output) is str:232                report_file = file('%s%sTEST-%s.xml' % \233                    (test_runner.output, os.sep, suite), 'w')234                try:235                    report_file.write(xml_content)236                finally:237                    report_file.close()238            else:239                # Assume that test_runner.output is a stream240                test_runner.output.write(xml_content)241class XMLTestRunner(TextTestRunner):...test_xml_result.py
Source:test_xml_result.py  
...82        )83    # save the original method that will be overwritten84    report_testcase = copy.deepcopy(xmlrunner.runner._XMLTestResult._report_testcase)85    @staticmethod86    def _report_testcase(87        test_result: TestInfo, xml_testsuite: Element, xml_document: Document88    ):89        """Callback function that create the test case section into the XML document.90        :param test_result: result of a test run91        :param xml_testsuite: xml test suite to be written92        :param xml_document: xml document base93        """94        # call the original method95        XmlTestResult.report_testcase(test_result, xml_testsuite, xml_document)96        # here can be added additional tags that have to be stored into the xml test report97        xml_testsuite.setAttribute("test_ids", str(test_result.test_ids))98    # Catch and redefine xmlrunner.runner._XMLTestResult._report_testcase...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
