Skip to content

Instantly share code, notes, and snippets.

@dunossauro
Last active June 17, 2019 10:57
Show Gist options
  • Save dunossauro/566ac8e3cc28ba1e9e6446c9bff36440 to your computer and use it in GitHub Desktop.
Save dunossauro/566ac8e3cc28ba1e9e6446c9bff36440 to your computer and use it in GitHub Desktop.
Merge behave junit rerun file with original report
"""
Behave junit merge.
Merge behave-junit rerun and original file
usage: junit-merge [-h] [--report REPORT] [--rerun_report RERUN_REPORT]
optional arguments:
-h, --help show this help message and exit
--report REPORT junit-file
--rerun_report RERUN_REPORT rerun junit-file
--verbose VERBOSE verbose mode
"""
from logging import basicConfig, DEBUG, info
from argparse import ArgumentParser
import xml.etree.ElementTree as ET
from os import path, remove
log = basicConfig(filename='junit-merge.log', level=DEBUG)
parser = ArgumentParser('junit-merge')
parser.add_argument('--report', type=str, help='junit-file')
parser.add_argument('--rerun_report', type=str, help='rerun junit-file')
parser.add_argument('--verbose', type=bool, help='verbose mode')
args = parser.parse_args()
if args.verbose:
info(f'report file {args.report}')
info(f'report file {args.rerun_report}')
source_junit = ET.parse(args.report)
source_rerun = ET.parse(args.rerun_report)
origin_root = source_junit.getiterator()[0]
rerun_root = source_rerun.getiterator()[0]
junit_testsuite_atrtibs = origin_root.attrib
rerun_testsuite_atrtibs = rerun_root.attrib
testsuite = {
'errors': rerun_testsuite_atrtibs['errors'],
'failures': rerun_testsuite_atrtibs['failures'],
'name': junit_testsuite_atrtibs['name'],
'skipped': junit_testsuite_atrtibs['skipped'],
'tests': junit_testsuite_atrtibs['skipped'],
}
root = ET.Element(origin_root.tag, **testsuite)
def put_childs(junit, rerun, root):
"""Get testcase sub elements when rerun fail.
NOTE: when rerun failed too, put_childs put the original junit traceback,
not rerun traceback
"""
childs = junit.getchildren()
ET.SubElement(rerun, childs[0].tag, **childs[0].attrib)
if len(childs) == 2:
ET.SubElement(rerun, childs[1].tag, **childs[1].attrib)
root.append(rerun)
for junit_line, rerun_line in zip(
source_junit.findall('./testcase'), source_rerun.findall('./testcase')
):
junit_status = junit_line.attrib['status']
rerun_status = rerun_line.attrib['status']
if args.verbose:
info(f'Junit report line: {junit_line} status: {junit_status}')
info(f'Rerun report line: {rerun_line} status: {rerun_status}')
if junit_status == 'failed' and rerun_status == 'passed':
ET.SubElement(root, rerun_line.tag, **junit_line.attrib)
elif junit_status == 'failed' and rerun_status == 'failed':
put_childs(junit_line, rerun_line, root)
else:
ET.SubElement(root, junit_line.tag, **junit_line.attrib)
tree = ET.ElementTree(root)
tree.write(args.report)
remove(path.abspath(args.rerun_report))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment