Coverage for pyEDAA/Reports/Unittesting/JUnit/CTestJUnit.py: 58%

147 statements  

« 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 

38 

39from lxml.etree import ElementTree, Element, SubElement, tostring, _Element 

40from pyTooling.Common import firstValue 

41from pyTooling.Decorators import export, InheritDocString 

42 

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 

48 

49 

50TestsuiteType = TypeVar("TestsuiteType", bound="Testsuite") 

51TestcaseAggregateReturnType = Tuple[int, int, int] 

52TestsuiteAggregateReturnType = Tuple[int, int, int, int, int] 

53 

54 

55@export 

56@InheritDocString(ju_Testcase, merge=True) 

57class Testcase(ju_Testcase): 

58 """ 

59 This is a derived implementation for the CTest JUnit dialect. 

60 """ 

61 

62 

63@export 

64@InheritDocString(ju_Testclass, merge=True) 

65class Testclass(ju_Testclass): 

66 """ 

67 This is a derived implementation for the CTest JUnit dialect. 

68 """ 

69 

70 

71@export 

72@InheritDocString(ju_Testsuite, merge=True) 

73class Testsuite(ju_Testsuite): 

74 """ 

75 This is a derived implementation for the CTest JUnit dialect. 

76 """ 

77 

78 @classmethod 

79 def FromTestsuite(cls, testsuite: ut_Testsuite) -> "Testsuite": 

80 """ 

81 Convert a test suite of the unified test entity data model to the JUnit specific data model's test suite object 

82 adhering to the CTest JUnit dialect. 

83 

84 :param testsuite: Test suite from unified data model. 

85 :return: Test suite from JUnit specific data model (CTest JUnit dialect). 

86 """ 

87 juTestsuite = cls( 

88 testsuite._name, 

89 startTime=testsuite._startTime, 

90 duration=testsuite._totalDuration, 

91 status= testsuite._status, 

92 ) 

93 

94 juTestsuite._tests = testsuite._tests 

95 juTestsuite._skipped = testsuite._skipped 

96 juTestsuite._errored = testsuite._errored 

97 juTestsuite._failed = testsuite._failed 

98 juTestsuite._passed = testsuite._passed 

99 

100 for tc in testsuite.IterateTestcases(): 

101 ts = tc._parent 

102 if ts is None: 

103 raise UnittestException(f"Testcase '{tc._name}' is not part of a hierarchy.") 

104 

105 classname = ts._name 

106 ts = ts._parent 

107 while ts is not None and ts._kind > TestsuiteKind.Logical: 

108 classname = f"{ts._name}.{classname}" 

109 ts = ts._parent 

110 

111 if classname in juTestsuite._testclasses: 

112 juClass = juTestsuite._testclasses[classname] 

113 else: 

114 juClass = Testclass(classname, parent=juTestsuite) 

115 

116 juClass.AddTestcase(Testcase.FromTestcase(tc)) 

117 

118 return juTestsuite 

119 

120 

121@export 

122@InheritDocString(ju_TestsuiteSummary, merge=True) 

123class TestsuiteSummary(ju_TestsuiteSummary): 

124 """ 

125 This is a derived implementation for the CTest JUnit dialect. 

126 """ 

127 

128 @classmethod 

129 def FromTestsuiteSummary(cls, testsuiteSummary: ut_TestsuiteSummary) -> "TestsuiteSummary": 

130 """ 

131 Convert a test suite summary of the unified test entity data model to the JUnit specific data model's test suite 

132 summary object adhering to the CTest JUnit dialect. 

133 

134 :param testsuiteSummary: Test suite summary from unified data model. 

135 :return: Test suite summary from JUnit specific data model (CTest JUnit dialect). 

136 """ 

137 return cls( 

138 testsuiteSummary._name, 

139 startTime=testsuiteSummary._startTime, 

140 duration=testsuiteSummary._totalDuration, 

141 status=testsuiteSummary._status, 

142 testsuites=(ut_Testsuite.FromTestsuite(testsuite) for testsuite in testsuiteSummary._testsuites.values()) 

143 ) 

144 

145 

146@export 

147class Document(ju_Document): 

148 """ 

149 A document reader and writer for the CTest JUnit XML file format. 

150 

151 This class reads, validates and transforms an XML file in the CTest JUnit format into a JUnit data model. It can then 

152 be converted into a unified test entity data model. 

153 

154 In reverse, a JUnit data model instance with the specific CTest JUnit file format can be created from a unified test 

155 entity data model. This data model can be written as XML into a file. 

156 """ 

157 

158 _TESTCASE: ClassVar[Type[Testcase]] = Testcase 

159 _TESTCLASS: ClassVar[Type[Testclass]] = Testclass 

160 _TESTSUITE: ClassVar[Type[Testsuite]] = Testsuite 

161 

162 @classmethod 

163 def FromTestsuiteSummary(cls, xmlReportFile: Path, testsuiteSummary: ut_TestsuiteSummary): 

