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