This answer borrows from Bence's example, but adds functionality of echoing grand total summary lines.
#!/usr/bin/env python3
# invoke with robot --listener API-Libs/RF_Listener.py
import xml.etree.ElementTree as xmlElementTree
ROBOT_LISTENER_API_VERSION = 3
class RF_Listener: # class should be same as filename
def __init__(self):
self.ROBOT_LISTENER_API_VERSION = 3
def output_file(self, path): # Listener that parses the output XML when it is ready
root = xmlElementTree.parse(path).getroot()
for type_tag in root.findall('./statistics/total/stat'):
# <stat pass="1" fail="2">Critical Tests</stat>
# <stat pass="3" fail="4">All Tests</stat>
cntPassed = int(type_tag.attrib.get("pass")) # attrib is dict-like (except for 'text')
cntFailed = int(type_tag.attrib.get("fail"))
cntTests = cntPassed + cntFailed
pct_pass = cntPassed / cntTests * 100
fmt_str = "{}: {} tests, {} passed, {} failed, {:.3g}% pass rate (--listener summary)"
print(fmt_str.format(type_tag.text,cntTests, cntPassed, cntFailed,pct_pass))
# optionally write grand total results summary to a file
This version prints to STDOUT instead of writing to a file, thus inserting these two lines near the end of the output. Suitable for grep
(esp. for use in Jenkins job description-setter
plugin.)
Critical Tests: 3 tests, 1 passed, 2 failed, 33.3% pass rate (--listener summary)
All Tests: 7 tests, 3 passed, 4 failed, 42.9% pass rate (--listener summary)