Best Python code snippet using unittest-xml-reporting_python
xmlrunner.py
Source:xmlrunner.py
...172 if testcase_name not in tests_by_testcase:173 tests_by_testcase[testcase_name] = []174 tests_by_testcase[testcase_name].append(test_info)175 return tests_by_testcase176 def _report_testsuite(suite_name, tests, xml_document):177 "Appends the testsuite section to the XML document."178 testsuite = xml_document.createElement('testsuite')179 xml_document.appendChild(testsuite)180 testsuite.setAttribute('name', str(suite_name))181 testsuite.setAttribute('tests', str(len(tests)))182 testsuite.setAttribute('time', '%.3f' %183 sum([e.get_elapsed_time() for e in tests]))184 failures = len([1 for e in tests if e.outcome == _TestInfo.FAILURE])185 testsuite.setAttribute('failures', str(failures))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:...
__init__.py
Source:__init__.py
...139 tests_by_testcase[testcase_name].append(test_info)140 141 return tests_by_testcase142 143 def _report_testsuite(suite_name, tests, xml_document):144 "Appends the testsuite section to the XML document."145 testsuite = xml_document.createElement('testsuite')146 xml_document.appendChild(testsuite)147 148 testsuite.setAttribute('name', suite_name)149 testsuite.setAttribute('tests', str(len(tests)))150 151 testsuite.setAttribute('time', '%.3f' % \152 sum(map(lambda e: e.get_elapsed_time(), tests)))153 154 failures = filter(lambda e: e.outcome==_TestInfo.FAILURE, tests)155 testsuite.setAttribute('failures', str(len(failures)))156 157 errors = filter(lambda e: e.outcome==_TestInfo.ERROR, tests)158 testsuite.setAttribute('errors', str(len(errors)))159 160 return testsuite161 162 _report_testsuite = staticmethod(_report_testsuite)163 164 def _test_method_name(test_method):165 "Returns the test method name."166 test_id = test_method.id()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 stream...
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!!