gtest_xml_test_utils.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.iteritems():
  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. 'property': 'name',
  97. }
  98. def _GetChildren(self, element):
  99. """
  100. Fetches all of the child nodes of element, a DOM Element object.
  101. Returns them as the values of a dictionary keyed by the IDs of the
  102. children. For <testsuites>, <testsuite>, <testcase>, and <property>
  103. elements, the ID is the value of their "name" attribute; for <failure>
  104. elements, it is the value of the "message" attribute; for <properties>
  105. elements, it is the value of their parent's "name" attribute plus the
  106. literal string "properties"; CDATA sections and non-whitespace
  107. text nodes are concatenated into a single CDATA section with ID
  108. "detail". An exception is raised if any element other than the above
  109. four is encountered, if two child elements with the same identifying
  110. attributes are encountered, or if any other type of node is encountered.
  111. """
  112. children = {}
  113. for child in element.childNodes:
  114. if child.nodeType == Node.ELEMENT_NODE:
  115. if child.tagName == 'properties':
  116. self.assert_(child.parentNode is not None,
  117. 'Encountered <properties> element without a parent')
  118. child_id = child.parentNode.getAttribute('name') + '-properties'
  119. else:
  120. self.assert_(child.tagName in self.identifying_attribute,
  121. 'Encountered unknown element <%s>' % child.tagName)
  122. child_id = child.getAttribute(
  123. self.identifying_attribute[child.tagName])
  124. self.assert_(child_id not in children)
  125. children[child_id] = child
  126. elif child.nodeType in [Node.TEXT_NODE, Node.CDATA_SECTION_NODE]:
  127. if 'detail' not in children:
  128. if (child.nodeType == Node.CDATA_SECTION_NODE or
  129. not child.nodeValue.isspace()):
  130. children['detail'] = child.ownerDocument.createCDATASection(
  131. child.nodeValue)
  132. else:
  133. children['detail'].nodeValue += child.nodeValue
  134. else:
  135. self.fail('Encountered unexpected node type %d' % child.nodeType)
  136. return children
  137. def NormalizeXml(self, element):
  138. """
  139. Normalizes Google Test's XML output to eliminate references to transient
  140. information that may change from run to run.
  141. * The "time" attribute of <testsuites>, <testsuite> and <testcase>
  142. elements is replaced with a single asterisk, if it contains
  143. only digit characters.
  144. * The "timestamp" attribute of <testsuites> elements is replaced with a
  145. single asterisk, if it contains a valid ISO8601 datetime value.
  146. * The "type_param" attribute of <testcase> elements is replaced with a
  147. single asterisk (if it sn non-empty) as it is the type name returned
  148. by the compiler and is platform dependent.
  149. * The line info reported in the first line of the "message"
  150. attribute and CDATA section of <failure> elements is replaced with the
  151. file's basename and a single asterisk for the line number.
  152. * The directory names in file paths are removed.
  153. * The stack traces are removed.
  154. """
  155. if element.tagName == 'testsuites':
  156. timestamp = element.getAttributeNode('timestamp')
  157. timestamp.value = re.sub(r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d$',
  158. '*', timestamp.value)
  159. if element.tagName in ('testsuites', 'testsuite', 'testcase'):
  160. time = element.getAttributeNode('time')
  161. time.value = re.sub(r'^\d+(\.\d+)?$', '*', time.value)
  162. type_param = element.getAttributeNode('type_param')
  163. if type_param and type_param.value:
  164. type_param.value = '*'
  165. elif element.tagName == 'failure':
  166. source_line_pat = r'^.*[/\\](.*:)\d+\n'
  167. # Replaces the source line information with a normalized form.
  168. message = element.getAttributeNode('message')
  169. message.value = re.sub(source_line_pat, '\\1*\n', message.value)
  170. for child in element.childNodes:
  171. if child.nodeType == Node.CDATA_SECTION_NODE:
  172. # Replaces the source line information with a normalized form.
  173. cdata = re.sub(source_line_pat, '\\1*\n', child.nodeValue)
  174. # Removes the actual stack trace.
  175. child.nodeValue = re.sub(r'Stack trace:\n(.|\n)*',
  176. 'Stack trace:\n*', cdata)
  177. for child in element.childNodes:
  178. if child.nodeType == Node.ELEMENT_NODE:
  179. self.NormalizeXml(child)