Skip to content

Instantly share code, notes, and snippets.

@sigilioso
Created July 28, 2014 12:29
Show Gist options
  • Save sigilioso/232bec3673ad0aac8052 to your computer and use it in GitHub Desktop.
Save sigilioso/232bec3673ad0aac8052 to your computer and use it in GitHub Desktop.
Parses xunit xml output and shows it in a 'human-readable' way.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import glob
from lxml import etree
def tests_elements(nodes):
for elm in nodes:
subelm = elm.getchildren()[0] if elm.getchildren() else None
yield {'id': '{}:{}'.format(elm.get('classname', ''), elm.get('name')),
'status': elm.get('status', subelm.tag if subelm is not None else 'passed').upper(),
'time': elm.get('time', '---'),
'message': subelm.get('message', '---') if subelm is not None else '---',
'data': subelm.text if subelm is not None else '---'}
def get_data(xml, suite):
data = etree.parse(xml)
root = data.getroot()
suite['total'] = suite.get('total', 0) + int(root.get('tests'))
for word in ('errors', 'failures', 'skip'):
kw = 'total_{}'.format(word)
suite[kw] = suite.get(kw, 0) + int(root.get(word, 0))
suite['tests'] = suite.get('tests', []) + [test for test in tests_elements(root.getchildren())]
def show(suite):
print('='*80)
print('RUN {} tests'.format(suite['total']))
print('='*80)
print(' * Errors: {}'.format(suite['total_errors']))
print(' * Failures: {}'.format(suite['total_failures']))
print(' * Skip: {}'.format(suite['total_skip']))
print('-'*80)
for test in suite['tests']:
print('{} --- {}'.format(test['status'], test['id']))
print('Time: {}'.format(test['time']))
print('Message: {}'.format(test['message']))
print('Data: {}'.format(test['data']))
print('-'*80)
def main():
suite = {}
path = sys.argv[1]
if os.path.isdir(path):
for xml in glob.glob(os.path.join(path, '*.xml')):
get_data(xml, suite)
elif os.path.isfile(path):
get_data(path, suite)
show(suite)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment