Coverage for pyEDAA/Reports/Unittesting/OpenTestReporting/Events.py: 100%

47 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 event-based XML format. 

33 

34The JUnit Platform writes this format into :file:`open-test-report.xml`, if the configuration parameter 

35``junit.platform.reporting.open.xml.enabled`` is ``true``. The root element ``<e:events>`` holds an ``<infrastructure>`` 

36element and one event per state change of a test or container: 

37 

38* ``<e:started>`` starts a test or container, identified by its ``id``, with a display name (``name``) and the ``id`` of 

39 its parent (``parentId``). 

40* ``<e:reported>`` adds attachments while it runs, e.g. key-value pairs or output on STDOUT. 

41* ``<e:finished>`` finishes it with a result. 

42 

43Every event carries a timestamp. A test or container, which was started but didn't finish, e.g. because the test run 

44crashed, has no result. 

45 

46.. rubric:: Example 

47 

48.. code-block:: Python 

49 

50 from pathlib import Path 

51 from pyEDAA.Reports.Unittesting.OpenTestReporting.Events import Document 

52 

53 document = Document(Path("build/test-results/test/open-test-report.xml"), analyzeAndConvert=True) 

54 summary = document.ToTestsuiteSummary() 

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

56""" 

57from __future__ import annotations 

58 

59from datetime import datetime 

60from typing import Dict 

61 

62from lxml.etree import QName, _Element 

63from pyTooling.Decorators import export 

64from pyTooling.Stopwatch import Stopwatch 

65 

66from pyEDAA.Reports.Resources.OpenTestReporting import Specification 

67from pyEDAA.Reports.Unittesting import UnittestError 

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

69 

70 

71@export 

72class Document(otr_Document): 

73 """ 

74 A document reader for Open Test Reporting's event-based XML format. 

75 

76 A started event creates a :class:`~pyEDAA.Reports.Unittesting.OpenTestReporting.Node` below the node named by 

77 ``parentId``, or a root node. Every event adds its metadata, sources, attachments and result to the node of its 

78 ``id``. A finished event sets the node's duration: the time from its start to its finish. 

79 """ 

80 

81 _ROOT_NAME = "e:events" #: Prefixed name of the root element ``<e:events>``. 

82 

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

84 """ 

85 Return the qualified tag of ``<e:events>`` in a version of Open Test Reporting. 

86 

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

88 :returns: The tag of the root element, qualified by the namespace URI of the event-based format. 

89 """ 

90 return f"{{{specification.Events.URI}}}events" 

91 

92 def Convert(self) -> None: 

93 """ 

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

95 

96 The events are processed in the order of the file. 

97 

98 .. hint:: 

99 

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

101 

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

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

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

105 :raises UnittestError: If a started event reuses the ``id`` of a started test or container. 

106 :raises UnittestError: If a started event names a ``parentId``, which wasn't started before. |br| 

107 A parent needs to be started before its children. 

108 :raises UnittestError: If a reported or finished event names an ``id``, which wasn't started before. |br| 

109 A test or container needs to be started before it reports or finishes. 

110 """ 

111 if self._xmlDocument is None: 

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

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

114 raise ex 

115 

116 core = self.Specification.Core.URI 

117 events = self.Specification.Events.URI 

118 

119 with Stopwatch() as sw: 

120 nodes: Dict[str, Node] = {} 

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

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

123 self._ConvertInfrastructure(element) 

124 continue 

125 

126 nodeID = element.attrib["id"] 

127 if element.tag == f"{{{events}}}started": 

128 if nodeID in nodes: 

129 raise UnittestError(f"Started event in '{self._path}' reuses the ID '{nodeID}'.") 

130 

131 if (parentID := element.attrib.get("parentId", None)) is None: 

132 parent = self 

133 elif (parent := nodes.get(parentID, None)) is None: 

134 ex = UnittestError(f"Started event '{nodeID}' in '{self._path}' names an unknown parent ID '{parentID}'.") 

135 ex.add_note("A parent needs to be started before its children.") 

136 raise ex 

137 

138 node = Node(element.attrib["name"], datetime.fromisoformat(element.attrib["time"]), parent=parent) 

139 nodes[nodeID] = node 

140 elif (node := nodes.get(nodeID, None)) is None: 

141 ex = UnittestError(f"Event '{QName(element).localname}' in '{self._path}' names an unknown ID '{nodeID}'.") 

142 ex.add_note("A test or container needs to be started before it reports or finishes.") 

143 raise ex 

144 elif element.tag == f"{{{events}}}finished": 

145 node._duration = datetime.fromisoformat(element.attrib["time"]) - node._startTime 

146 

147 self._ConvertTestInfo(element, node) 

148 

149 self._modelConversion = sw.Duration