Skip to content

Instantly share code, notes, and snippets.

@rberrelleza
Created September 14, 2016 22:53
Show Gist options
  • Save rberrelleza/724d7eaaaee63da31a9264ca589cfb01 to your computer and use it in GitHub Desktop.
Save rberrelleza/724d7eaaaee63da31a9264ca589cfb01 to your computer and use it in GitHub Desktop.
Sort junit test files by the provided attribute
import argparse
import sys
import xml.etree.ElementTree as ET
def getChildValue(val):
try:
return float(val)
except:
return val
def sortchildrenby(parent, attr):
parent[:] = sorted(parent, key=lambda child: getChildValue(child.get(attr)))
parser = argparse.ArgumentParser(description='Sort junit files')
parser.add_argument('--field', dest='field', default='time',
help='sort tests by (default: time)')
args = parser.parse_args()
lines = sys.stdin.readlines()
tree = ET.fromstring("".join(lines))
sortchildrenby(tree, args.field)
for child in tree:
sortchildrenby(child, args.field)
print(ET.tostring(tree, encoding='utf8', method='xml'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment