Skip to content
parseTestResults.py 1.87 KiB
Newer Older
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()