aboutsummaryrefslogtreecommitdiff
path: root/scripts/xunit/xunit.py
blob: c63613610d1ce854df27ce9085ddd22b70bf7bf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91


# Copyright (c) 2020, ARM Limited.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

from __future__ import print_function
import xml.etree.ElementTree as ET

class xunit_results():
    def __init__(self, name='Testsuites'):
        self.name = name
        self.suites = []
    def create_suite(self, name):
        s = xunit_suite(name)
        self.suites.append(s)
        return s
    def write_results(self, filename):
        suites = ET.Element(self.name)
        tree = ET.ElementTree(suites)
        for s in self.suites:
            testsuite = ET.SubElement(suites, 'testsuite', {'name' : s.name, 'errors' : '0'})
            tests = 0
            failures = 0
            skip = 0
            for t in s.tests:
                test = ET.SubElement(testsuite, 'testcase', {'name' : t.name, 'classname' : t.classname, 'time' : t.time})
                tests += 1
                if t.skip:
                    skip += 1
                    ET.SubElement(test, 'skipped', {'type' : 'Skipped test'})
                if t.fail:
                    failures += 1
                    fail = ET.SubElement(test, 'failure', {'type' : 'Test failed'})
                    fail.text = t.fail
                if t.sysout:
                    sysout = ET.SubElement(test, 'system-out')
                    sysout.text = t.sysout
                if t.syserr:
                    syserr = ET.SubElement(test, 'system-err')
                    syserr.text = t.syserr
            testsuite.attrib['tests'] = str(tests)
            testsuite.attrib['failures'] = str(failures)
            testsuite.attrib['skip'] = str(skip)
        tree.write(filename, 'UTF-8', True)


class xunit_suite():
    def __init__(self, name):
        self.name = name
        self.tests = []

class xunit_test():
    def __init__(self, name, classname=None):
        self.name = name
        if classname:
            self.classname = classname
        else:
            self.classname = name
        self.time = '0.000'
        self.fail = None
        self.skip = False
        self.sysout = None
        self.syserr = None
    def failed(self, text):
        self.fail = text
    def skipped(self):
        self.skip = True


if __name__ == '__main__':
    r = xunit_results()
    s = r.create_suite('selftest')
    for i in range(0,10):
        t = xunit_test('atest' + str(i))
        if i == 3:
            t.failed('Unknown failure foo')
        if i == 7:
            t.skipped()
        s.tests.append(t)
    r.write_results('foo.xml')