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 32 """Unit test utilities for gtest_xml_output""" 33 34 __author__ = 'eefacm (at] gmail.com (Sean Mcafee)' 35 36 import re 37 import unittest 38 39 from xml.dom import minidom, Node 40 41 GTEST_OUTPUT_FLAG = "--gtest_output" 42 GTEST_DEFAULT_OUTPUT_FILE = "test_detail.xml" 43 44 class GTestXMLTestCase(unittest.TestCase): 45 """ 46 Base class for tests of Google Test's XML output functionality. 47 """ 48 49 50 def AssertEquivalentNodes(self, expected_node, actual_node): 51 """ 52 Asserts that actual_node (a DOM node object) is equivalent to 53 expected_node (another DOM node object), in that either both of 54 them are CDATA nodes and have the same value, or both are DOM 55 elements and actual_node meets all of the following conditions: 56 57 * It has the same tag name as expected_node. 58 * It has the same set of attributes as expected_node, each with 59 the same value as the corresponding attribute of expected_node. 60 An exception is any attribute named "time", which needs only be 61 convertible to a floating-point number. 62 * It has an equivalent set of child nodes (including elements and 63 CDATA sections) as expected_node. Note that we ignore the 64 order of the children as they are not guaranteed to be in any 65 particular order. 66 """ 67 68 if expected_node.nodeType == Node.CDATA_SECTION_NODE: 69 self.assertEquals(Node.CDATA_SECTION_NODE, actual_node.nodeType) 70 self.assertEquals(expected_node.nodeValue, actual_node.nodeValue) 71 return 72 73 self.assertEquals(Node.ELEMENT_NODE, actual_node.nodeType) 74 self.assertEquals(Node.ELEMENT_NODE, expected_node.nodeType) 75 self.assertEquals(expected_node.tagName, actual_node.tagName) 76 77 expected_attributes = expected_node.attributes 78 actual_attributes = actual_node .attributes 79 self.assertEquals(expected_attributes.length, actual_attributes.length) 80 for i in range(expected_attributes.length): 81 expected_attr = expected_attributes.item(i) 82 actual_attr = actual_attributes.get(expected_attr.name) 83 self.assert_(actual_attr is not None) 84 self.assertEquals(expected_attr.value, actual_attr.value) 85 86 expected_children = self._GetChildren(expected_node) 87 actual_children = self._GetChildren(actual_node) 88 self.assertEquals(len(expected_children), len(actual_children)) 89 for child_id, child in expected_children.iteritems(): 90 self.assert_(child_id in actual_children, 91 '<%s> is not in <%s>' % (child_id, actual_children)) 92 self.AssertEquivalentNodes(child, actual_children[child_id]) 93 94 identifying_attribute = { 95 "testsuite": "name", 96 "testcase": "name", 97 "failure": "message", 98 } 99 100 def _GetChildren(self, element): 101 """ 102 Fetches all of the child nodes of element, a DOM Element object. 103 Returns them as the values of a dictionary keyed by the IDs of the 104 children. For <testsuite> and <testcase> elements, the ID is the 105 value of their "name" attribute; for <failure> elements, it is the 106 value of the "message" attribute; for CDATA section node, it is 107 "detail". An exception is raised if any element other than the 108 above four is encountered, if two child elements with the same 109 identifying attributes are encountered, or if any other type of 110 node is encountered, other than Text nodes containing only 111 whitespace. 112 """ 113 114 children = {} 115 for child in element.childNodes: 116 if child.nodeType == Node.ELEMENT_NODE: 117 self.assert_(child.tagName in self.identifying_attribute, 118 "Encountered unknown element <%s>" % child.tagName) 119 childID = child.getAttribute(self.identifying_attribute[child.tagName]) 120 self.assert_(childID not in children) 121 children[childID] = child 122 elif child.nodeType == Node.TEXT_NODE: 123 self.assert_(child.nodeValue.isspace()) 124 elif child.nodeType == Node.CDATA_SECTION_NODE: 125 self.assert_("detail" not in children) 126 children["detail"] = child 127 else: 128 self.fail("Encountered unexpected node type %d" % child.nodeType) 129 return children 130 131 def NormalizeXml(self, element): 132 """ 133 Normalizes Google Test's XML output to eliminate references to transient 134 information that may change from run to run. 135 136 * The "time" attribute of <testsuite> and <testcase> elements is 137 replaced with a single asterisk, if it contains only digit 138 characters. 139 * The line number reported in the first line of the "message" 140 attribute of <failure> elements is replaced with a single asterisk. 141 * The directory names in file paths are removed. 142 * The stack traces are removed. 143 """ 144 145 if element.tagName in ("testsuite", "testcase"): 146 time = element.getAttributeNode("time") 147 time.value = re.sub(r"^\d+(\.\d+)?$", "*", time.value) 148 elif element.tagName == "failure": 149 for child in element.childNodes: 150 if child.nodeType == Node.CDATA_SECTION_NODE: 151 # Removes the source line number. 152 cdata = re.sub(r"^.*/(.*:)\d+\n", "\\1*\n", child.nodeValue) 153 # Removes the actual stack trace. 154 child.nodeValue = re.sub(r"\nStack trace:\n(.|\n)*", 155 "", cdata) 156 for child in element.childNodes: 157 if child.nodeType == Node.ELEMENT_NODE: 158 self.NormalizeXml(child) 159