Coverage for pyEDAA/Reports/Unittesting/JUnit/GoogleTestJUnit.py: 49%
194 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-16 22:20 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-16 22:20 +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.Decorators import export, InheritDocString
42from pyEDAA.Reports.Unittesting import UnittestException, TestsuiteKind
43from pyEDAA.Reports.Unittesting import TestcaseStatus, TestsuiteStatus, IterationScheme
44from pyEDAA.Reports.Unittesting import TestsuiteSummary as ut_TestsuiteSummary, Testsuite as ut_Testsuite
45from pyEDAA.Reports.Unittesting.JUnit import Testcase as ju_Testcase, Testclass as ju_Testclass, Testsuite as ju_Testsuite
46from 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 GoogleTest JUnit dialect.
59 """
62@export
63@InheritDocString(ju_Testclass, merge=True)
64class Testclass(ju_Testclass):
65 """
66 This is a derived implementation for the GoogleTest JUnit dialect.
67 """
70@export
71@InheritDocString(ju_Testsuite, merge=True)
72class Testsuite(ju_Testsuite):
73 """
74 This is a derived implementation for the GoogleTest JUnit 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():
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 GoogleTest JUnit dialect.
128 :param testsuite: Test suite from unified data model.
129 :return: Test suite from JUnit specific data model (GoogleTest JUnit 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 GoogleTest JUnit 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 GoogleTest JUnit dialect.
178 :param testsuiteSummary: Test suite summary from unified data model.
179 :return: Test suite summary from JUnit specific data model (GoogleTest JUnit 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 GoogelTest JUnit XML file format.
195 This class reads, validates and transforms an XML file in the GoogelTest JUnit format into a JUnit data model. It can
196 then be converted into a unified test entity data model.
198 In reverse, a JUnit data model instance with the specific GoogelTest JUnit file format can be created from a unified
199 test 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 GoogleTest JUnit dialect.
233 """
234 xmlSchemaFile = "GoogleTest-JUnit.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 GoogleTest 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 for rootNode in rootElement.iterchildren(tag="testsuite"): # type: _Element
300 self._ConvertTestsuite(self, rootNode)
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=True),
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 (``<testsuites>``) 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 rootElement = Element("testsuites")
338 rootElement.attrib["name"] = self._name
339 if self._startTime is not None: 339 ↛ 341line 339 didn't jump to line 341 because the condition on line 339 was always true
340 rootElement.attrib["timestamp"] = f"{self._startTime.isoformat()}"
341 if self._duration is not None: 341 ↛ 343line 341 didn't jump to line 343 because the condition on line 341 was always true
342 rootElement.attrib["time"] = f"{self._duration.total_seconds():.6f}"
343 rootElement.attrib["tests"] = str(self._tests)
344 rootElement.attrib["failures"] = str(self._failed)
345 rootElement.attrib["errors"] = str(self._errored)
346 # rootElement.attrib["skipped"] = str(self._skipped)
347 rootElement.attrib["disabled"] = "0" # TODO: find a value
348 # if self._assertionCount is not None:
349 # rootElement.attrib["assertions"] = f"{self._assertionCount}"
351 self._xmlDocument = ElementTree(rootElement)
353 for testsuite in self._testsuites.values():
354 self._GenerateTestsuite(testsuite, rootElement)
356 def _GenerateTestsuite(self, testsuite: Testsuite, parentElement: _Element) -> None:
357 """
358 Generate the internal XML data structure for a test suite.
360 This method generates the XML element (``<testsuite>``) and recursively calls other generated methods.
362 :param testsuite: The test suite to convert to an XML data structures.
363 :param parentElement: The parent XML data structure element, this data structure part will be added to.
364 :return:
365 """
366 testsuiteElement = SubElement(parentElement, "testsuite")
367 testsuiteElement.attrib["name"] = testsuite._name
368 if testsuite._startTime is not None: 368 ↛ 370line 368 didn't jump to line 370 because the condition on line 368 was always true
369 testsuiteElement.attrib["timestamp"] = f"{testsuite._startTime.isoformat()}"
370 if testsuite._duration is not None: 370 ↛ 372line 370 didn't jump to line 372 because the condition on line 370 was always true
371 testsuiteElement.attrib["time"] = f"{testsuite._duration.total_seconds():.6f}"
372 testsuiteElement.attrib["tests"] = str(testsuite._tests)
373 testsuiteElement.attrib["failures"] = str(testsuite._failed)
374 testsuiteElement.attrib["errors"] = str(testsuite._errored)
375 testsuiteElement.attrib["skipped"] = str(testsuite._skipped)
376 testsuiteElement.attrib["disabled"] = "0" # TODO: find a value
377 # if testsuite._assertionCount is not None:
378 # testsuiteElement.attrib["assertions"] = f"{testsuite._assertionCount}"
379 # if testsuite._hostname is not None:
380 # testsuiteElement.attrib["hostname"] = testsuite._hostname
382 for testclass in testsuite._testclasses.values():
383 for tc in testclass._testcases.values():
384 self._GenerateTestcase(tc, testsuiteElement)
386 def _GenerateTestcase(self, testcase: Testcase, parentElement: _Element) -> None:
387 """
388 Generate the internal XML data structure for a test case.
390 This method generates the XML element (``<testcase>``) and recursively calls other generated methods.
392 :param testcase: The test case to convert to an XML data structures.
393 :param parentElement: The parent XML data structure element, this data structure part will be added to.
394 :return:
395 """
396 testcaseElement = SubElement(parentElement, "testcase")
397 if testcase.Classname is not None: 397 ↛ 399line 397 didn't jump to line 399 because the condition on line 397 was always true
398 testcaseElement.attrib["classname"] = testcase.Classname
399 testcaseElement.attrib["name"] = testcase._name
400 if testcase._duration is not None: 400 ↛ 402line 400 didn't jump to line 402 because the condition on line 400 was always true
401 testcaseElement.attrib["time"] = f"{testcase._duration.total_seconds():.6f}"
402 if testcase._assertionCount is not None: 402 ↛ 403line 402 didn't jump to line 403 because the condition on line 402 was never true
403 testcaseElement.attrib["assertions"] = f"{testcase._assertionCount}"
405 testcaseElement.attrib["timestamp"] = f"{testcase._parent._parent._startTime.isoformat()}" # TODO: find a value
406 testcaseElement.attrib["file"] = "" # TODO: find a value
407 testcaseElement.attrib["line"] = "0" # TODO: find a value
408 testcaseElement.attrib["status"] = "run" # TODO: find a value
409 testcaseElement.attrib["result"] = "completed" # TODO: find a value
411 if testcase._status is TestcaseStatus.Passed: 411 ↛ 413line 411 didn't jump to line 413 because the condition on line 411 was always true
412 pass
413 elif testcase._status is TestcaseStatus.Failed:
414 failureElement = SubElement(testcaseElement, "failure")
415 elif testcase._status is TestcaseStatus.Skipped:
416 skippedElement = SubElement(testcaseElement, "skipped")
417 else:
418 errorElement = SubElement(testcaseElement, "error")