def create_junit_results(data, output=None, **kwargs): """ Creates a Junit result, can write to a file if desired, or return xml string. (used by Jenkins) input either dict(dict(dict())) or dict(list(dict())) dict = {suite: {test: {stderr,stdout,time,class,err,fail,skip}}} list = {suite: [(test, {stderr,stdout,time,class,err,fail,skip})]} :param data: A dictionary with dict or list hierarchy :param output: A filename to write results to /path/to/file/*.junit.xml :return: Returns an XML string if no output, else nothing. """ log.debug('creating junit results: output={}'.format(output)) stdout_format = kwargs.pop('stdout_format', None) test_class = kwargs.pop('test_class', None) package = kwargs.pop('package', None) from junit_xml import TestSuite, TestCase test_suites = [] for suite, tests in data.items(): test_cases = [] for test, result in (tests if isinstance(tests, list) else tests.items()): tc = TestCase(test) stdout = result.get('stdout') if stdout_format is not None and callable(stdout_format): if hasattr(stdout_format, 'func_code') and 'kwargs' in stdout_format.func_code.co_varnames: stdout = stdout_format(stdout, suite_name=suite, test_name=test, **kwargs) else: stdout = stdout_format(stdout) tc.stdout = stdout tc.stderr = result.get('stderr') tc.elapsed_sec = result.get('time') tc.classname = result.get('class', test_class) err = result.get('err') if err: tc.add_error_info(*err if isinstance(err, (list, tuple)) else [err]) fail = result.get('fail') if fail: tc.add_failure_info(*fail if isinstance(fail, (list, tuple)) else [fail]) skip = result.get('skip') if skip: tc.add_skipped_info(*skip if isinstance(skip, (list, tuple)) else [skip]) test_cases.append(tc) ts = TestSuite(suite, test_cases, package=package) test_suites.append(ts) if output: check_makedir(os.path.dirname(output)) with open(output, 'w') as out: TestSuite.to_file(out, test_suites) return output else: return TestSuite.to_xml_string(test_suites)
def _create_skipped_test_case(name, index): skipped_test_case = TestCase(name + "; Skip index" + str(index)) skipped_test_case.classname = "Skipped" skipped_test_case.skipped_output = "Skipped test case" skipped_test_case.elapsed_sec = 0 return skipped_test_case