gtest_xml_test_utils.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # Copyright 2006, Google Inc.
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. """Unit test utilities for gtest_xml_output"""
  30. import re
  31. from xml.dom import minidom, Node
  32. from googletest.test import gtest_test_utils
  33. GTEST_DEFAULT_OUTPUT_FILE = 'test_detail.xml'
  34. class GTestXMLTestCase(gtest_test_utils.TestCase):
  35. """Base class for tests of Google Test's XML output functionality."""
  36. def AssertEquivalentNodes(self, expected_node, actual_node):
  37. """Asserts that actual_node is equivalent to expected_node.
  38. Asserts that actual_node (a DOM node object) is equivalent to
  39. expected_node (another DOM node object), in that either both of
  40. them are CDATA nodes and have the same value, or both are DOM
  41. elements and actual_node meets all of the following conditions:
  42. * It has the same tag name as expected_node.
  43. * It has the same set of attributes as expected_node, each with
  44. the same value as the corresponding attribute of expected_node.
  45. Exceptions are any attribute named "time", which needs only be
  46. convertible to a floating-point number and any attribute named
  47. "type_param" which only has to be non-empty.
  48. * It has an equivalent set of child nodes (including elements and
  49. CDATA sections) as expected_node. Note that we ignore the
  50. order of the children as they are not guaranteed to be in any
  51. particular order.
  52. Args:
  53. expected_node: expected DOM node object
  54. actual_node: actual DOM node object
  55. """
  56. if expected_node.nodeType == Node.CDATA_SECTION_NODE:
  57. self.assertEqual(Node.CDATA_SECTION_NODE, actual_node.nodeType)
  58. self.assertEqual(expected_node.nodeValue, actual_node.nodeValue)
  59. return
  60. self.assertEqual(Node.ELEMENT_NODE, actual_node.nodeType)
  61. self.assertEqual(Node.ELEMENT_NODE, expected_node.nodeType)
  62. self.assertEqual(expected_node.tagName, actual_node.tagName)
  63. expected_attributes = expected_node.attributes
  64. actual_attributes = actual_node.attributes
  65. self.assertEqual(
  66. expected_attributes.length,
  67. actual_attributes.length,
  68. 'attribute numbers differ in element %s:\nExpected: %r\nActual: %r'
  69. % (
  70. actual_node.tagName,
  71. expected_attributes.keys(),
  72. actual_attributes.keys(),
  73. ),
  74. )
  75. for i in range(expected_attributes.length):
  76. expected_attr = expected_attributes.item(i)
  77. actual_attr = actual_attributes.get(expected_attr.name)
  78. self.assertTrue(
  79. actual_attr is not None,
  80. 'expected attribute %s not found in element %s'
  81. % (expected_attr.name, actual_node.tagName),
  82. )
  83. self.assertEqual(
  84. expected_attr.value,
  85. actual_attr.value,
  86. ' values of attribute %s in element %s differ: %s vs %s'
  87. % (
  88. expected_attr.name,
  89. actual_node.tagName,
  90. expected_attr.value,
  91. actual_attr.value,
  92. ),
  93. )
  94. expected_children = self._GetChildren(expected_node)
  95. actual_children = self._GetChildren(actual_node)
  96. self.assertEqual(
  97. len(expected_children),
  98. len(actual_children),
  99. 'number of child elements differ in element ' + actual_node.tagName,
  100. )
  101. for child_id, child in expected_children.items():
  102. self.assertTrue(
  103. child_id in actual_children,
  104. '<%s> is not in <%s> (in element %s)'
  105. % (child_id, actual_children, actual_node.tagName),
  106. )
  107. self.AssertEquivalentNodes(child, actual_children[child_id])
  108. identifying_attribute = {
  109. 'testsuites': 'name',
  110. 'testsuite': 'name',
  111. 'testcase': 'name',
  112. 'failure': 'message',
  113. 'skipped': 'message',
  114. 'property': 'name',
  115. }
  116. def _GetChildren(self, element):
  117. """Fetches all of the child nodes of element, a DOM Element object.
  118. Returns them as the values of a dictionary keyed by the IDs of the children.
  119. For <testsuites>, <testsuite>, <testcase>, and <property> elements, the ID
  120. is the value of their "name" attribute; for <failure> elements, it is the
  121. value of the "message" attribute; for <properties> elements, it is the value
  122. of their parent's "name" attribute plus the literal string "properties";
  123. CDATA sections and non-whitespace text nodes are concatenated into a single
  124. CDATA section with ID "detail". An exception is raised if any element other
  125. than the above four is encountered, if two child elements with the same
  126. identifying attributes are encountered, or if any other type of node is
  127. encountered.
  128. Args:
  129. element: DOM Element object
  130. Returns:
  131. Dictionary where keys are the IDs of the children.
  132. """
  133. children = {}
  134. for child in element.childNodes:
  135. if child.nodeType == Node.ELEMENT_NODE:
  136. if child.tagName == 'properties':
  137. self.assertTrue(
  138. child.parentNode is not None,
  139. 'Encountered <properties> element without a parent',
  140. )
  141. child_id = child.parentNode.getAttribute('name') + '-properties'
  142. else:
  143. self.assertTrue(
  144. child.tagName in self.identifying_attribute,
  145. 'Encountered unknown element <%s>' % child.tagName,
  146. )
  147. child_id = child.getAttribute(
  148. self.identifying_attribute[child.tagName]
  149. )
  150. self.assertNotIn(child_id, children)
  151. children[child_id] = child
  152. elif child.nodeType in [Node.TEXT_NODE, Node.CDATA_SECTION_NODE]:
  153. if 'detail' not in children:
  154. if (
  155. child.nodeType == Node.CDATA_SECTION_NODE
  156. or not child.nodeValue.isspace()
  157. ):
  158. children['detail'] = child.ownerDocument.createCDATASection(
  159. child.nodeValue
  160. )
  161. else:
  162. children['detail'].nodeValue += child.nodeValue
  163. else:
  164. self.fail('Encountered unexpected node type %d' % child.nodeType)
  165. return children
  166. def NormalizeXml(self, element):
  167. """Normalizes XML that may change from run to run.
  168. Normalizes Google Test's XML output to eliminate references to transient
  169. information that may change from run to run.
  170. * The "time" attribute of <testsuites>, <testsuite> and <testcase>
  171. elements is replaced with a single asterisk, if it contains
  172. only digit characters.
  173. * The "timestamp" attribute of <testsuites> elements is replaced with a
  174. single asterisk, if it contains a valid ISO8601 datetime value.
  175. * The "type_param" attribute of <testcase> elements is replaced with a
  176. single asterisk (if it sn non-empty) as it is the type name returned
  177. by the compiler and is platform dependent.
  178. * The line info reported in the first line of the "message"
  179. attribute and CDATA section of <failure> elements is replaced with the
  180. file's basename and a single asterisk for the line number.
  181. * The directory names in file paths are removed.
  182. * The stack traces are removed.
  183. Args:
  184. element: DOM element to normalize
  185. """
  186. if element.tagName == 'testcase':
  187. source_file = element.getAttributeNode('file')
  188. if source_file:
  189. source_file.value = re.sub(r'^.*[/\\](.*)', '\\1', source_file.value)
  190. if element.tagName in ('testsuites', 'testsuite', 'testcase'):
  191. timestamp = element.getAttributeNode('timestamp')
  192. timestamp.value = re.sub(
  193. r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d$', '*', timestamp.value
  194. )
  195. if element.tagName in ('testsuites', 'testsuite', 'testcase'):
  196. time = element.getAttributeNode('time')
  197. # The value for exact N seconds has a trailing decimal point (e.g., "10."
  198. # instead of "10")
  199. time.value = re.sub(r'^\d+\.(\d+)?$', '*', time.value)
  200. type_param = element.getAttributeNode('type_param')
  201. if type_param and type_param.value:
  202. type_param.value = '*'
  203. elif element.tagName == 'failure' or element.tagName == 'skipped':
  204. source_line_pat = r'^.*[/\\](.*:)\d+\n'
  205. # Replaces the source line information with a normalized form.
  206. message = element.getAttributeNode('message')
  207. message.value = re.sub(source_line_pat, '\\1*\n', message.value)
  208. for child in element.childNodes:
  209. if child.nodeType == Node.CDATA_SECTION_NODE:
  210. # Replaces the source line information with a normalized form.
  211. cdata = re.sub(source_line_pat, '\\1*\n', child.nodeValue)
  212. # Removes the actual stack trace.
  213. child.nodeValue = re.sub(
  214. r'Stack trace:\n(.|\n)*', 'Stack trace:\n*', cdata
  215. )
  216. for child in element.childNodes:
  217. if child.nodeType == Node.ELEMENT_NODE:
  218. self.NormalizeXml(child)