Coverage for pyEDAA/Reports/Unittesting/OpenTestReporting/Hierarchy.py: 91%

43 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-21 23:49 +0000

1# ==================================================================================================================== # 

2# _____ ____ _ _ ____ _ # 

3# _ __ _ _| ____| _ \ / \ / \ | _ \ ___ _ __ ___ _ __| |_ ___ # 

4# | '_ \| | | | _| | | | |/ _ \ / _ \ | |_) / _ \ '_ \ / _ \| '__| __/ __| # 

5# | |_) | |_| | |___| |_| / ___ \ / ___ \ _| _ < __/ |_) | (_) | | | |_\__ \ # 

6# | .__/ \__, |_____|____/_/ \_\/_/ \_(_)_| \_\___| .__/ \___/|_| \__|___/ # 

7# |_| |___/ |_| # 

8# ==================================================================================================================== # 

9# Authors: # 

10# Patrick Lehmann # 

11# # 

12# License: # 

13# ==================================================================================================================== # 

14# Copyright 2026-2026 Electronic Design Automation Abstraction (EDA²) # 

15# # 

16# Licensed under the Apache License, Version 2.0 (the "License"); # 

17# you may not use this file except in compliance with the License. # 

18# You may obtain a copy of the License at # 

19# # 

20# http://www.apache.org/licenses/LICENSE-2.0 # 

21# # 

22# Unless required by applicable law or agreed to in writing, software # 

23# distributed under the License is distributed on an "AS IS" BASIS, # 

24# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

25# See the License for the specific language governing permissions and # 

26# limitations under the License. # 

27# # 

28# SPDX-License-Identifier: Apache-2.0 # 

29# ==================================================================================================================== # 

30# 

31""" 

32Reader for Open Test Reporting's hierarchical XML format. 

33 

34Open Test Reporting's command line tool converts the event-based format into this format (``convert`` command). The 

35root element ``<h:execution>`` holds an ``<infrastructure>`` element and a ``<h:root>`` element per test or container 

36without parent. A test or container nests its children as ``<h:child>`` elements, and carries a display name 

37(``name``), a start time (``start``) and a duration (``duration``), besides metadata, sources, attachments and its 

38result. 

39 

40.. rubric:: Example 

41 

42.. code-block:: Python 

43 

44 from pathlib import Path 

45 from pyEDAA.Reports.Unittesting.OpenTestReporting.Hierarchy import Document 

46 

47 document = Document(Path("build/test-results/test/hierarchy.xml"), analyzeAndConvert=True) 

48 summary = document.ToTestsuiteSummary() 

49 print(f"{summary.Tests} tests: {summary.Passed} passed, {summary.Failed} failed, {summary.Skipped} skipped") 

50""" 

51from __future__ import annotations 

52 

53from datetime import datetime, timedelta 

54from re import fullmatch 

55 

56from lxml.etree import _Element 

57from pyTooling.Decorators import export 

58from pyTooling.Stopwatch import Stopwatch 

59 

60from pyEDAA.Reports.Resources.OpenTestReporting import Specification 

61from pyEDAA.Reports.Unittesting import UnittestError 

62from pyEDAA.Reports.Unittesting.OpenTestReporting import Execution, Node, Document as otr_Document 

63 

64 

65@export 

66class Document(otr_Document): 

67 """ 

68 A document reader for Open Test Reporting's hierarchical XML format. 

69 

70 A ``<h:root>`` element becomes a root :class:`~pyEDAA.Reports.Unittesting.OpenTestReporting.Node`, a ``<h:child>`` 

71 element a node below the node of its parent element. The ``duration`` attribute (an ``xs:duration`` like 

72 ``PT0.193317644S``) becomes the node's duration, truncated to microseconds. 

73 """ 

74 

75 _ROOT_NAME = "h:execution" #: Prefixed name of the root element ``<h:execution>``. 

76 

77 def _RootTag(self, specification: Specification) -> str: 

78 """ 

79 Return the qualified tag of ``<h:execution>`` in a version of Open Test Reporting. 

80 

81 :param specification: The XML namespaces and schemas of a version. 

82 :returns: The tag of the root element, qualified by the namespace URI of the hierarchical format. 

83 """ 

84 return f"{{{specification.Hierarchy.URI}}}execution" 

85 

86 def Convert(self) -> None: 

87 """ 

88 Convert the parsed and validated XML data structure into a tree of nodes. 

89 

90 .. hint:: 

91 

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

93 

94 :raises UnittestError: If the XML file was not analyzed before. |br| 

95 Call 'Document.Analyze()' or create the document using 

96 'Document(path, analyzeAndConvert=True)'. 

97 :raises UnittestError: If a duration has years, months or a sign. |br| 

98 Supported are days, hours, minutes and seconds. 

99 """ 

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

101 ex = UnittestError(f"Open Test Reporting file '{self._path}' needs to be read and analyzed by an XML parser.") 

102 ex.add_note("Call 'Document.Analyze()' or create the document using 'Document(path, analyzeAndConvert=True)'.") 

103 raise ex 

104 

105 core = self.Specification.Core.URI 

106 hierarchy = self.Specification.Hierarchy.URI 

107 

108 with Stopwatch() as sw: 

109 for element in self._xmlDocument.getroot().iterchildren("*"): # type: _Element 

110 if element.tag == f"{{{core}}}infrastructure": 

111 self._ConvertInfrastructure(element) 

112 elif element.tag == f"{{{hierarchy}}}root": 112 ↛ 109line 112 didn't jump to line 109 because the condition on line 112 was always true

113 self._ConvertNode(element, self) 

114 

115 self._modelConversion = sw.Duration 

116 

117 def _ConvertNode(self, nodeElement: _Element, parent: Node | Execution) -> Node: 

118 """ 

119 Convert a ``<h:root>`` or ``<h:child>`` element and its child elements to a node. 

120 

121 :param nodeElement: The XML element node describing a test or container. 

122 :param parent: The parent node, or the execution for a root node. 

123 :returns: The node. 

124 :raises UnittestError: If the duration has years, months or a sign. |br| 

125 Supported are days, hours, minutes and seconds. 

126 """ 

127 if (durationText := nodeElement.attrib.get("duration", None)) is None: 

128 duration = None 

129 elif (match := fullmatch(r"P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)(?:\.(\d+))?S)?)?", durationText)) is None: 

130 ex = UnittestError(f"Unsupported duration '{durationText}' in '{self._path}'.") 

131 ex.add_note("Supported are days, hours, minutes and seconds, e.g. 'PT0.193317644S'.") 

132 raise ex 

133 else: 

134 days, hours, minutes, seconds, fraction = match.groups(default="0") 

135 duration = timedelta( 

136 days=int(days), 

137 hours=int(hours), 

138 minutes=int(minutes), 

139 seconds=int(seconds), 

140 microseconds=int(fraction[:6].ljust(6, "0")) 

141 ) 

142 

143 node = Node( 

144 nodeElement.attrib["name"], 

145 datetime.fromisoformat(nodeElement.attrib["start"]), 

146 duration, 

147 parent=parent 

148 ) 

149 self._ConvertTestInfo(nodeElement, node) 

150 

151 hierarchy = self.Specification.Hierarchy.URI 

152 for childElement in nodeElement.iterchildren(f"{{{hierarchy}}}child"): # type: _Element 

153 self._ConvertNode(childElement, node) 

154 

155 return node