Coverage for pyEDAA/Reports/Unittesting/JUnit/GoogleTestJUnit.py: 63%
158 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-27 22:23 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-27 22:23 +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 @classmethod
78 def FromTestsuite(cls, testsuite: ut_Testsuite) -> "Testsuite":
79 """
80 Convert a test suite of the unified test entity data model to the JUnit specific data model's test suite object
81 adhering to the GoogleTest JUnit dialect.
83 :param testsuite: Test suite from unified data model.
84 :return: Test suite from JUnit specific data model (GoogleTest JUnit dialect).
85 """
86 juTestsuite = cls(
87 testsuite._name,
88 startTime=testsuite._startTime,
89 duration=testsuite._totalDuration,
90 status= testsuite._status,
91 )
93 juTestsuite._tests = testsuite._tests
94 juTestsuite._skipped = testsuite._skipped
95 juTestsuite._errored = testsuite._errored
96 juTestsuite._failed = testsuite._failed
97 juTestsuite._passed = testsuite._passed
99 for tc in testsuite.IterateTestcases():
100 ts = tc._parent
101 if ts is None:
102 raise UnittestException(f"Testcase '{tc._name}' is not part of a hierarchy.")
104 classname = ts._name
105 ts = ts._parent
106 while ts is not None and ts._kind > TestsuiteKind.Logical:
107 classname = f"{ts._name}.{classname}"
108 ts = ts._parent
110 if classname in juTestsuite._testclasses:
111 juClass = juTestsuite._testclasses[classname]
112 else:
113 juClass = Testclass(classname, parent=juTestsuite)
115 juClass.AddTestcase(Testcase.FromTestcase(tc))
117 return juTestsuite
120@export
121@InheritDocString(ju_TestsuiteSummary, merge=True)
122class TestsuiteSummary(ju_TestsuiteSummary):
123 """
124 This is a derived implementation for the GoogleTest JUnit dialect.
125 """
127 @classmethod
128 def FromTestsuiteSummary(cls, testsuiteSummary: ut_TestsuiteSummary) -> "TestsuiteSummary":
129 """
130 Convert a test suite summary of the unified test entity data model to the JUnit specific data model's test suite
131 summary object adhering to the GoogleTest JUnit dialect.
133 :param testsuiteSummary: Test suite summary from unified data model.
134 :return: Test suite summary from JUnit specific data model (GoogleTest JUnit dialect).
135 """
136 return cls(
137 testsuiteSummary._name,
138 startTime=testsuiteSummary._startTime,
139 duration=testsuiteSummary._totalDuration,
140 status=testsuiteSummary._status,
141 testsuites=(ut_Testsuite.FromTestsuite(testsuite) for testsuite in testsuiteSummary._testsuites.values())
142 )
145@export
146class Document(ju_Document):
147 """
148 A document reader and writer for the GoogelTest JUnit XML file format.
150 This class reads, validates and transforms an XML file in the GoogelTest JUnit format into a JUnit data model. It can
151 then be converted into a unified test entity data model.
153 In reverse, a JUnit data model instance with the specific GoogelTest JUnit file format can be created from a unified
154 test entity data model. This data model can be written as XML into a file.
155 """
157 _TESTCASE: ClassVar[Type[Testcase]] = Testcase
158 _TESTCLASS: ClassVar[Type[Testclass]] = Testclass
159 _TESTSUITE: ClassVar[Type[Testsuite]] = Testsuite
161 @classmethod
162 def FromTestsuiteSummary(cls, xmlReportFile: Path, testsuiteSummary: ut_TestsuiteSummary):
163 doc = cls(xmlReportFile)
164 doc._name = testsuiteSummary._name
165 doc._startTime = testsuiteSummary._startTime
166 doc._duration = testsuiteSummary._totalDuration
167 doc._status = testsuiteSummary._status
168 doc._tests = testsuiteSummary._tests
169 doc._skipped = testsuiteSummary._skipped
170 doc._errored = testsuiteSummary._errored
171 doc._failed = testsuiteSummary._failed
172 doc._passed = testsuiteSummary._passed
174 doc.AddTestsuites(Testsuite.FromTestsuite(testsuite) for testsuite in testsuiteSummary._testsuites.values())
176 return doc
178 def Analyze(self) -> None:
179 """
180 Analyze the XML file, parse the content into an XML data structure and validate the data structure using an XML
181 schema.
183 .. hint::
185 The time spend for analysis will be made available via property :data:`AnalysisDuration`.
187 The used XML schema definition is specific to the GoogleTest JUnit dialect.
188 """
189 xmlSchemaFile = "GoogleTest-JUnit.xsd"
190 self._Analyze(xmlSchemaFile)
192 def Write(self, path: Nullable[Path] = None, overwrite: bool = False, regenerate: bool = False) -> None:
193 """
194 Write the data model as XML into a file adhering to the GoogleTest dialect.
196 :param path: Optional path to the XMl file, if internal path shouldn't be used.
197 :param overwrite: If true, overwrite an existing file.
198 :param regenerate: If true, regenerate the XML structure from data model.
199 :raises UnittestException: If the file cannot be overwritten.
200 :raises UnittestException: If the internal XML data structure wasn't generated.
201 :raises UnittestException: If the file cannot be opened or written.
202 """
203 if path is None: 203 ↛ 204line 203 didn't jump to line 204 because the condition on line 203 was never true
204 path = self._path
206 if not overwrite and path.exists(): 206 ↛ 207line 206 didn't jump to line 207 because the condition on line 206 was never true
207 raise UnittestException(f"JUnit XML file '{path}' can not be overwritten.") \
208 from FileExistsError(f"File '{path}' already exists.")
210 if regenerate: 210 ↛ 213line 210 didn't jump to line 213 because the condition on line 210 was always true
211 self.Generate(overwrite=True)
213 if self._xmlDocument is None: 213 ↛ 214line 213 didn't jump to line 214 because the condition on line 213 was never true
214 ex = UnittestException(f"Internal XML document tree is empty and needs to be generated before write is possible.")
215 ex.add_note(f"Call 'JUnitDocument.Generate()' or 'JUnitDocument.Write(..., regenerate=True)'.")
216 raise ex
218 try:
219 with path.open("wb") as file:
220 file.write(tostring(self._xmlDocument, encoding="utf-8", xml_declaration=True, pretty_print=True))
221 except Exception as ex:
222 raise UnittestException(f"JUnit XML file '{path}' can not be written.") from ex
224 def Convert(self) -> None:
225 """
226 Convert the parsed and validated XML data structure into a JUnit test entity hierarchy.
228 This method converts the root element.
230 .. hint::
232 The time spend for model conversion will be made available via property :data:`ModelConversionDuration`.
234 :raises UnittestException: If XML was not read and parsed before.
235 """
236 if self._xmlDocument is None: 236 ↛ 237line 236 didn't jump to line 237 because the condition on line 236 was never true
237 ex = UnittestException(f"JUnit XML file '{self._path}' needs to be read and analyzed by an XML parser.")
238 ex.add_note(f"Call 'JUnitDocument.Analyze()' or create the document using 'JUnitDocument(path, parse=True)'.")
239 raise ex
241 startConversion = perf_counter_ns()
242 rootElement: _Element = self._xmlDocument.getroot()
244 self._name = self._ConvertName(rootElement, optional=True)
245 self._startTime =self._ConvertTimestamp(rootElement, optional=True)
246 self._duration = self._ConvertTime(rootElement, optional=True)
248 # tests = rootElement.getAttribute("tests")
249 # skipped = rootElement.getAttribute("skipped")
250 # errors = rootElement.getAttribute("errors")
251 # failures = rootElement.getAttribute("failures")
252 # assertions = rootElement.getAttribute("assertions")
254 for rootNode in rootElement.iterchildren(tag="testsuite"): # type: _Element
255 self._ConvertTestsuite(self, rootNode)
257 self.Aggregate()
258 endConversation = perf_counter_ns()
259 self._modelConversion = (endConversation - startConversion) / 1e9
261 def _ConvertTestsuite(self, parent: TestsuiteSummary, testsuitesNode: _Element) -> None:
262 """
263 Convert the XML data structure of a ``<testsuite>`` to a test suite.
265 This method uses private helper methods provided by the base-class.
267 :param parent: The test suite summary as a parent element in the test entity hierarchy.
268 :param testsuitesNode: The current XML element node representing a test suite.
269 """
270 newTestsuite = Testsuite(
271 self._ConvertName(testsuitesNode, optional=False),
272 self._ConvertHostname(testsuitesNode, optional=True),
273 self._ConvertTimestamp(testsuitesNode, optional=False),
274 self._ConvertTime(testsuitesNode, optional=False),
275 parent=parent
276 )
278 self._ConvertTestsuiteChildren(testsuitesNode, newTestsuite)
280 def Generate(self, overwrite: bool = False) -> None:
281 """
282 Generate the internal XML data structure from test suites and test cases.
284 This method generates the XML root element (``<testsuites>``) and recursively calls other generated methods.
286 :param overwrite: Overwrite the internal XML data structure.
287 :raises UnittestException: If overwrite is false and the internal XML data structure is not empty.
288 """
289 if not overwrite and self._xmlDocument is not None: 289 ↛ 290line 289 didn't jump to line 290 because the condition on line 289 was never true
290 raise UnittestException(f"Internal XML document is populated with data.")
292 rootElement = Element("testsuites")
293 rootElement.attrib["name"] = self._name
294 if self._startTime is not None: 294 ↛ 296line 294 didn't jump to line 296 because the condition on line 294 was always true
295 rootElement.attrib["timestamp"] = f"{self._startTime.isoformat()}"
296 if self._duration is not None: 296 ↛ 298line 296 didn't jump to line 298 because the condition on line 296 was always true
297 rootElement.attrib["time"] = f"{self._duration.total_seconds():.6f}"
298 rootElement.attrib["tests"] = str(self._tests)
299 rootElement.attrib["failures"] = str(self._failed)
300 rootElement.attrib["errors"] = str(self._errored)
301 # rootElement.attrib["skipped"] = str(self._skipped)
302 rootElement.attrib["disabled"] = "0" # TODO: find a value
303 # if self._assertionCount is not None:
304 # rootElement.attrib["assertions"] = f"{self._assertionCount}"
306 self._xmlDocument = ElementTree(rootElement)
308 for testsuite in self._testsuites.values():
309 self._GenerateTestsuite(testsuite, rootElement)
311 def _GenerateTestsuite(self, testsuite: Testsuite, parentElement: _Element) -> None:
312 """
313 Generate the internal XML data structure for a test suite.
315 This method generates the XML element (``<testsuite>``) and recursively calls other generated methods.
317 :param testsuite: The test suite to convert to an XML data structures.
318 :param parentElement: The parent XML data structure element, this data structure part will be added to.
319 :return:
320 """
321 testsuiteElement = SubElement(parentElement, "testsuite")
322 testsuiteElement.attrib["name"] = testsuite._name
323 if testsuite._startTime is not None: 323 ↛ 325line 323 didn't jump to line 325 because the condition on line 323 was always true
324 testsuiteElement.attrib["timestamp"] = f"{testsuite._startTime.isoformat()}"
325 if testsuite._duration is not None: 325 ↛ 327line 325 didn't jump to line 327 because the condition on line 325 was always true
326 testsuiteElement.attrib["time"] = f"{testsuite._duration.total_seconds():.6f}"
327 testsuiteElement.attrib["tests"] = str(testsuite._tests)
328 testsuiteElement.attrib["failures"] = str(testsuite._failed)
329 testsuiteElement.attrib["errors"] = str(testsuite._errored)
330 testsuiteElement.attrib["skipped"] = str(testsuite._skipped)
331 testsuiteElement.attrib["disabled"] = "0" # TODO: find a value
332 # if testsuite._assertionCount is not None:
333 # testsuiteElement.attrib["assertions"] = f"{testsuite._assertionCount}"
334 # if testsuite._hostname is not None:
335 # testsuiteElement.attrib["hostname"] = testsuite._hostname
337 for testclass in testsuite._testclasses.values():
338 for tc in testclass._testcases.values():
339 self._GenerateTestcase(tc, testsuiteElement)
341 def _GenerateTestcase(self, testcase: Testcase, parentElement: _Element) -> None:
342 """
343 Generate the internal XML data structure for a test case.
345 This method generates the XML element (``<testcase>``) and recursively calls other generated methods.
347 :param testcase: The test case to convert to an XML data structures.
348 :param parentElement: The parent XML data structure element, this data structure part will be added to.
349 :return:
350 """
351 testcaseElement = SubElement(parentElement, "testcase")
352 if testcase.Classname is not None: 352 ↛ 354line 352 didn't jump to line 354 because the condition on line 352 was always true
353 testcaseElement.attrib["classname"] = testcase.Classname
354 testcaseElement.attrib["name"] = testcase._name
355 if testcase._duration is not None: 355 ↛ 357line 355 didn't jump to line 357 because the condition on line 355 was always true
356 testcaseElement.attrib["time"] = f"{testcase._duration.total_seconds():.6f}"
357 if testcase._assertionCount is not None: 357 ↛ 358line 357 didn't jump to line 358 because the condition on line 357 was never true
358 testcaseElement.attrib["assertions"] = f"{testcase._assertionCount}"
360 testcaseElement.attrib["timestamp"] = f"{testcase._parent._parent._startTime.isoformat()}" # TODO: find a value
361 testcaseElement.attrib["file"] = "" # TODO: find a value
362 testcaseElement.attrib["line"] = "0" # TODO: find a value
363 testcaseElement.attrib["status"] = "run" # TODO: find a value
364 testcaseElement.attrib["result"] = "completed" # TODO: find a value
366 if testcase._status is TestcaseStatus.Passed: 366 ↛ 368line 366 didn't jump to line 368 because the condition on line 366 was always true
367 pass
368 elif testcase._status is TestcaseStatus.Failed:
369 failureElement = SubElement(testcaseElement, "failure")
370 elif testcase._status is TestcaseStatus.Skipped:
371 skippedElement = SubElement(testcaseElement, "skipped")
372 else:
373 errorElement = SubElement(testcaseElement, "error")