Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 1 | # Copyright (c) 2020-2021, ARM Limited. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import xml.etree.ElementTree as ET |
| 16 | from xml.dom import minidom |
| 17 | |
| 18 | |
| 19 | class xunit_results: |
| 20 | def __init__(self): |
| 21 | self.name = "testsuites" |
| 22 | self.suites = [] |
| 23 | |
| 24 | def create_suite(self, name): |
| 25 | s = xunit_suite(name) |
| 26 | self.suites.append(s) |
| 27 | return s |
| 28 | |
| 29 | def write_results(self, filename): |
| 30 | suites = ET.Element(self.name) |
| 31 | tree = ET.ElementTree(suites) |
| 32 | for s in self.suites: |
| 33 | testsuite = ET.SubElement( |
| 34 | suites, "testsuite", {"name": s.name, "errors": "0"} |
| 35 | ) |
| 36 | tests = 0 |
| 37 | failures = 0 |
| 38 | skip = 0 |
| 39 | for t in s.tests: |
| 40 | test = ET.SubElement( |
| 41 | testsuite, |
| 42 | "testcase", |
| 43 | {"name": t.name, "classname": t.classname, "time": t.time}, |
| 44 | ) |
| 45 | tests += 1 |
| 46 | if t.skip: |
| 47 | skip += 1 |
| 48 | ET.SubElement(test, "skipped", {"type": "Skipped test"}) |
| 49 | if t.fail: |
| 50 | failures += 1 |
| 51 | fail = ET.SubElement(test, "failure", {"type": "Test failed"}) |
| 52 | fail.text = t.fail |
| 53 | if t.sysout: |
| 54 | sysout = ET.SubElement(test, "system-out") |
| 55 | sysout.text = t.sysout |
| 56 | if t.syserr: |
| 57 | syserr = ET.SubElement(test, "system-err") |
| 58 | syserr.text = t.syserr |
| 59 | testsuite.attrib["tests"] = str(tests) |
| 60 | testsuite.attrib["failures"] = str(failures) |
| 61 | testsuite.attrib["skip"] = str(skip) |
| 62 | xmlstr = minidom.parseString(ET.tostring(tree.getroot())).toprettyxml( |
| 63 | indent=" " |
| 64 | ) |
| 65 | with open(filename, "w") as f: |
| 66 | f.write(xmlstr) |
| 67 | |
| 68 | |
| 69 | class xunit_suite: |
| 70 | def __init__(self, name): |
| 71 | self.name = name |
| 72 | self.tests = [] |
| 73 | |
| 74 | |
| 75 | # classname should be of the form suite.class/subclass/subclass2/... It appears |
| 76 | # you can have an unlimited number of subclasses in this manner |
| 77 | |
| 78 | |
| 79 | class xunit_test: |
| 80 | def __init__(self, name, classname=None): |
| 81 | self.name = name |
| 82 | if classname: |
| 83 | self.classname = classname |
| 84 | else: |
| 85 | self.classname = name |
| 86 | self.time = "0.000" |
| 87 | self.fail = None |
| 88 | self.skip = False |
| 89 | self.sysout = None |
| 90 | self.syserr = None |
| 91 | |
| 92 | def failed(self, text): |
| 93 | self.fail = text |
| 94 | |
| 95 | def skipped(self): |
| 96 | self.skip = True |
| 97 | |
| 98 | |
| 99 | if __name__ == "__main__": |
| 100 | r = xunit_results() |
| 101 | s = r.create_suite("selftest") |
| 102 | for i in range(0, 10): |
| 103 | t = xunit_test("atest" + str(i), "selftest") |
| 104 | if i == 3: |
| 105 | t.failed("Unknown failure foo") |
| 106 | if i == 7: |
| 107 | t.skipped() |
| 108 | s.tests.append(t) |
| 109 | r.write_results("foo.xml") |