164 doc = cls(xmlReportFile) 

165 doc._name = testsuiteSummary._name 

166 doc._startTime = testsuiteSummary._startTime 

167 doc._duration = testsuiteSummary._totalDuration 

168 doc._status = testsuiteSummary._status 

169 doc._tests = testsuiteSummary._tests 

170 doc._skipped = testsuiteSummary._skipped 

171 doc._errored = testsuiteSummary._errored 

172 doc._failed = testsuiteSummary._failed 

173 doc._passed = testsuiteSummary._passed 

174 

175 doc.AddTestsuites(Testsuite.FromTestsuite(testsuite) for testsuite in testsuiteSummary._testsuites.values()) 

176 

177 return doc 

178 

179 def Analyze(self) -> None: 

180 """ 

181 Analyze the XML file, parse the content into an XML data structure and validate the data structure using an XML 

182 schema. 

183 

184 .. hint:: 

185 

186 The time spend for analysis will be made available via property :data:`AnalysisDuration`. 

187 

188 The used XML schema definition is specific to the CTest JUnit dialect. 

189 """ 

190 xmlSchemaFile = "CTest-JUnit.xsd" 

191 self._Analyze(xmlSchemaFile) 

192 

193 def Write(self, path: Nullable[Path] = None, overwrite: bool = False, regenerate: bool = False) -> None: 

194 """ 

195 Write the data model as XML into a file adhering to the CTest dialect. 

196 

197 :param path: Optional path to the XMl file, if internal path shouldn't be used. 

198 :param overwrite: If true, overwrite an existing file. 

199 :param regenerate: If true, regenerate the XML structure from data model. 

200 :raises UnittestException: If the file cannot be overwritten. 

201 :raises UnittestException: If the internal XML data structure wasn't generated. 

202 :raises UnittestException: If the file cannot be opened or written. 

203 """ 

204 if path is None: 204 ↛ 205line 204 didn't jump to line 205 because the condition on line 204 was never true

205 path = self._path 

206 

207 if not overwrite and path.exists(): 207 ↛ 208line 207 didn't jump to line 208 because the condition on line 207 was never true

208 raise UnittestException(f"JUnit XML file '{path}' can not be overwritten.") \ 

209 from FileExistsError(f"File '{path}' already exists.") 

210 

211 if regenerate: 211 ↛ 214line 211 didn't jump to line 214 because the condition on line 211 was always true

212 self.Generate(overwrite=True) 

213 

214 if self._xmlDocument is None: 214 ↛ 215line 214 didn't jump to line 215 because the condition on line 214 was never true

215 ex = UnittestException(f"Internal XML document tree is empty and needs to be generated before write is possible.") 

216 ex.add_note(f"Call 'JUnitDocument.Generate()' or 'JUnitDocument.Write(..., regenerate=True)'.") 

217 raise ex 

218 

219 try: 

220 with path.open("wb") as file: 

221 file.write(tostring(self._xmlDocument, encoding="utf-8", xml_declaration=True, pretty_print=True)) 

222 except Exception as ex: 

223 raise UnittestException(f"JUnit XML file '{path}' can not be written.") from ex 

224 

225 def Convert(self) -> None: 

226 """ 

227 Convert the parsed and validated XML data structure into a JUnit test entity hierarchy. 

228 

229 This method converts the root element. 

230 

231 .. hint:: 

232 

233 The time spend for model conversion will be made available via property :data:`ModelConversionDuration`. 

234 

235 :raises UnittestException: If XML was not read and parsed before. 

236 """ 

237 if self._xmlDocument is None: 237 ↛ 238line 237 didn't jump to line 238 because the condition on line 237 was never true

238 ex = UnittestException(f"JUnit XML file '{self._path}' needs to be read and analyzed by an XML parser.") 

239 ex.add_note(f"Call 'JUnitDocument.Analyze()' or create the document using 'JUnitDocument(path, parse=True)'.") 

240 raise ex 

241 

242 startConversion = perf_counter_ns() 

243 rootElement: _Element = self._xmlDocument.getroot() 

244 

245 self._name = self._ConvertName(rootElement, optional=True) 

246 self._startTime =self._ConvertTimestamp(rootElement, optional=True) 

247 self._duration = self._ConvertTime(rootElement, optional=True) 

248 

249 # tests = rootElement.getAttribute("tests") 

250 # skipped = rootElement.getAttribute("skipped") 

251 # errors = rootElement.getAttribute("errors") 

252 # failures = rootElement.getAttribute("failures") 

253 # assertions = rootElement.getAttribute("assertions") 

254 

255 ts = Testsuite(self._name, startTime=self._startTime, duration=self._duration, parent=self) 

256 self._ConvertTestsuiteChildren(rootElement, ts) 

257 

258 self.Aggregate() 

259 endConversation = perf_counter_ns() 

260 self._modelConversion = (endConversation - startConversion) / 1e9 

261 

262 def _ConvertTestsuite(self, parent: TestsuiteSummary, testsuitesNode: _Element) -> None: 

263 """ 

264 Convert the XML data structure of a ``<testsuite>`` to a test suite. 

265 

266 This method uses private helper methods provided by the base-class. 

267 

268 :param parent: The test suite summary as a parent element in the test entity hierarchy. 

269 :param testsuitesNode: The current XML element node representing a test suite. 

270 """ 

271 newTestsuite = Testsuite( 

272 self._ConvertName(testsuitesNode, optional=False), 

273 self._ConvertHostname(testsuitesNode, optional=False), 

274 self._ConvertTimestamp(testsuitesNode, optional=False), 

275 self._ConvertTime(testsuitesNode, optional=False), 

276 parent=parent 

277 ) 

278 

279 self._ConvertTestsuiteChildren(testsuitesNode, newTestsuite) 

280 

281 def Generate(self, overwrite: bool = False) -> None: 

282 """ 

283 Generate the internal XML data structure from test suites and test cases. 

284 

285 This method generates the XML root element (``<testsuite>``) and recursively calls other generated methods. 

286 

287 :param overwrite: Overwrite the internal XML data structure. 

288 :raises UnittestException: If overwrite is false and the internal XML data structure is not empty. 

289 """ 

290 if not overwrite and self._xmlDocument is not None: 290 ↛ 291line 290 didn't jump to line 291 because the condition on line 290 was never true

291 raise UnittestException(f"Internal XML document is populated with data.") 

292 

293 if self.TestsuiteCount != 1: 293 ↛ 294line 293 didn't jump to line 294 because the condition on line 293 was never true

294 ex = UnittestException(f"The CTest JUnit format requires exactly one test suite.") 

295 ex.add_note(f"Found {self.TestsuiteCount} test suites.") 

296 raise ex 

297 

298 testsuite = firstValue(self._testsuites) 

299 

300 rootElement = Element("testsuite") 

301 rootElement.attrib["name"] = self._name 

302 if self._startTime is not None: 302 ↛ 304line 302 didn't jump to line 304 because the condition on line 302 was always true

303 rootElement.attrib["timestamp"] = f"{self._startTime.isoformat()}" 

304 if self._duration is not None: 304 ↛ 306line 304 didn't jump to line 306 because the condition on line 304 was always true

305 rootElement.attrib["time"] = f"{self._duration.total_seconds():.6f}" 

306 rootElement.attrib["tests"] = str(self._tests) 

307 rootElement.attrib["failures"] = str(self._failed) 

308 # rootElement.attrib["errors"] = str(self._errored) 

309 rootElement.attrib["skipped"] = str(self._skipped) 

310 rootElement.attrib["disabled"] = "0" # TODO: find a value 

311 # if self._assertionCount is not None: 

312 # rootElement.attrib["assertions"] = f"{self._assertionCount}" 

313 rootElement.attrib["hostname"] = str(testsuite._hostname) # TODO: find a value 

314 

315 self._xmlDocument = ElementTree(rootElement) 

316 

317 for testclass in testsuite._testclasses.values(): 

318 for tc in testclass._testcases.values(): 

319 self._GenerateTestcase(tc, rootElement) 

320 

321 def _GenerateTestcase(self, testcase: Testcase, parentElement: _Element) -> None: 

322 """ 

323 Generate the internal XML data structure for a test case. 

324 

325 This method generates the XML element (``<testcase>``) and recursively calls other generated methods. 

326 

327 :param testcase: The test case to convert to an XML data structures. 

328 :param parentElement: The parent XML data structure element, this data structure part will be added to. 

329 :return: 

330 """ 

331 testcaseElement = SubElement(parentElement, "testcase") 

332 if testcase.Classname is not None: 332 ↛ 334line 332 didn't jump to line 334 because the condition on line 332 was always true

333 testcaseElement.attrib["classname"] = testcase.Classname 

334 testcaseElement.attrib["name"] = testcase._name 

335 if testcase._duration is not None: 335 ↛ 337line 335 didn't jump to line 337 because the condition on line 335 was always true

336 testcaseElement.attrib["time"] = f"{testcase._duration.total_seconds():.6f}" 

337 if testcase._assertionCount is not None: 337 ↛ 338line 337 didn't jump to line 338 because the condition on line 337 was never true

338 testcaseElement.attrib["assertions"] = f"{testcase._assertionCount}" 

339 

340 testcaseElement.attrib["status"] = "run" # TODO: find a value 

341 

342 if testcase._status is TestcaseStatus.Passed: 342 ↛ 344line 342 didn't jump to line 344 because the condition on line 342 was always true

343 pass 

344 elif testcase._status is TestcaseStatus.Failed: 

345 failureElement = SubElement(testcaseElement, "failure") 

346 elif testcase._status is TestcaseStatus.Skipped: 

347 skippedElement = SubElement(testcaseElement, "skipped") 

348 else: 

349 errorElement = SubElement(testcaseElement, "error")