Coverage for pyEDAA/Reports/Unittesting/JUnit/GoogleTestJUnit.py: 89%
146 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-14 02:26 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-14 02:26 +0000
1# ==================================================================================================================== #
2# _____ ____ _ _ ____ _ #
3# _ __ _ _| ____| _ \ / \ / \ | _ \ ___ _ __ ___ _ __| |_ ___ #
4# | '_ \| | | | _| | | | |/ _ \ / _ \ | |_) / _ \ '_ \ / _ \| '__| __/ __| #
5# | |_) | |_| | |___| |_| / ___ \ / ___ \ _| _ < __/ |_) | (_) | | | |_\__ \ #
6# | .__/ \__, |_____|____/_/ \_\/_/ \_(_)_| \_\___| .__/ \___/|_| \__|___/ #
7# |_| |___/ |_| #
8# ==================================================================================================================== #
9# Authors: #
10# Patrick Lehmann #
11# #
12# License: #
13# ==================================================================================================================== #
14# Copyright 2024-2026 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 __future__ import annotations
37from pathlib import Path
38from typing import Optional as Nullable, Tuple, TypeVar, Type, ClassVar
40from lxml.etree import ElementTree, Element, SubElement, tostring, _Element
41from pyTooling.Decorators import export, InheritDocString, DocStringMergeStrategy
42from pyTooling.Stopwatch import Stopwatch
44from pyEDAA.Reports.Unittesting import UnittestError, TestsuiteKind, TestsuiteSummary as ut_TestsuiteSummary, \
45 Testsuite as ut_Testsuite
46from pyEDAA.Reports.Unittesting.JUnit import Testcase as ju_Testcase, Testclass as ju_Testclass, \
47 Testsuite as ju_Testsuite, TestsuiteSummary as ju_TestsuiteSummary, \
48 Document as ju_Document
51TestsuiteType = TypeVar("TestsuiteType", bound="Testsuite")
52TestcaseAggregateReturnType = Tuple[int, int, int]
53TestsuiteAggregateReturnType = Tuple[int, int, int, int, int]
56@export
57@InheritDocString(ju_Testcase, DocStringMergeStrategy.BaseLast)
58class Testcase(ju_Testcase):
59 """
60 This is a derived implementation for the GoogleTest JUnit dialect.
61 """
64@export
65@InheritDocString(ju_Testclass, DocStringMergeStrategy.BaseLast)
66class Testclass(ju_Testclass):
67 """
68 This is a derived implementation for the GoogleTest JUnit dialect.
69 """
72@export
73@InheritDocString(ju_Testsuite, DocStringMergeStrategy.BaseLast)
74class Testsuite(ju_Testsuite):
75 """
76 This is a derived implementation for the GoogleTest JUnit dialect.
77 """
79 @classmethod
80 def FromTestsuite(cls, testsuite: ut_Testsuite) -> Testsuite:
81 """
82 Convert a test suite of the unified test entity data model to the JUnit specific data model's test suite object
83 adhering to the GoogleTest JUnit dialect.
85 :param testsuite: Test suite from unified data model.
86 :returns: Test suite from JUnit specific data model (GoogleTest JUnit dialect).
87 """
88 juTestsuite = cls(
89 testsuite._name,
90 hostname=testsuite._hostname,
91 startTime=testsuite._startTime,
92 duration=testsuite._totalDuration,
93 status= testsuite._status,
94 )
96 juTestsuite._tests = testsuite._tests
97 juTestsuite._skipped = testsuite._skipped
98 juTestsuite._errored = testsuite._errored
99 juTestsuite._failed = testsuite._failed
100 juTestsuite._passed = testsuite._passed
102 for tc in testsuite.IterateTestcases():
103 ts = tc._parent
104 if ts is None: 104 ↛ 105line 104 didn't jump to line 105 because the condition on line 104 was never true
105 raise UnittestError(f"Testcase '{tc._name}' is not part of a hierarchy.")
106 elif ts is testsuite:
107 juTestsuite.AddTestcase(Testcase.FromTestcase(tc))
108 continue
110 classname = ts._name
111 ts = ts._parent
112 while ts is not None and ts._kind > TestsuiteKind.Logical:
113 classname = f"{ts._name}.{classname}"
114 ts = ts._parent
116 if classname in juTestsuite._testclasses:
117 juClass = juTestsuite._testclasses[classname]
118 else:
119 juClass = Testclass(classname, parent=juTestsuite)
121 juClass.AddTestcase(Testcase.FromTestcase(tc))
123 return juTestsuite
126@export
127@InheritDocString(ju_TestsuiteSummary, DocStringMergeStrategy.BaseLast)
128class TestsuiteSummary(ju_TestsuiteSummary):
129 """
130 This is a derived implementation for the GoogleTest JUnit dialect.
131 """
133 @classmethod
134 def FromTestsuiteSummary(cls, testsuiteSummary: ut_TestsuiteSummary) -> TestsuiteSummary:
135 """
136 Convert a test suite summary of the unified test entity data model to the JUnit specific data model's test suite
137 summary object adhering to the GoogleTest JUnit dialect.
139 :param testsuiteSummary: Test suite summary from unified data model.
140 :returns: Test suite summary from JUnit specific data model (GoogleTest JUnit dialect).
141 """
142 return cls(
143 testsuiteSummary._name,
144 startTime=testsuiteSummary._startTime,
145 duration=testsuiteSummary._totalDuration,
146 status=testsuiteSummary._status,
147 testsuites=(Testsuite.FromTestsuite(testsuite) for testsuite in testsuiteSummary._testsuites.values())
148 )
151@export
152class Document(ju_Document):
153 """
154 A document reader and writer for the GoogelTest JUnit XML file format.
156 This class reads, validates and transforms an XML file in the GoogelTest JUnit format into a JUnit data model. It can
157 then be converted into a unified test entity data model.
159 In reverse, a JUnit data model instance with the specific GoogelTest JUnit file format can be created from a unified
160 test entity data model. This data model can be written as XML into a file.
161 """
163 _DIALECT: ClassVar[str] = "GoogleTest + JUnit"
164 _TESTCASE: ClassVar[Type[Testcase]] = Testcase
165 _TESTCLASS: ClassVar[Type[Testclass]] = Testclass
166 _TESTSUITE: ClassVar[Type[Testsuite]] = Testsuite
168 def Analyze(self) -> None:
169 """
170 Analyze the XML file, parse the content into an XML data structure and validate the data structure using an XML
171 schema.
173 .. hint::
175 The time spend for analysis will be made available via property :data:`AnalysisDuration`.
177 The used XML schema definition is specific to the GoogleTest JUnit dialect.
178 """
179 xmlSchemaFile = "GoogleTest-JUnit.xsd"
180 self._Analyze(xmlSchemaFile)
182 def Write(self, path: Nullable[Path] = None, overwrite: bool = False, regenerate: bool = False) -> None:
183 """
184 Write the data model as XML into a file adhering to the GoogleTest dialect.
186 :param path: Optional, path to the XML file, if internal path shouldn't be used.
187 :param overwrite: Optional, if true, overwrite an existing file.
188 :param regenerate: Optional, if true, regenerate the XML structure from data model.
189 :raises UnittestError: If the file cannot be overwritten.
190 :raises UnittestError: If the internal XML data structure wasn't generated.
191 :raises UnittestError: If the file cannot be opened or written.
192 """
193 if path is None:
194 path = self._path
196 if not overwrite and path.exists():
197 raise UnittestError(f"JUnit XML file '{path}' can not be overwritten.") \
198 from FileExistsError(f"File '{path}' already exists.")
200 if regenerate: 200 ↛ 203line 200 didn't jump to line 203 because the condition on line 200 was always true
201 self.Generate(overwrite=True)
203 if self._xmlDocument is None: 203 ↛ 204line 203 didn't jump to line 204 because the condition on line 203 was never true
204 ex = UnittestError("Internal XML document tree is empty and needs to be generated before write is possible.")
205 ex.add_note("Call 'JUnitDocument.Generate()' or 'JUnitDocument.Write(..., regenerate=True)'.")
206 raise ex
208 content = tostring(self._xmlDocument, encoding="utf-8", xml_declaration=True, pretty_print=True)
209 try:
210 with path.open("wb") as file:
211 file.write(content)
212 except OSError as ex:
213 raise UnittestError(f"JUnit XML file '{path}' can not be written.") from ex
215 def Convert(self) -> None:
216 """
217 Convert the parsed and validated XML data structure into a JUnit test entity hierarchy.
219 This method converts the root element.
221 .. hint::
223 The time spend for model conversion will be made available via property :data:`ModelConversionDuration`.
225 :raises UnittestError: If XML was not read and parsed before.
226 """
227 if self._xmlDocument is None: 227 ↛ 228line 227 didn't jump to line 228 because the condition on line 227 was never true
228 ex = UnittestError(f"JUnit XML file '{self._path}' needs to be read and analyzed by an XML parser.")
229 ex.add_note("Call 'JUnitDocument.Analyze()' or create the document using 'JUnitDocument(path, parse=True)'.")
230 raise ex
232 with Stopwatch() as sw:
233 rootElement: _Element = self._xmlDocument.getroot()
235 self._name = self._ConvertName(rootElement, optional=True)
236 self._startTime =self._ConvertTimestamp(rootElement, optional=True)
237 self._duration = self._ConvertTime(rootElement, optional=True)
239 # tests = rootElement.getAttribute("tests")
240 # skipped = rootElement.getAttribute("skipped")
241 # errors = rootElement.getAttribute("errors")
242 # failures = rootElement.getAttribute("failures")
243 # assertions = rootElement.getAttribute("assertions")
245 for rootNode in rootElement.iterchildren(tag="testsuite"): # type: _Element
246 self._ConvertTestsuite(self, rootNode)
248 self.Aggregate()
250 self._modelConversion = sw.Duration
252 def _ConvertTestsuite(self, parent: TestsuiteSummary, testsuitesNode: _Element) -> None:
253 """
254 Convert the XML data structure of a ``<testsuite>`` to a test suite.
256 This method uses private helper methods provided by the base-class.
258 :param parent: The test suite summary as a parent element in the test entity hierarchy.
259 :param testsuitesNode: The current XML element node representing a test suite.
260 """
261 newTestsuite = Testsuite(
262 self._ConvertName(testsuitesNode, optional=False),
263 self._ConvertHostname(testsuitesNode, optional=True),
264 self._ConvertTimestamp(testsuitesNode, optional=False),
265 self._ConvertTime(testsuitesNode, optional=False),
266 parent=parent
267 )
269 self._ConvertTestsuiteChildren(testsuitesNode, newTestsuite)
271 def Generate(self, overwrite: bool = False) -> None:
272 """
273 Generate the internal XML data structure from test suites and test cases.
275 This method generates the XML root element (``<testsuites>``) and recursively calls other generated methods.
277 :param overwrite: Optional, overwrite the internal XML data structure.
278 :raises UnittestError: If overwrite is false and the internal XML data structure is not empty.
279 """
280 if not overwrite and self._xmlDocument is not None: 280 ↛ 281line 280 didn't jump to line 281 because the condition on line 280 was never true
281 raise UnittestError("Internal XML document is populated with data.")
283 rootElement = Element("testsuites")
284 rootElement.attrib["name"] = self._name
285 if self._startTime is None:
286 raise UnittestError(
287 f"The {self._DIALECT} format requires a timestamp on <testsuites>, but the report has none."
288 )
289 rootElement.attrib["timestamp"] = f"{self._startTime.isoformat()}"
290 if self._duration is not None: 290 ↛ 292line 290 didn't jump to line 292 because the condition on line 290 was always true
291 rootElement.attrib["time"] = f"{self._duration.total_seconds():.6f}"
292 rootElement.attrib["tests"] = str(self._tests)
293 rootElement.attrib["failures"] = str(self._failed)
294 rootElement.attrib["errors"] = str(self._errored)
295 # rootElement.attrib["skipped"] = str(self._skipped)
296 rootElement.attrib["disabled"] = "0" # TODO: find a value
297 # if self._assertionCount is not None:
298 # rootElement.attrib["assertions"] = f"{self._assertionCount}"
300 self._xmlDocument = ElementTree(rootElement)
302 for testsuite in self._testsuites.values():
303 self._GenerateTestsuite(testsuite, rootElement)
305 def _GenerateTestsuite(self, testsuite: Testsuite, parentElement: _Element) -> None:
306 """
307 Generate the internal XML data structure for a test suite.
309 This method generates the XML element (``<testsuite>``) and recursively calls other generated methods.
311 :param testsuite: The test suite to convert to an XML data structures.
312 :param parentElement: The parent XML data structure element, this data structure part will be added to.
313 """
314 testsuiteElement = SubElement(parentElement, "testsuite")
315 testsuiteElement.attrib["name"] = testsuite._name
316 if testsuite._startTime is None:
317 raise UnittestError(
318 f"The {self._DIALECT} format requires a timestamp on <testsuite>, but the report has none."
319 )
320 testsuiteElement.attrib["timestamp"] = f"{testsuite._startTime.isoformat()}"
321 if testsuite._duration is not None: 321 ↛ 323line 321 didn't jump to line 323 because the condition on line 321 was always true
322 testsuiteElement.attrib["time"] = f"{testsuite._duration.total_seconds():.6f}"
323 testsuiteElement.attrib["tests"] = str(testsuite._tests)
324 testsuiteElement.attrib["failures"] = str(testsuite._failed)
325 testsuiteElement.attrib["errors"] = str(testsuite._errored)
326 testsuiteElement.attrib["skipped"] = str(testsuite._skipped)
327 testsuiteElement.attrib["disabled"] = "0" # TODO: find a value
328 # if testsuite._assertionCount is not None:
329 # testsuiteElement.attrib["assertions"] = f"{testsuite._assertionCount}"
330 # if testsuite._hostname is not None:
331 # testsuiteElement.attrib["hostname"] = testsuite._hostname
333 for tc in testsuite._testcases.values():
334 self._GenerateTestcase(tc, testsuiteElement)
336 for testclass in testsuite._testclasses.values():
337 for tc in testclass._testcases.values():
338 self._GenerateTestcase(tc, testsuiteElement)
340 def _GenerateTestcase(self, testcase: Testcase, parentElement: _Element) -> None:
341 """
342 Generate the internal XML data structure for a test case.
344 This method generates the XML element (``<testcase>``) and recursively calls other generated methods.
346 :param testcase: The test case to convert to an XML data structures.
347 :param parentElement: The parent XML data structure element, this data structure part will be added to.
348 """
349 testcaseElement = SubElement(parentElement, "testcase")
350 if testcase.Classname is not None: 350 ↛ 352line 350 didn't jump to line 352 because the condition on line 350 was always true
351 testcaseElement.attrib["classname"] = testcase.Classname
352 testcaseElement.attrib["name"] = testcase._name
353 if testcase._duration is not None: 353 ↛ 355line 353 didn't jump to line 355 because the condition on line 353 was always true
354 testcaseElement.attrib["time"] = f"{testcase._duration.total_seconds():.6f}"
355 if testcase._assertionCount is not None: 355 ↛ 356line 355 didn't jump to line 356 because the condition on line 355 was never true
356 testcaseElement.attrib["assertions"] = f"{testcase._assertionCount}"
358 if testcase._parent._parent._startTime is None: 358 ↛ 359line 358 didn't jump to line 359 because the condition on line 358 was never true
359 raise UnittestError(
360 f"The {self._DIALECT} format requires a timestamp on <testcase>, but the report has none."
361 )
362 testcaseElement.attrib["timestamp"] = f"{testcase._parent._parent._startTime.isoformat()}"
363 testcaseElement.attrib["file"] = "" # TODO: find a value
364 testcaseElement.attrib["line"] = "0" # TODO: find a value
365 testcaseElement.attrib["status"] = "run" # TODO: find a value
366 testcaseElement.attrib["result"] = "completed" # TODO: find a value
368 self._GenerateTestcaseChildren(testcase, testcaseElement)