| 1 |
#!/usr/bin/env python |
| 2 |
""" |
| 3 |
Filter JUnit XML reports to show only a subset of all information. |
| 4 |
|
| 5 |
JUNIT XML SCHEMA: |
| 6 |
|
| 7 |
element:testsuite: failures="..." errors="..." |
| 8 |
+-- element:testcase: name="..." status="..." |
| 9 |
""" |
| 10 |
|
| 11 |
from __future__ import absolute_import, print_function, with_statement |
| 12 |
import os.path |
| 13 |
import sys |
| 14 |
import argparse |
| 15 |
from fnmatch import fnmatch |
| 16 |
from xml.etree import ElementTree as ET |
| 17 |
from xml.dom import minidom |
| 18 |
from behave.textutil import indent |
| 19 |
|
| 20 |
__author = "Jens Engel" |
| 21 |
__status__ = "prototype" |
| 22 |
|
| 23 |
NAME = os.path.basename(__file__) |
| 24 |
VERSION = "0.1.0" |
| 25 |
REPORT_DIR = "reports" |
| 26 |
|
| 27 |
def xml_prettify(elem): |
| 28 |
"""Return a pretty-printed XML string for the XML element.""" |
| 29 |
text = ET.tostring(elem, "utf-8") |
| 30 |
reparsed = minidom.parseString(text) |
| 31 |
return reparsed.toprettyxml(indent=" ") |
| 32 |
|
| 33 |
def xml_select_testcases_with_status(tree, status): |
| 34 |
return tree.findall(".//testcase[@status='%s']" % status) |
| 35 |
|
| 36 |
def path_select_files(paths, pattern="*.xml"): |
| 37 |
if not paths: |
| 38 |
paths = [REPORT_DIR] |
| 39 |
|
| 40 |
selected = [] |
| 41 |
for pathname in paths: |
| 42 |
if os.path.isdir(pathname): |
| 43 |
for root, dirs, files in os.walk(pathname): |
| 44 |
for filename in files: |
| 45 |
if fnmatch(filename, pattern): |
| 46 |
filename2 = os.path.join(root, filename) |
| 47 |
selected.append(os.path.normpath(filename2)) |
| 48 |
elif os.path.isfile(pathname) and fnmatch(pathname, pattern): |
| 49 |
selected.append(pathname) |
| 50 |
return selected |
| 51 |
|
| 52 |
def report_testcases(filename, testcases): |
| 53 |
print(u"REPORT: {0}".format(filename)) |
| 54 |
for xml_testcase in testcases: |
| 55 |
print(" TESTCASE: {0}".format(xml_testcase.get("name"))) |
| 56 |
xml_text = indent(xml_prettify(xml_testcase), " ") |
| 57 |
print(xml_text) |
| 58 |
|
| 59 |
def main(args=None): |
| 60 |
"""Filter JUnit XML reports to show only a subset of all information.""" |
| 61 |
if args is None: |
| 62 |
args = sys.argv[1:] |
| 63 |
|
| 64 |
parser = argparse.ArgumentParser(prog=NAME, |
| 65 |
description=main.__doc__) |
| 66 |
parser.add_argument("-s", "--status", default="failed", required=False, |
| 67 |
choices=["passed", "failed", "skipped"], |
| 68 |
help="Status to select (passed, failed, skipped).") |
| 69 |
parser.add_argument("xml_file", nargs="*", |
| 70 |
help="XML file(s) or directory with XML files.") |
| 71 |
parser.add_argument("--version", action="version", version=VERSION) |
| 72 |
|
| 73 |
options = parser.parse_args(args) |
| 74 |
|
| 75 |
xml_files = options.xml_file |
| 76 |
xml_reports = path_select_files(xml_files) |
| 77 |
for xml_filename in xml_reports: |
| 78 |
tree = ET.parse(xml_filename) |
| 79 |
testcases = xml_select_testcases_with_status(tree, options.status) |
| 80 |
if testcases: |
| 81 |
report_testcases(xml_filename, testcases) |
| 82 |
return 0 |
| 83 |
|
| 84 |
if __name__ == "__main__": |
| 85 |
sys.exit(main()) |