Best Python code snippet using autotest_python
junit.py
Source:junit.py  
...89            args = ', '.join(('%s=%s' % a for a in task.args.items()))90            if args:91                name += ' ' + args92        self._task_data[uuid] = TaskData(uuid, name, path, play)93    def _finish_task(self, status, result):94        """ record the results of a task for a single host """95        task_uuid = result._task._uuid96        if hasattr(result, '_host'):97            host_uuid = result._host._uuid98            host_name = result._host.name99        else:100            host_uuid = 'include'101            host_name = 'include'102        task_data = self._task_data[task_uuid]103        if status == 'failed' and 'EXPECTED FAILURE' in task_data.name:104            status = 'ok'105        task_data.add_host(HostData(host_uuid, host_name, status, result))106    def _build_test_case(self, task_data, host_data):107        """ build a TestCase from the given TaskData and HostData """108        name = '[%s] %s: %s' % (host_data.name, task_data.play, task_data.name)109        duration = host_data.finish - task_data.start110        if self._task_class == 'true':111            junit_classname = re.sub('\.yml:[0-9]+$', '', task_data.path)112        else:113            junit_classname = task_data.path114        if host_data.status == 'included':115            return TestCase(name, junit_classname, duration, host_data.result)116        res = host_data.result._result117        rc = res.get('rc', 0)118        dump = self._dump_results(res, indent=0)119        dump = self._cleanse_string(dump)120        if host_data.status == 'ok':121            return TestCase(name, junit_classname, duration, dump)122        test_case = TestCase(name, junit_classname, duration)123        if host_data.status == 'failed':124            if 'exception' in res:125                message = res['exception'].strip().split('\n')[-1]126                output = res['exception']127                test_case.add_error_info(message, output)128            elif 'msg' in res:129                message = res['msg']130                test_case.add_failure_info(message, dump)131            else:132                test_case.add_failure_info('rc=%s' % rc, dump)133        elif host_data.status == 'skipped':134            if 'skip_reason' in res:135                message = res['skip_reason']136            else:137                message = 'skipped'138            test_case.add_skipped_info(message)139        return test_case140    def _cleanse_string(self, value):141        """ convert surrogate escapes to the unicode replacement character to avoid XML encoding errors """142        return to_text(to_bytes(value, errors='surrogateescape'), errors='replace')143    def _generate_report(self):144        """ generate a TestSuite report from the collected TaskData and HostData """145        test_cases = []146        for task_uuid, task_data in self._task_data.items():147            for host_uuid, host_data in task_data.host_data.items():148                test_cases.append(self._build_test_case(task_data, host_data))149        test_suite = TestSuite(self._playbook_name, test_cases)150        report = TestSuite.to_xml_string([test_suite])151        output_file = os.path.join(self._output_dir, '%s-%s.xml' % (self._playbook_name, time.time()))152        with open(output_file, 'wb') as xml:153            xml.write(to_bytes(report, errors='surrogate_or_strict'))154    def v2_playbook_on_start(self, playbook):155        self._playbook_path = playbook._file_name156        self._playbook_name = os.path.splitext(os.path.basename(self._playbook_path))[0]157    def v2_playbook_on_play_start(self, play):158        self._play_name = play.get_name()159    def v2_runner_on_no_hosts(self, task):160        self._start_task(task)161    def v2_playbook_on_task_start(self, task, is_conditional):162        self._start_task(task)163    def v2_playbook_on_cleanup_task_start(self, task):164        self._start_task(task)165    def v2_playbook_on_handler_task_start(self, task):166        self._start_task(task)167    def v2_runner_on_failed(self, result, ignore_errors=False):168        if ignore_errors:169            self._finish_task('ok', result)170        else:171            self._finish_task('failed', result)172    def v2_runner_on_ok(self, result):173        self._finish_task('ok', result)174    def v2_runner_on_skipped(self, result):175        self._finish_task('skipped', result)176    def v2_playbook_on_include(self, included_file):177        self._finish_task('included', included_file)178    def v2_playbook_on_stats(self, stats):179        self._generate_report()180class TaskData:181    """182    Data about an individual task.183    """184    def __init__(self, uuid, name, path, play):185        self.uuid = uuid186        self.name = name187        self.path = path188        self.play = play189        self.start = None190        self.host_data = OrderedDict()191        self.start = time.time()...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!!
