-
Notifications
You must be signed in to change notification settings - Fork 1
/
junit2json.py
executable file
·96 lines (80 loc) · 2.35 KB
/
junit2json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
# Author: Sagi Shnaidman (@sshnaidm), Red Hat.
import argparse
import json
from junitparser import JUnitXml, TestSuite
def get_stat(xml):
result = {
"total": 0,
"pass": 0,
"skip": 0,
"fail": 0,
"error": 0,
"total_run": 0,
"total_time": 0,
"tests": {}
}
for t in xml:
name = t.name
time_ts = t.time or 0
result['total'] += 1
result['tests'][name] = {}
result['tests'][name]['time'] = time_ts
result['total_time'] += time_ts
if t.is_passed:
result['pass'] += 1
result['tests'][name]['result'] = 'pass'
elif t.is_skipped:
result['skip'] += 1
result['tests'][name]['result'] = 'skip'
elif t.result and t.result[0].type == 'Failure':
result['fail'] += 1
result['tests'][name]['result'] = 'fail'
else:
result['error'] += 1
result['tests'][name]['result'] = 'error'
result['total_run'] = result['total'] - result['skip']
return result
def merge(xml_tests):
all_tests = dict()
flat = []
for suite in xml_tests:
if isinstance(suite, TestSuite):
flat += [i for i in suite]
else:
flat.append(suite)
for i in flat:
name = i.name
if name not in all_tests:
all_tests[name] = i
else:
# Overwrite skipped tests with results
if all_tests[name].is_skipped and not i.is_skipped:
all_tests[name] = i
return list(all_tests.values())
def main():
parser = argparse.ArgumentParser(
description="Extract tasks from a playbook."
)
parser.add_argument(
"--output",
"-o",
help="Output file. Default: cnf_result.json",
default="cnf_result.json",
)
parser.add_argument(
"files",
nargs="+",
help="Files to extract tests from.",
)
args = parser.parse_args()
all_xml = JUnitXml.fromfile(args.files[0])
for i in args.files[1:]:
all_xml += JUnitXml.fromfile(i)
if len(args.files) > 1 or isinstance(all_xml, JUnitXml):
all_xml = merge(all_xml)
data = get_stat(all_xml)
with open(args.output, "w") as f:
f.write(json.dumps(data))
if __name__ == '__main__':
main()