gtest_xml_test_utils.py 8.7 KB

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