Skip to content

Instantly share code, notes, and snippets.

@kobenguyent
Forked from cgoldberg/merge_junit_results.py
Last active February 19, 2020 14:16
Show Gist options
  • Save kobenguyent/5c46ce01734cdfaa9c402eba550fbdd1 to your computer and use it in GitHub Desktop.
Save kobenguyent/5c46ce01734cdfaa9c402eba550fbdd1 to your computer and use it in GitHub Desktop.
Merge multiple JUnit XML results files into a single results file.
#!/usr/bin/env python
"""Merge multiple JUnit XML results files into a single results file."""
# MIT License
#
# Copyright (c) 2012 Corey Goldberg
#
# Updated by Thanh Nguyen - September 2019
import os
import sys
import xml.etree.ElementTree as ET
"""Merge multiple JUnit XML files into a single results file.
Output dumps to stdout.
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
time = 0.0
cases = []
for file_name in xml_files:
tree = ET.parse(file_name)
root = tree.getroot()
for test_suite in root:
failures += int(test_suite.attrib['failures'])
tests += int(test_suite.attrib['tests'])
errors += int(test_suite.attrib['errors'])
time += float(test_suite.attrib['time'])
cases.append(test_suite)
new_root = ET.Element('testsuites')
ET.SubElement(new_root, 'testsuite', failures='%s' % failures, tests='%s' % tests, errors='%s' % errors, time='%s' % time)
for case in cases:
for suite in new_root:
suite.extend(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()
@kobenguyent
Copy link
Author

Updated code to get rid of using soon deprecated getChildren().

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