#!/usr/bin/env python | |
# | |
# Corey Goldberg, Dec 2012 | |
# Dmytro Makhno, Mar 2015 | |
# | |
import os | |
import sys | |
import xml.etree.ElementTree as ET | |
"""Merge/Solder multiple JUnit XML files into a single results file. | |
If testcase exists in last result, do not add it again. Used in rerun. | |
Output dumps to sdtdout. | |
example usage: | |
$ python merge_junit_results.py results1.xml results2.xml > results.xml | |
""" | |
def main(): | |
args = sys.argv[1:] | |
if not args: | |
usage() | |
sys.exit(2) | |
if '-h' in args or '--help' in args: | |
usage() | |
sys.exit(2) | |
merge_results(args[:]) | |
def merge_results(xml_files): | |
failures = 0 | |
tests = 0 | |
errors = 0 | |
skips = 0 | |
cases = [] | |
def need_add(classname, name): | |
return len([c for c in cases if c.attrib['classname'] == classname and c.attrib['name'] == name]) == 0 | |
xml_files.reverse() | |
for file_name in xml_files: | |
if not os.path.isfile(file_name): | |
continue | |
tree = ET.parse(file_name) | |
test_suite = tree.getroot() | |
failures = int(test_suite.attrib['failures']) | |
tests = int(test_suite.attrib['tests']) | |
errors = int(test_suite.attrib['errors']) | |
skips = int(test_suite.attrib['skip']) | |
for child in test_suite.getchildren(): | |
if need_add(child.attrib['classname'], child.attrib['name']): | |
cases.append(child) | |
new_root = ET.Element('testsuite') | |
new_root.attrib['failures'] = '%s' % failures | |
new_root.attrib['tests'] = '%s' % tests | |
new_root.attrib['errors'] = '%s' % errors | |
new_root.attrib['skip'] = '%s' % skips | |
for case in cases: | |
new_root.append(case) | |
new_tree = ET.ElementTree(new_root) | |
ET.dump(new_tree) | |
def usage(): | |
this_file = os.path.basename(__file__) | |
print 'Usage: %s results1.xml results2.xml' % this_file | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment