Coverage for pyEDAA/Reports/Unittesting/JUnit/AntJUnit4.py: 45%
183 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-06 22:25 +0000
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-06 22:25 +0000
1# ==================================================================================================================== #
2# _____ ____ _ _ ____ _ #
3# _ __ _ _| ____| _ \ / \ / \ | _ \ ___ _ __ ___ _ __| |_ ___ #
4# | '_ \| | | | _| | | | |/ _ \ / _ \ | |_) / _ \ '_ \ / _ \| '__| __/ __| #
5# | |_) | |_| | |___| |_| / ___ \ / ___ \ _| _ < __/ |_) | (_) | | | |_\__ \ #
6# | .__/ \__, |_____|____/_/ \_\/_/ \_(_)_| \_\___| .__/ \___/|_| \__|___/ #
7# |_| |___/ |_| #
8# ==================================================================================================================== #
9# Authors: #
10# Patrick Lehmann #
11# #
12# License: #
13# ==================================================================================================================== #
14# Copyright 2024-2025 Electronic Design Automation Abstraction (EDA²) #
15# Copyright 2023-2023 Patrick Lehmann - Bötzingen, Germany #
16# #
17# Licensed under the Apache License, Version 2.0 (the "License"); #
18# you may not use this file except in compliance with the License. #
19# You may obtain a copy of the License at #
20# #
21# http://www.apache.org/licenses/LICENSE-2.0 #
22# #
23# Unless required by applicable law or agreed to in writing, software #
24# distributed under the License is distributed on an "AS IS" BASIS, #
25# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
26# See the License for the specific language governing permissions and #
27# limitations under the License. #
28# #
29# SPDX-License-Identifier: Apache-2.0 #
30# ==================================================================================================================== #
31#
32"""
33Reader for JUnit unit testing summary files in XML format.
34"""
35from pathlib import Path
36from time import perf_counter_ns
37from typing import Optional as Nullable, Generator, Tuple, Union, TypeVar, Type, ClassVar
39from lxml.etree import ElementTree, Element, SubElement, tostring, _Element
40from pyTooling.Common import firstValue
41from pyTooling.Decorators import export, InheritDocString
43from pyEDAA.Reports.Unittesting import UnittestException, TestsuiteKind
44from pyEDAA.Reports.Unittesting import TestcaseStatus, TestsuiteStatus, IterationScheme
45from pyEDAA.Reports.Unittesting import TestsuiteSummary as ut_TestsuiteSummary, Testsuite as ut_Testsuite
46from pyEDAA.Reports.Unittesting.JUnit import Testcase as ju_Testcase, Testclass as ju_Testclass, Testsuite as ju_Testsuite
47from pyEDAA.Reports.Unittesting.JUnit import TestsuiteSummary as ju_TestsuiteSummary, Document as ju_Document
49TestsuiteType = TypeVar("TestsuiteType", bound="Testsuite")
50TestcaseAggregateReturnType = Tuple[int, int, int]
51TestsuiteAggregateReturnType = Tuple[int, int, int, int, int]
54@export
55@InheritDocString(ju_Testcase, merge=True)
56class Testcase(ju_Testcase):
57 """
58 This is a derived implementation for the Ant + JUnit4 dialect.
59 """
62@export
63@InheritDocString(ju_Testclass, merge=True)
64class Testclass(ju_Testclass):
65 """
66 This is a derived implementation for the Ant + JUnit4 dialect.
67 """
70@export
71@InheritDocString(ju_Testsuite, merge=True)
72class Testsuite(ju_Testsuite):
73 """
74 This is a derived implementation for the Ant + JUnit4 dialect.
75 """
77 def Aggregate(self, strict: bool = True) -> TestsuiteAggregateReturnType:
78 tests, skipped, errored, failed, passed = super().Aggregate()
80 for testclass in self._testclasses.values(): # type: Testclass
81 _ = testclass.Aggregate(strict)
83 tests += 1
85 status = testclass._status
86 if status is TestcaseStatus.Unknown:
87 raise UnittestException(f"Found testclass '{testclass._name}' with state 'Unknown'.")
88 elif status is TestcaseStatus.Skipped:
89 skipped += 1
90 elif status is TestcaseStatus.Errored:
91 errored += 1
92 elif status is TestcaseStatus.Passed:
93 passed += 1
94 elif status is TestcaseStatus.Failed:
95 failed += 1
96 elif status & TestcaseStatus.Mask is not TestcaseStatus.Unknown:
97 raise UnittestException(f"Found testclass '{testclass._name}' with unsupported state '{status}'.")
98 else:
99 raise UnittestException(f"Internal error for testclass '{testclass._name}', field '_status' is '{status}'.")
101 self._tests = tests
102 self._skipped = skipped
103 self._errored = errored
104 self._failed = failed
105 self._passed = passed
107 if errored > 0:
108 self._status = TestsuiteStatus.Errored
109 elif failed > 0:
110 self._status = TestsuiteStatus.Failed
111 elif tests == 0:
112 self._status = TestsuiteStatus.Empty
113 elif tests - skipped == passed:
114 self._status = TestsuiteStatus.Passed
115 elif tests == skipped:
116 self._status = TestsuiteStatus.Skipped
117 else:
118 self._status = TestsuiteStatus.Unknown
120 return tests, skipped, errored, failed, passed
122 @classmethod
123 def FromTestsuite(cls, testsuite: ut_Testsuite) -> "Testsuite":
124 """
125 Convert a test suite of the unified test entity data model to the JUnit specific data model's test suite object
126 adhering to the Ant + JUnit4 dialect.
128 :param testsuite: Test suite from unified data model.
129 :return: Test suite from JUnit specific data model (Ant + JUnit4 dialect).
130 """
131 juTestsuite = cls(
132 testsuite._name,
133 startTime=testsuite._startTime,
134 duration=testsuite._totalDuration,
135 status= testsuite._status,
136 )
138 juTestsuite._tests = testsuite._tests
139 juTestsuite._skipped = testsuite._skipped
140 juTestsuite._errored = testsuite._errored
141 juTestsuite._failed = testsuite._failed
142 juTestsuite._passed = testsuite._passed
144 for tc in testsuite.IterateTestcases():
145 ts = tc._parent
146 if ts is None:
147 raise UnittestException(f"Testcase '{tc._name}' is not part of a hierarchy.")
149 classname = ts._name
150 ts = ts._parent
151 while ts is not None and ts._kind > TestsuiteKind.Logical:
152 classname = f"{ts._name}.{classname}"
153 ts = ts._parent
155 if classname in juTestsuite._testclasses:
156 juClass = juTestsuite._testclasses[classname]
157 else:
158 juClass = Testclass(classname, parent=juTestsuite)
160 juClass.AddTestcase(Testcase.FromTestcase(tc))
162 return juTestsuite
165@export
166@InheritDocString(ju_TestsuiteSummary, merge=True)
167class TestsuiteSummary(ju_TestsuiteSummary):
168 """
169 This is a derived implementation for the Ant + JUnit4 dialect.
170 """
172 @classmethod
173 def FromTestsuiteSummary(cls, testsuiteSummary: ut_TestsuiteSummary) -> "TestsuiteSummary":
174 """
175 Convert a test suite summary of the unified test entity data model to the JUnit specific data model's test suite
176 summary object adhering to the Ant + JUnit4 dialect.
178 :param testsuiteSummary: Test suite summary from unified data model.
179 :return: Test suite summary from JUnit specific data model (Ant + JUnit4 dialect).
180 """
181 return cls(
182 testsuiteSummary._name,
183 startTime=testsuiteSummary._startTime,
184 duration=testsuiteSummary._totalDuration,
185 status=testsuiteSummary._status,
186 testsuites=(ut_Testsuite.FromTestsuite(testsuite) for testsuite in testsuiteSummary._testsuites.values())
187 )
190@export
191class Document(ju_Document):
192 """
193 A document reader and writer for the Ant + JUnit4 XML file format.
195 This class reads, validates and transforms an XML file in the Ant + JUnit4 format into a JUnit data model. It can then
196 be converted into a unified test entity data model.
198 In reverse, a JUnit data model instance with the specific Ant + JUnit4 file format can be created from a unified test
199 entity data model. This data model can be written as XML into a file.
200 """
202 _TESTCASE: ClassVar[Type[Testcase]] = Testcase
203 _TESTCLASS: ClassVar[Type[Testclass]] = Testclass
204 _TESTSUITE: ClassVar[Type[Testsuite]] = Testsuite
206 @classmethod
207 def FromTestsuiteSummary(cls, xmlReportFile: Path, testsuiteSummary: ut_TestsuiteSummary):
208 doc = cls(xmlReportFile)
209 doc._name = testsuiteSummary._name
210 doc._startTime = testsuiteSummary._startTime
211 doc._duration = testsuiteSummary._totalDuration
212 doc._status = testsuiteSummary._status
213 doc._tests = testsuiteSummary._tests
214 doc._skipped = testsuiteSummary._skipped
215 doc._errored = testsuiteSummary._errored
216 doc._failed = testsuiteSummary._failed
217 doc._passed = testsuiteSummary._passed
219 doc.AddTestsuites(Testsuite.FromTestsuite(testsuite) for testsuite in testsuiteSummary._testsuites.values())
221 return doc
223 def Analyze(self) -> None:
224 """
225 Analyze the XML file, parse the content into an XML data structure and validate the data structure using an XML
226 schema.
228 .. hint::
230 The time spend for analysis will be made available via property :data:`AnalysisDuration`..
232 The used XML schema definition is specific to the Ant JUnit4 dialect.
233 """
234 xmlSchemaFile = "Ant-JUnit4.xsd"
235 self._Analyze(xmlSchemaFile)
237 def Write(self, path: Nullable[Path] = None, overwrite: bool = False, regenerate: bool = False) -> None:
238 """
239 Write the data model as XML into a file adhering to the Ant + JUnit4 dialect.
241 :param path: Optional path to the XMl file, if internal path shouldn't be used.
242 :param overwrite: If true, overwrite an existing file.
243 :param regenerate: If true, regenerate the XML structure from data model.
244 :raises UnittestException: If the file cannot be overwritten.
245 :raises UnittestException: If the internal XML data structure wasn't generated.
246 :raises UnittestException: If the file cannot be opened or written.
247 """
248 if path is None: 248 ↛ 249line 248 didn't jump to line 249 because the condition on line 248 was never true
249 path = self._path
251 if not overwrite and path.exists(): 251 ↛ 252line 251 didn't jump to line 252 because the condition on line 251 was never true
252 raise UnittestException(f"JUnit XML file '{path}' can not be overwritten.") \
253 from FileExistsError(f"File '{path}' already exists.")
255 if regenerate: 255 ↛ 258line 255 didn't jump to line 258 because the condition on line 255 was always true
256 self.Generate(overwrite=True)
258 if self._xmlDocument is None: 258 ↛ 259line 258 didn't jump to line 259 because the condition on line 258 was never true
259 ex = UnittestException(f"Internal XML document tree is empty and needs to be generated before write is possible.")
260 ex.add_note(f"Call 'JUnitDocument.Generate()' or 'JUnitDocument.Write(..., regenerate=True)'.")
261 raise ex
263 try:
264 with path.open("wb") as file:
265 file.write(tostring(self._xmlDocument, encoding="utf-8", xml_declaration=True, pretty_print=True))
266 except Exception as ex:
267 raise UnittestException(f"JUnit XML file '{path}' can not be written.") from ex
269 def Convert(self) -> None:
270 """
271 Convert the parsed and validated XML data structure into a JUnit test entity hierarchy.
273 This method converts the root element.
275 .. hint::
277 The time spend for model conversion will be made available via property :data:`ModelConversionDuration`.
279 :raises UnittestException: If XML was not read and parsed before.
280 """
281 if self._xmlDocument is None: 281 ↛ 282line 281 didn't jump to line 282 because the condition on line 281 was never true
282 ex = UnittestException(f"JUnit XML file '{self._path}' needs to be read and analyzed by an XML parser.")
283 ex.add_note(f"Call 'JUnitDocument.Analyze()' or create the document using 'JUnitDocument(path, parse=True)'.")
284 raise ex
286 startConversion = perf_counter_ns()
287 rootElement: _Element = self._xmlDocument.getroot()
289 self._name = self._ConvertName(rootElement, optional=True)
290 self._startTime =self._ConvertTimestamp(rootElement, optional=True)
291 self._duration = self._ConvertTime(rootElement, optional=True)
293 # tests = rootElement.getAttribute("tests")
294 # skipped = rootElement.getAttribute("skipped")
295 # errors = rootElement.getAttribute("errors")
296 # failures = rootElement.getAttribute("failures")
297 # assertions = rootElement.getAttribute("assertions")
299 ts = Testsuite(self._name, startTime=self._startTime, duration=self._duration, parent=self)
300 self._ConvertTestsuiteChildren(rootElement, ts)
302 self.Aggregate()
303 endConversation = perf_counter_ns()
304 self._modelConversion = (endConversation - startConversion) / 1e9
306 def _ConvertTestsuite(self, parent: TestsuiteSummary, testsuitesNode: _Element) -> None:
307 """
308 Convert the XML data structure of a ``<testsuite>`` to a test suite.
310 This method uses private helper methods provided by the base-class.
312 :param parent: The test suite summary as a parent element in the test entity hierarchy.
313 :param testsuitesNode: The current XML element node representing a test suite.
314 """
315 newTestsuite = Testsuite(
316 self._ConvertName(testsuitesNode, optional=False),
317 self._ConvertHostname(testsuitesNode, optional=False),
318 self._ConvertTimestamp(testsuitesNode, optional=False),
319 self._ConvertTime(testsuitesNode, optional=False),
320 parent=parent
321 )
323 self._ConvertTestsuiteChildren(testsuitesNode, newTestsuite)
325 def Generate(self, overwrite: bool = False) -> None:
326 """
327 Generate the internal XML data structure from test suites and test cases.
329 This method generates the XML root element (``<testsuite>``) and recursively calls other generated methods.
331 :param overwrite: Overwrite the internal XML data structure.
332 :raises UnittestException: If overwrite is false and the internal XML data structure is not empty.
333 """
334 if not overwrite and self._xmlDocument is not None: 334 ↛ 335line 334 didn't jump to line 335 because the condition on line 334 was never true
335 raise UnittestException(f"Internal XML document is populated with data.")
337 if self.TestsuiteCount != 1: 337 ↛ 338line 337 didn't jump to line 338 because the condition on line 337 was never true
338 ex = UnittestException(f"The Ant + JUnit4 format requires exactly one test suite.")
339 ex.add_note(f"Found {self.TestsuiteCount} test suites.")
340 raise ex
342 testsuite = firstValue(self._testsuites)
344 rootElement = Element("testsuite")
345 rootElement.attrib["name"] = self._name
346 if self._startTime is not None: 346 ↛ 348line 346 didn't jump to line 348 because the condition on line 346 was always true
347 rootElement.attrib["timestamp"] = f"{self._startTime.isoformat()}"
348 if self._duration is not None: 348 ↛ 350line 348 didn't jump to line 350 because the condition on line 348 was always true
349 rootElement.attrib["time"] = f"{self._duration.total_seconds():.6f}"
350 rootElement.attrib["tests"] = str(self._tests)
351 rootElement.attrib["failures"] = str(self._failed)
352 rootElement.attrib["errors"] = str(self._errored)
353 rootElement.attrib["skipped"] = str(self._skipped)
354 # if self._assertionCount is not None:
355 # rootElement.attrib["assertions"] = f"{self._assertionCount}"
356 if testsuite._hostname is not None: 356 ↛ 357line 356 didn't jump to line 357 because the condition on line 356 was never true
357 rootElement.attrib["hostname"] = testsuite._hostname
359 self._xmlDocument = ElementTree(rootElement)
361 for testclass in testsuite._testclasses.values():
362 for tc in testclass._testcases.values():
363 self._GenerateTestcase(tc, rootElement)
365 def _GenerateTestcase(self, testcase: Testcase, parentElement: _Element) -> None:
366 """
367 Generate the internal XML data structure for a test case.
369 This method generates the XML element (``<testcase>``) and recursively calls other generated methods.
371 :param testcase: The test case to convert to an XML data structures.
372 :param parentElement: The parent XML data structure element, this data structure part will be added to.
373 :return:
374 """
375 testcaseElement = SubElement(parentElement, "testcase")
376 if testcase.Classname is not None: 376 ↛ 378line 376 didn't jump to line 378 because the condition on line 376 was always true
377 testcaseElement.attrib["classname"] = testcase.Classname
378 testcaseElement.attrib["name"] = testcase._name
379 if testcase._duration is not None: 379 ↛ 381line 379 didn't jump to line 381 because the condition on line 379 was always true
380 testcaseElement.attrib["time"] = f"{testcase._duration.total_seconds():.6f}"
381 if testcase._assertionCount is not None: 381 ↛ 382line 381 didn't jump to line 382 because the condition on line 381 was never true
382 testcaseElement.attrib["assertions"] = f"{testcase._assertionCount}"
384 if testcase._status is TestcaseStatus.Passed:
385 pass
386 elif testcase._status is TestcaseStatus.Failed: 386 ↛ 388line 386 didn't jump to line 388 because the condition on line 386 was always true
387 failureElement = SubElement(testcaseElement, "failure")
388 elif testcase._status is TestcaseStatus.Skipped:
389 skippedElement = SubElement(testcaseElement, "skipped")
390 else:
391 errorElement = SubElement(testcaseElement, "error")