Newer
Older
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
from bs4 import BeautifulSoup
from sys import argv
from testEntry import TestEntry
from writeExcel import ExcelWriter
from sys import exit
import re
usage_str = """
=====================================
Robot Test Reporter written in Python
=====================================
Usage
python parseTestResults.py output.xml
where output.xml is the xml file generated by robot
The command outputs to a new xlslx file if it does not exist, or
appends to an existing one.
"""
class TestOutputParser:
def __init__(self, fname):
self.testEntries = []
self.load_file(fname)
def load_file(self, fname):
self.contents = ""
with open(fname, "r", encoding="utf8") as f:
self.contents = f.read()
if self.contents == "":
print("Empty file {}".format(fname))
exit(-1)
def run_parser(self):
soup = BeautifulSoup(self.contents, "lxml")
tests = soup.find_all("test")
for test in tests:
self.testEntries.append(createTestEntry(test))
# Write tests
ew = ExcelWriter()
for entry in self.testEntries:
ew.writeTestEntry(entry)
ew.save()
def createTestEntry(xmlObj):
# retrieve ID and name
idRaw = xmlObj.find("doc", recursive=False).contents
mg = re.search(r"Test ID: ([0-9\.]*)$", idRaw[0].string, re.MULTILINE)
testId = mg.group(1)
name = xmlObj["name"]
#retrieve status and error message (if FAIL)
statusObj = xmlObj.find("status", recursive=False)
cts = statusObj.contents
errorMsg = cts[0] if len(cts) > 0 else ""
result = statusObj["status"]
return TestEntry(testId, name, result, errorMsg)
def display_usage():
print(usage_str)
if __name__ == "__main__":
if len(argv) < 2:
display_usage()
exit()
TestOutputParser(argv[1]).run_parser()