Last active
October 28, 2020 06:42
-
-
Save sveinungkb/affa21bbcb84658594a9 to your computer and use it in GitHub Desktop.
HP Unified Functional Testing report to ANT JUnit XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# By Sveinung Kval Bakken @ iHeartRadio | |
# http://tech.iheart.com | |
# | |
# This script will parse any HP Unified Functional Testing reports and generate ANT JUnit XML reports | |
# for use with Team City / Jenkins | |
import sys | |
from xml.dom import minidom | |
from xml.dom.minidom import Document, Element | |
if len(sys.argv) < 2: | |
print("Pass HP XML results as parameters, like this: python generate-report.py Results.xml OtherResults.xml, passed arguments: %s"% sys.argv[1:]) | |
files = sys.argv[1:] | |
def printChildren(element, depth, indent): | |
if depth == 0: | |
return | |
for i in range(0, indent): | |
print('.'), | |
if element.nodeType == element.TEXT_NODE: | |
print("Text: %s"% element.wholeText), | |
elif element.nodeType == element.CDATA_SECTION_NODE: | |
print("Cdata: %s"% element.wholeText), | |
else: | |
print("Element: %s"% element.tagName), | |
print("") | |
for child in element.childNodes: | |
printChildren(child, depth -1, indent +1) | |
def valueForCdata(cdataElement): | |
return cdataElement.firstChild.wholeText | |
class Step: | |
def __init__(self, stepElement): | |
self.name = valueForCdata(stepElement.getElementsByTagName("Obj")[0]) | |
nodeArgsElement = stepElement.getElementsByTagName("NodeArgs")[0] | |
self.status = nodeArgsElement.getAttribute("status") | |
self.passed = self.status == "Passed" | |
self.details = stepElement.getElementsByTagName("Details")[0] | |
self.time = float(stepElement.getElementsByTagName("TimeTick")[0].firstChild.nodeValue) | |
self.duration = 0 | |
def asElement(self): | |
testcase = Element("testcase", "") | |
testcase.setAttribute("name", self.name) | |
testcase.setAttribute("time", str(self.duration)) | |
if not self.passed: | |
failureMessageElement = Element("failure") | |
failureMessageElement.setAttribute("message", valueForCdata(self.details)) | |
testcase.appendChild(failureMessageElement) | |
return testcase | |
class Action: | |
def __init__(self, actionElement): | |
actionNameElement = actionElement.getElementsByTagName("AName")[0] | |
self.name = valueForCdata(actionNameElement) | |
print("Processing action: %s" % self.name) | |
self.steps = [] | |
self.total = 0 | |
self.failures = 0 | |
stepElements = actionElement.getElementsByTagName("Step") | |
lastTime = 0 | |
for stepElement in stepElements: | |
step = Step(stepElement) | |
if (lastTime != 0): | |
step.duration = step.time - lastTime | |
lastTime = step.time | |
self.steps.append(step) | |
self.total = self.total +1 | |
if not step.passed: | |
self.failures = self.failures + 1 | |
def asElement(self): | |
testsuiteElement = Element("testsuite", "") | |
testsuiteElement.setAttribute("name", self.name) | |
for step in self.steps: | |
testsuiteElement.appendChild(step.asElement()) | |
testsuiteElement.setAttribute("tests", str(self.total)) | |
testsuiteElement.setAttribute("failures", str(self.failures)) | |
return testsuiteElement | |
def processStep(stepElement): | |
print("procesing step: %s" % stepElement.getAttribute("rID")) | |
testcase = Element("testcase", "") | |
obj = stepElement.getElementsByTagName("Obj")[0] | |
testcase.setAttribute("name", valueForCdata(obj)) | |
return testcase | |
def parseReport(reportFile): | |
print("Processing: %s"% reportFile) | |
reportDoc = minidom.parse(reportFile) | |
# printChildren(reportDoc.documentElement, 3, 0) | |
# Setup report | |
reportName = valueForCdata(reportDoc.getElementsByTagName("DName")[0]) | |
junitReport = Document() | |
testsuitesElement = junitReport.createElement("testsuites") | |
testsuitesElement.setAttribute("name", reportName) | |
junitReport.appendChild(testsuitesElement) | |
total = 0 | |
failures = 0 | |
actionElements = reportDoc.getElementsByTagName("Action") | |
for actionElement in actionElements: | |
action = Action(actionElement) | |
total = total + action.total | |
failures = failures + action.failures | |
testsuitesElement.appendChild(action.asElement()) | |
testsuitesElement.setAttribute("failures", str(failures)) | |
testsuitesElement.setAttribute("tests", str(total)) | |
reportName = format("%s-report.xml" % reportFile.replace(".xml", "")) | |
testsuitesElement.writexml( open(reportName, 'w'), indent=" ", addindent=" ", newl='\n') | |
testsuitesElement.unlink() | |
print("Wrote JUnit report to: %s" % reportName) | |
#print(junitReport.toprettyxml()) | |
for file in files: | |
parseReport(file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @sveinungkb, for your support. I will give a shot & try to tweak the script for me.
Any how, thanks for your support. Have a good day!