forked from sorki/autotest2junit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautotest2junit
executable file
·92 lines (75 loc) · 2.89 KB
/
autotest2junit
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
#!/usr/bin/env python3
# Author: [email protected]
# Version: 1.0
import re
from lxml import etree
OK_LINES = r'(?P<num>\d+)\.\s(?P<name>[^\s]+)\s\((?P<file>[^:]+):(?P<line>\d+)\).*\((?P<time>[^\s]+)\s(?P<time2>[^\)]+)\)$'
ER_LINES = r'\s\s(?P<num>\d+):\s(?P<file>[^:]+):(?P<line>\d+)\s+(?P<name>[^\s]+)$'
def time_to_seconds(inp):
hours = 0
minutes = 0
seconds = 0
if 'h' in inp:
hours = int(re.search(r'(\d+)h', inp).group(1))
if 'm' in inp:
minutes = int(re.search(r'(\d+)m', inp).group(1))
if 's' in inp:
seconds = float(re.search(r'(\d*\.?\d*)s', inp).group(1))
return '%.3f' % (seconds + minutes * 60 + hours * 3600)
def parse(fname):
totaltime=0
list_ok=False
list_er=False
passed=[]
failed=[]
ok = re.compile(OK_LINES)
er = re.compile(ER_LINES)
with open(fname) as f:
complete = f.readlines()
for line in complete:
if 'testsuite: starting' in line:
list_ok=True
continue
if '## Summary of the failures. ##' in line:
list_er=True
continue
if '## Detailed failed' in line:
list_er=False
continue
if 'test suite duration' in line:
totaltime=line.split(':')[2].strip()
if list_ok:
match = ok.search(line)
if not match:
list_ok=False
continue
passed.append(match.groupdict())
if list_er:
match = er.search(line)
if not match:
continue
failed.append(match.groupdict())
tests=len(passed) + len(failed)
fails=len(failed)
testsuite = etree.Element('testsuite', name='testsuite', errors='0',
failures=str(fails), tests=str(tests), time=time_to_seconds(totaltime))
for i in failed:
f = etree.SubElement(testsuite, 'testcase',
name=i['name'],
classname='%s.%s' % (i['file'].replace('.at', ''), i['name']))
etree.SubElement(f, 'failure', type='fail', message='Test failed')
for i in passed:
etree.SubElement(testsuite, 'testcase',
name=i['name'],
classname='%s.%s' % (i['file'].replace('.at', ''), i['name']),
time=time_to_seconds(i['time']))
out = etree.SubElement(testsuite, 'system-out')
out.text = etree.CDATA(' '.join(complete))
err = etree.SubElement(testsuite, 'system-err')
with open('testsuite.xml', 'w') as outfile:
# No, “UTF-8” is not the same as “unicode” in this case.
# https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring
outfile.write(etree.tostring(testsuite, pretty_print=True,
encoding='unicode'))
if __name__ == "__main__":
parse('testsuite.log')