Instantly share code, notes, and snippets.

What would you like to do?
Merge multiple JUnit XML results files into a single results file.
#!/usr/bin/env python
# Corey Goldberg, Dec 2012
import os
import sys
import xml.etree.ElementTree as ET
"""Merge multiple JUnit XML files into a single results file.
Output dumps to sdtdout.
example usage:
$ python results1.xml results2.xml > results.xml
def main():
args = sys.argv[1:]
if not args:
if '-h' in args or '--help' in args:
def merge_results(xml_files):
failures = 0
tests = 0
errors = 0
time = 0.0
cases = []
for file_name in xml_files:
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'])
time += float(test_suite.attrib['time'])
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['time'] = '%s' % time
for case in cases:
new_tree = ET.ElementTree(new_root)
def usage():
this_file = os.path.basename(__file__)
print 'Usage: %s results1.xml results2.xml' % this_file
if __name__ == '__main__':

This comment has been minimized.

jswetzen commented May 16, 2016

This is very useful! Can I use this commercially? If so, with what kind of license?


This comment has been minimized.


This comment has been minimized.

yodaqua commented Dec 19, 2017

Hi there

This code drops the name attribute of a test suite when merging.

I've added the name attribute to the code, however the merged reports, has as it's name, the last test suite.

This means that all the test suites test cases are reported as part of only one test suite (the last test suite, or, in this case the last Junit XML report).

I'm working on a fix. As soon as I have it I'll send merge request.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment