Coverage for pyEDAA/Reports/Unittesting/JUnit/PyTestJUnit.py: 67%

190 statements  

« 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 

38 

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

40from pyTooling.Decorators import export, InheritDocString 

41 

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 

47 

48 

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

50TestcaseAggregateReturnType = Tuple[int, int, int] 

51TestsuiteAggregateReturnType = Tuple[int, int, int, int, int] 

52 

53 

54@export 

55@InheritDocString(ju_Testcase, merge=True) 

56class Testcase(ju_Testcase): 

57 """ 

58 This is a derived implementation for the pyTest JUnit dialect. 

59 """ 

60 

61 

62@export 

63@InheritDocString(ju_Testclass, merge=True) 

64class Testclass(ju_Testclass): 

65 """ 

66 This is a derived implementation for the pyTest JUnit dialect. 

67 """ 

68 

69 

70@export 

71@InheritDocString(ju_Testsuite, merge=True) 

72class Testsuite(ju_Testsuite): 

73 """ 

74 This is a derived implementation for the pyTest JUnit dialect. 

75 """ 

76 

77 def Aggregate(self, strict: bool = True) -> TestsuiteAggregateReturnType: 

78 tests, skipped, errored, failed, passed = super().Aggregate() 

79 

80 for testclass in self._testclasses.values(): 

81 _ = testclass.Aggregate(strict) 

82 

83 tests += 1 

84 

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}'.") 

100 

101 self._tests = tests 

102 self._skipped = skipped 

103 self._errored = errored 

104 self._failed = failed 

105 self._passed = passed 

106 

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 

119 

120 return tests, skipped, errored, failed, passed 

121 

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 pyTest JUnit dialect. 

127 

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

129 :return: Test suite from JUnit specific data model (pyTest JUnitdialect). 

130 """ 

131 juTestsuite = cls( 

132 testsuite._name, 

133 startTime=testsuite._startTime, 

134 duration=testsuite._totalDuration, 

135 status= testsuite._status, 

136 ) 

137 

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 

143 

144 for tc in testsuite.IterateTestcases(): 

145 ts = tc._parent 

146 if ts is None: 146 ↛ 147line 146 didn't jump to line 147 because the condition on line 146 was never true

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

148 

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 

154 

155 if classname in juTestsuite._testclasses: 

156 juClass = juTestsuite._testclasses[classname] 

157 else: 

158 juClass = Testclass(classname, parent=juTestsuite) 

159 

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

161 

162 return juTestsuite 

163 

164 

165@export 

166@InheritDocString(ju_TestsuiteSummary, merge=True) 

167class TestsuiteSummary(ju_TestsuiteSummary): 

168 """ 

169 This is a derived implementation for the pyTest JUnit dialect. 

170 """ 

171 

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 pyTest JUnit dialect. 

177 

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

179 :return: Test suite summary from JUnit specific data model (pyTest 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 ) 

188 

189 

190@export 

191class Document(ju_Document): 

192 """ 

193 A document reader and writer for the pyTest JUnit XML file format. 

194 

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

196 be converted into a unified test entity data model. 

197 

198 In reverse, a JUnit data model instance with the specific pyTest JUnit 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 """ 

201 

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

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

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

205 

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 

218 

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

220 

221 return doc 

222 

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. 

227 

228 .. hint:: 

229 

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

231 

232 The used XML schema definition is specific to the pyTest JUnit dialect. 

233 """ 

234 xmlSchemaFile = "PyTest-JUnit.xsd" 

235 self._Analyze(xmlSchemaFile) 

236 

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 pyTest dialect. 

240 

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: 

249 path = self._path 

250 

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.") 

254 

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) 

257 

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 

262 

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 

268 

269 def Convert(self) -> None: 

270 """ 

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

272 

273 This method converts the root element. 

274 

275 .. hint:: 

276 

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

278 

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 

285 

286 startConversion = perf_counter_ns() 

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

288 

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

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

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

292 

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") 

298 

299 for rootNode in rootElement.iterchildren(tag="testsuite"): # type: _Element 

300 self._ConvertTestsuite(self, rootNode) 

301 

302 self.Aggregate() 

303 endConversation = perf_counter_ns() 

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

305 

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

307 """ 

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

309 

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

311 

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 ) 

322 

323 self._ConvertTestsuiteChildren(testsuitesNode, newTestsuite) 

324 

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

326 """ 

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

328 

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

330 

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.") 

336 

337 rootElement = Element("testsuites") 

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

339 if self._startTime is not None: 339 ↛ 340line 339 didn't jump to line 340 because the condition on line 339 was never true

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

341 if self._duration is not None: 

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 # if self._assertionCount is not None: 

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

349 

350 self._xmlDocument = ElementTree(rootElement) 

351 

352 for testsuite in self._testsuites.values(): 

353 self._GenerateTestsuite(testsuite, rootElement) 

354 

355 def _GenerateTestsuite(self, testsuite: Testsuite, parentElement: _Element) -> None: 

356 """ 

357 Generate the internal XML data structure for a test suite. 

358 

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

360 

361 :param testsuite: The test suite to convert to an XML data structures. 

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

363 :return: 

364 """ 

365 testsuiteElement = SubElement(parentElement, "testsuite") 

366 testsuiteElement.attrib["name"] = testsuite._name 

367 if testsuite._startTime is not None: 367 ↛ 369line 367 didn't jump to line 369 because the condition on line 367 was always true

368 testsuiteElement.attrib["timestamp"] = f"{testsuite._startTime.isoformat()}" 

369 if testsuite._duration is not None: 369 ↛ 371line 369 didn't jump to line 371 because the condition on line 369 was always true

370 testsuiteElement.attrib["time"] = f"{testsuite._duration.total_seconds():.6f}" 

371 testsuiteElement.attrib["tests"] = str(testsuite._tests) 

372 testsuiteElement.attrib["failures"] = str(testsuite._failed) 

373 testsuiteElement.attrib["errors"] = str(testsuite._errored) 

374 testsuiteElement.attrib["skipped"] = str(testsuite._skipped) 

375 # if testsuite._assertionCount is not None: 

376 # testsuiteElement.attrib["assertions"] = f"{testsuite._assertionCount}" 

377 if testsuite._hostname is not None: 

378 testsuiteElement.attrib["hostname"] = testsuite._hostname 

379 

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

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

382 self._GenerateTestcase(tc, testsuiteElement) 

383 

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

385 """ 

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

387 

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

389 

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

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

392 :return: 

393 """ 

394 testcaseElement = SubElement(parentElement, "testcase") 

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

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

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

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

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

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

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

402 

403 if testcase._status is TestcaseStatus.Passed: 

404 pass 

405 elif testcase._status is TestcaseStatus.Failed: 

406 failureElement = SubElement(testcaseElement, "failure") 

407 elif testcase._status is TestcaseStatus.Skipped: 407 ↛ 410line 407 didn't jump to line 410 because the condition on line 407 was always true

408 skippedElement = SubElement(testcaseElement, "skipped") 

409 else: 

410 errorElement = SubElement(testcaseElement, "error")