gtest_xml_test_utils.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. import gtest_test_utils
  33. GTEST_DEFAULT_OUTPUT_FILE = 'test_detail.xml'
  34. class GTestXMLTestCase(gtest_test_utils.TestCase):
  35. """
  36. Base class for tests of Google Test's XML output functionality.
  37. """
  38. def AssertEquivalentNodes(self, expected_node, actual_node):
  39. """
  40. Asserts that actual_node (a DOM node object) is equivalent to
  41. expected_node (another DOM node object), in that either both of
  42. them are CDATA nodes and have the same value, or both are DOM
  43. elements and actual_node meets all of the following conditions:
  44. * It has the same tag name as expected_node.
  45. * It has the same set of attributes as expected_node, each with
  46. the same value as the corresponding attribute of expected_node.
  47. Exceptions are any attribute named "time", which needs only be
  48. convertible to a floating-point number and any attribute named
  49. "type_param" which only has to be non-empty.
  50. * It has an equivalent set of child nodes (including elements and
  51. CDATA sections) as expected_node. Note that we ignore the
  52. order of the children as they are not guaranteed to be in any
  53. particular order.
  54. """
  55. if expected_node.nodeType == Node.CDATA_SECTION_NODE:
  56. self.assertEquals(Node.CDATA_SECTION_NODE, actual_node.nodeType)
  57. self.assertEquals(expected_node.nodeValue, actual_node.nodeValue)
  58. return
  59. self.assertEquals(Node.ELEMENT_NODE, actual_node.nodeType)
  60. self.assertEquals(Node.ELEMENT_NODE, expected_node.nodeType)
  61. self.assertEquals(expected_node.tagName, actual_node.tagName)
  62. expected_attributes = expected_node.attributes
  63. actual_attributes = actual_node.attributes
  64. self.assertEquals(
  65. expected_attributes.length, actual_attributes.length,
  66. 'attribute numbers differ in element %s:\nExpected: %r\nActual: %r' % (
  67. actual_node.tagName, expected_attributes.keys(),
  68. actual_attributes.keys()))
  69. for i in range(expected_attributes.length):
  70. expected_attr = expected_attributes.item(i)
  71. actual_attr = actual_attributes.get(expected_attr.name)
  72. self.assert_(
  73. actual_attr is not None,
  74. 'expected attribute %s not found in element %s' %
  75. (expected_attr.name, actual_node.tagName))
  76. self.assertEquals(
  77. expected_attr.value, actual_attr.value,
  78. ' values of attribute %s in element %s differ: %s vs %s' %
  79. (expected_attr.name, actual_node.tagName,
  80. expected_attr.value, actual_attr.value))
  81. expected_children = self._GetChildren(expected_node)
  82. actual_children = self._GetChildren(actual_node)
  83. self.assertEquals(
  84. len(expected_children), len(actual_children),
  85. 'number of child elements differ in element ' + actual_node.tagName)
  86. for child_id, child in expected_children.items():
  87. self.assert_(child_id in actual_children,
  88. '<%s> is not in <%s> (in element %s)' %
  89. (child_id, actual_children, actual_node.tagName))
  90. self.AssertEquivalentNodes(child, actual_children[child_id])
  91. identifying_attribute = {
  92. 'testsuites': 'name',
  93. 'testsuite': 'name',
  94. 'testcase': 'name',
  95. 'failure': 'message',
  96. 'skipped': 'message',
  97. 'property': 'name',
  98. }
  99. def _GetChildren(self, element):
  100. """
  101. Fetches all of the child nodes of element, a DOM Element object.
  102. Returns them as the values of a dictionary keyed by the IDs of the
  103. children. For <testsuites>, <testsuite>, <testcase>, and <property>
  104. elements, the ID is the value of their "name" attribute; for <failure>
  105. elements, it is the value of the "message" attribute; for <properties>
  106. elements, it is the value of their parent's "name" attribute plus the
  107. literal string "properties"; CDATA sections and non-whitespace
  108. text nodes are concatenated into a single CDATA section with ID
  109. "detail". An exception is raised if any element other than the above
  110. four is encountered, if two child elements with the same identifying
  111. attributes are encountered, or if any other type of node is encountered.
  112. """
  113. children = {}
  114. for child in element.childNodes:
  115. if child.nodeType == Node.ELEMENT_NODE:
  116. if child.tagName == 'properties':
  117. self.assert_(child.parentNode is not None,
  118. 'Encountered <properties> element without a parent')
  119. child_id = child.parentNode.getAttribute('name') + '-properties'
  120. else:
  121. self.assert_(child.tagName in self.identifying_attribute,
  122. 'Encountered unknown element <%s>' % child.tagName)
  123. child_id = child.getAttribute(
  124. self.identifying_attribute[child.tagName])
  125. self.assert_(child_id not in children)
  126. children[child_id] = child
  127. elif child.nodeType in [Node.TEXT_NODE, Node.CDATA_SECTION_NODE]:
  128. if 'detail' not in children:
  129. if (child.nodeType == Node.CDATA_SECTION_NODE or
  130. not child.nodeValue.isspace()):
  131. children['detail'] = child.ownerDocument.createCDATASection(
  132. child.nodeValue)
  133. else:
  134. children['detail'].nodeValue += child.nodeValue
  135. else:
  136. self.fail('Encountered unexpected node type %d' % child.nodeType)
  137. return children
  138. def NormalizeXml(self, element):
  139. """
  140. Normalizes Google Test's XML output to eliminate references to transient
  141. information that may change from run to run.
  142. * The "time" attribute of <testsuites>, <testsuite> and <testcase>
  143. elements is replaced with a single asterisk, if it contains
  144. only digit characters.
  145. * The "timestamp" attribute of <testsuites> elements is replaced with a
  146. single asterisk, if it contains a valid ISO8601 datetime value.
  147. * The "type_param" attribute of <testcase> elements is replaced with a
  148. single asterisk (if it sn non-empty) as it is the type name returned
  149. by the compiler and is platform dependent.
  150. * The line info reported in the first line of the "message"
  151. attribute and CDATA section of <failure> elements is replaced with the
  152. file's basename and a single asterisk for the line number.
  153. * The directory names in file paths are removed.
  154. * The stack traces are removed.
  155. """
  156. if element.tagName in ('testsuites', 'testsuite', 'testcase'):
  157. timestamp = element.getAttributeNode('timestamp')
  158. timestamp.value = re.sub(r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d$',
  159. '*', timestamp.value)
  160. if element.tagName in ('testsuites', 'testsuite', 'testcase'):
  161. time = element.getAttributeNode('time')
  162. time.value = re.sub(r'^\d+(\.\d+)?$', '*', time.value)
  163. type_param = element.getAttributeNode('type_param')
  164. if type_param and type_param.value:
  165. type_param.value = '*'
  166. elif element.tagName == 'failure' or element.tagName == 'skipped':
  167. source_line_pat = r'^.*[/\\](.*:)\d+\n'
  168. # Replaces the source line information with a normalized form.
  169. message = element.getAttributeNode('message')
  170. message.value = re.sub(source_line_pat, '\\1*\n', message.value)
  171. for child in element.childNodes:
  172. if child.nodeType == Node.CDATA_SECTION_NODE:
  173. # Replaces the source line information with a normalized form.
  174. cdata = re.sub(source_line_pat, '\\1*\n', child.nodeValue)
  175. # Removes the actual stack trace.
  176. child.nodeValue = re.sub(r'Stack trace:\n(.|\n)*',
  177. 'Stack trace:\n*', cdata)
  178. for child in element.childNodes:
  179. if child.nodeType == Node.ELEMENT_NODE:
  180. self.NormalizeXml(child)