summaryrefslogtreecommitdiff
path: root/lib/ansible/utils/_junit_xml.py
blob: 31aff862245bf3ee475349af47a982ccd6e425da (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"""
Dataclasses for creating JUnit XML files.
See: https://github.com/junit-team/junit5/blob/main/platform-tests/src/test/resources/jenkins-junit.xsd
"""
from __future__ import annotations

import abc
import dataclasses
import datetime
import decimal

from xml.dom import minidom

# noinspection PyPep8Naming
from xml.etree import ElementTree as ET


@dataclasses.dataclass  # type: ignore[misc]  # https://github.com/python/mypy/issues/5374
class TestResult(metaclass=abc.ABCMeta):
    """Base class for the result of a test case."""

    output: str | None = None
    message: str | None = None
    type: str | None = None

    def __post_init__(self):
        if self.type is None:
            self.type = self.tag

    @property
    @abc.abstractmethod
    def tag(self) -> str:
        """Tag name for the XML element created by this result type."""

    def get_attributes(self) -> dict[str, str]:
        """Return a dictionary of attributes for this instance."""
        return _attributes(
            message=self.message,
            type=self.type,
        )

    def get_xml_element(self) -> ET.Element:
        """Return an XML element representing this instance."""
        element = ET.Element(self.tag, self.get_attributes())
        element.text = self.output

        return element


@dataclasses.dataclass
class TestFailure(TestResult):
    """Failure info for a test case."""

    @property
    def tag(self) -> str:
        """Tag name for the XML element created by this result type."""
        return 'failure'


@dataclasses.dataclass
class TestError(TestResult):
    """Error info for a test case."""

    @property
    def tag(self) -> str:
        """Tag name for the XML element created by this result type."""
        return 'error'


@dataclasses.dataclass
class TestCase:
    """An individual test case."""

    name: str
    assertions: int | None = None
    classname: str | None = None
    status: str | None = None
    time: decimal.Decimal | None = None

    errors: list[TestError] = dataclasses.field(default_factory=list)
    failures: list[TestFailure] = dataclasses.field(default_factory=list)
    skipped: str | None = None
    system_out: str | None = None
    system_err: str | None = None

    is_disabled: bool = False

    @property
    def is_failure(self) -> bool:
        """True if the test case contains failure info."""
        return bool(self.failures)

    @property
    def is_error(self) -> bool:
        """True if the test case contains error info."""
        return bool(self.errors)

    @property
    def is_skipped(self) -> bool:
        """True if the test case was skipped."""
        return bool(self.skipped)

    def get_attributes(self) -> dict[str, str]:
        """Return a dictionary of attributes for this instance."""
        return _attributes(
            assertions=self.assertions,
            classname=self.classname,
            name=self.name,
            status=self.status,
            time=self.time,
        )

    def get_xml_element(self) -> ET.Element:
        """Return an XML element representing this instance."""
        element = ET.Element('testcase', self.get_attributes())

        if self.skipped:
            ET.SubElement(element, 'skipped').text = self.skipped

        element.extend([error.get_xml_element() for error in self.errors])
        element.extend([failure.get_xml_element() for failure in self.failures])

        if self.system_out:
            ET.SubElement(element, 'system-out').text = self.system_out

        if self.system_err:
            ET.SubElement(element, 'system-err').text = self.system_err

        return element


@dataclasses.dataclass
class TestSuite:
    """A collection of test cases."""

    name: str
    hostname: str | None = None
    id: str | None = None
    package: str | None = None
    timestamp: datetime.datetime | None = None

    properties: dict[str, str] = dataclasses.field(default_factory=dict)
    cases: list[TestCase] = dataclasses.field(default_factory=list)
    system_out: str | None = None
    system_err: str | None = None

    @property
    def disabled(self) -> int:
        """The number of disabled test cases."""
        return sum(case.is_disabled for case in self.cases)

    @property
    def errors(self) -> int:
        """The number of test cases containing error info."""
        return sum(case.is_error for case in self.cases)

    @property
    def failures(self) -> int:
        """The number of test cases containing failure info."""
        return sum(case.is_failure for case in self.cases)

    @property
    def skipped(self) -> int:
        """The number of test cases containing skipped info."""
        return sum(case.is_skipped for case in self.cases)

    @property
    def tests(self) -> int:
        """The number of test cases."""
        return len(self.cases)

    @property
    def time(self) -> decimal.Decimal:
        """The total time from all test cases."""
        return decimal.Decimal(sum(case.time for case in self.cases if case.time))

    def get_attributes(self) -> dict[str, str]:
        """Return a dictionary of attributes for this instance."""
        return _attributes(
            disabled=self.disabled,
            errors=self.errors,
            failures=self.failures,
            hostname=self.hostname,
            id=self.id,
            name=self.name,
            package=self.package,
            skipped=self.skipped,
            tests=self.tests,
            time=self.time,
            timestamp=self.timestamp.isoformat(timespec='seconds') if self.timestamp else None,
        )

    def get_xml_element(self) -> ET.Element:
        """Return an XML element representing this instance."""
        element = ET.Element('testsuite', self.get_attributes())

        if self.properties:
            ET.SubElement(element, 'properties').extend([ET.Element('property', dict(name=name, value=value)) for name, value in self.properties.items()])

        element.extend([test_case.get_xml_element() for test_case in self.cases])

        if self.system_out:
            ET.SubElement(element, 'system-out').text = self.system_out

        if self.system_err:
            ET.SubElement(element, 'system-err').text = self.system_err

        return element


@dataclasses.dataclass
class TestSuites:
    """A collection of test suites."""

    name: str | None = None

    suites: list[TestSuite] = dataclasses.field(default_factory=list)

    @property
    def disabled(self) -> int:
        """The number of disabled test cases."""
        return sum(suite.disabled for suite in self.suites)

    @property
    def errors(self) -> int:
        """The number of test cases containing error info."""
        return sum(suite.errors for suite in self.suites)

    @property
    def failures(self) -> int:
        """The number of test cases containing failure info."""
        return sum(suite.failures for suite in self.suites)

    @property
    def tests(self) -> int:
        """The number of test cases."""
        return sum(suite.tests for suite in self.suites)

    @property
    def time(self) -> decimal.Decimal:
        """The total time from all test cases."""
        return decimal.Decimal(sum(suite.time for suite in self.suites))

    def get_attributes(self) -> dict[str, str]:
        """Return a dictionary of attributes for this instance."""
        return _attributes(
            disabled=self.disabled,
            errors=self.errors,
            failures=self.failures,
            name=self.name,
            tests=self.tests,
            time=self.time,
        )

    def get_xml_element(self) -> ET.Element:
        """Return an XML element representing this instance."""
        element = ET.Element('testsuites', self.get_attributes())
        element.extend([suite.get_xml_element() for suite in self.suites])

        return element

    def to_pretty_xml(self) -> str:
        """Return a pretty formatted XML string representing this instance."""
        return _pretty_xml(self.get_xml_element())


def _attributes(**kwargs) -> dict[str, str]:
    """Return the given kwargs as a dictionary with values converted to strings. Items with a value of None will be omitted."""
    return {key: str(value) for key, value in kwargs.items() if value is not None}


def _pretty_xml(element: ET.Element) -> str:
    """Return a pretty formatted XML string representing the given element."""
    return minidom.parseString(ET.tostring(element, encoding='unicode')).toprettyxml()