diff options
Diffstat (limited to 'googletest/test/gtest_xml_test_utils.py')
-rwxr-xr-x | googletest/test/gtest_xml_test_utils.py | 159 |
1 files changed, 102 insertions, 57 deletions
diff --git a/googletest/test/gtest_xml_test_utils.py b/googletest/test/gtest_xml_test_utils.py index ec42c62c..74e0f4a0 100755 --- a/googletest/test/gtest_xml_test_utils.py +++ b/googletest/test/gtest_xml_test_utils.py @@ -31,18 +31,17 @@ import re from xml.dom import minidom, Node -import gtest_test_utils +from googletest.test import gtest_test_utils GTEST_DEFAULT_OUTPUT_FILE = 'test_detail.xml' -class GTestXMLTestCase(gtest_test_utils.TestCase): - """ - Base class for tests of Google Test's XML output functionality. - """ +class GTestXMLTestCase(gtest_test_utils.TestCase): + """Base class for tests of Google Test's XML output functionality.""" def AssertEquivalentNodes(self, expected_node, actual_node): - """ + """Asserts that actual_node is equivalent to expected_node. + Asserts that actual_node (a DOM node object) is equivalent to expected_node (another DOM node object), in that either both of them are CDATA nodes and have the same value, or both are DOM @@ -58,46 +57,66 @@ class GTestXMLTestCase(gtest_test_utils.TestCase): CDATA sections) as expected_node. Note that we ignore the order of the children as they are not guaranteed to be in any particular order. + + Args: + expected_node: expected DOM node object + actual_node: actual DOM node object """ if expected_node.nodeType == Node.CDATA_SECTION_NODE: - self.assertEquals(Node.CDATA_SECTION_NODE, actual_node.nodeType) - self.assertEquals(expected_node.nodeValue, actual_node.nodeValue) + self.assertEqual(Node.CDATA_SECTION_NODE, actual_node.nodeType) + self.assertEqual(expected_node.nodeValue, actual_node.nodeValue) return - self.assertEquals(Node.ELEMENT_NODE, actual_node.nodeType) - self.assertEquals(Node.ELEMENT_NODE, expected_node.nodeType) - self.assertEquals(expected_node.tagName, actual_node.tagName) + self.assertEqual(Node.ELEMENT_NODE, actual_node.nodeType) + self.assertEqual(Node.ELEMENT_NODE, expected_node.nodeType) + self.assertEqual(expected_node.tagName, actual_node.tagName) expected_attributes = expected_node.attributes actual_attributes = actual_node.attributes - self.assertEquals( - expected_attributes.length, actual_attributes.length, - 'attribute numbers differ in element %s:\nExpected: %r\nActual: %r' % ( - actual_node.tagName, expected_attributes.keys(), - actual_attributes.keys())) + self.assertEqual( + expected_attributes.length, + actual_attributes.length, + 'attribute numbers differ in element %s:\nExpected: %r\nActual: %r' + % ( + actual_node.tagName, + expected_attributes.keys(), + actual_attributes.keys(), + ), + ) for i in range(expected_attributes.length): expected_attr = expected_attributes.item(i) actual_attr = actual_attributes.get(expected_attr.name) - self.assert_( + self.assertTrue( actual_attr is not None, - 'expected attribute %s not found in element %s' % - (expected_attr.name, actual_node.tagName)) - self.assertEquals( - expected_attr.value, actual_attr.value, - ' values of attribute %s in element %s differ: %s vs %s' % - (expected_attr.name, actual_node.tagName, - expected_attr.value, actual_attr.value)) + 'expected attribute %s not found in element %s' + % (expected_attr.name, actual_node.tagName), + ) + self.assertEqual( + expected_attr.value, + actual_attr.value, + ' values of attribute %s in element %s differ: %s vs %s' + % ( + expected_attr.name, + actual_node.tagName, + expected_attr.value, + actual_attr.value, + ), + ) expected_children = self._GetChildren(expected_node) actual_children = self._GetChildren(actual_node) - self.assertEquals( - len(expected_children), len(actual_children), - 'number of child elements differ in element ' + actual_node.tagName) + self.assertEqual( + len(expected_children), + len(actual_children), + 'number of child elements differ in element ' + actual_node.tagName, + ) for child_id, child in expected_children.items(): - self.assert_(child_id in actual_children, - '<%s> is not in <%s> (in element %s)' % - (child_id, actual_children, actual_node.tagName)) + self.assertTrue( + child_id in actual_children, + '<%s> is not in <%s> (in element %s)' + % (child_id, actual_children, actual_node.tagName), + ) self.AssertEquivalentNodes(child, actual_children[child_id]) identifying_attribute = { @@ -110,40 +129,54 @@ class GTestXMLTestCase(gtest_test_utils.TestCase): } def _GetChildren(self, element): - """ - Fetches all of the child nodes of element, a DOM Element object. - Returns them as the values of a dictionary keyed by the IDs of the - children. For <testsuites>, <testsuite>, <testcase>, and <property> - elements, the ID is the value of their "name" attribute; for <failure> - elements, it is the value of the "message" attribute; for <properties> - elements, it is the value of their parent's "name" attribute plus the - literal string "properties"; CDATA sections and non-whitespace - text nodes are concatenated into a single CDATA section with ID - "detail". An exception is raised if any element other than the above - four is encountered, if two child elements with the same identifying - attributes are encountered, or if any other type of node is encountered. + """Fetches all of the child nodes of element, a DOM Element object. + + Returns them as the values of a dictionary keyed by the IDs of the children. + For <testsuites>, <testsuite>, <testcase>, and <property> elements, the ID + is the value of their "name" attribute; for <failure> elements, it is the + value of the "message" attribute; for <properties> elements, it is the value + of their parent's "name" attribute plus the literal string "properties"; + CDATA sections and non-whitespace text nodes are concatenated into a single + CDATA section with ID "detail". An exception is raised if any element other + than the above four is encountered, if two child elements with the same + identifying attributes are encountered, or if any other type of node is + encountered. + + Args: + element: DOM Element object + + Returns: + Dictionary where keys are the IDs of the children. """ children = {} for child in element.childNodes: if child.nodeType == Node.ELEMENT_NODE: if child.tagName == 'properties': - self.assert_(child.parentNode is not None, - 'Encountered <properties> element without a parent') + self.assertTrue( + child.parentNode is not None, + 'Encountered <properties> element without a parent', + ) child_id = child.parentNode.getAttribute('name') + '-properties' else: - self.assert_(child.tagName in self.identifying_attribute, - 'Encountered unknown element <%s>' % child.tagName) + self.assertTrue( + child.tagName in self.identifying_attribute, + 'Encountered unknown element <%s>' % child.tagName, + ) child_id = child.getAttribute( - self.identifying_attribute[child.tagName]) - self.assert_(child_id not in children) + self.identifying_attribute[child.tagName] + ) + self.assertNotIn(child_id, children) children[child_id] = child elif child.nodeType in [Node.TEXT_NODE, Node.CDATA_SECTION_NODE]: if 'detail' not in children: - if (child.nodeType == Node.CDATA_SECTION_NODE or - not child.nodeValue.isspace()): + if ( + child.nodeType == Node.CDATA_SECTION_NODE + or not child.nodeValue.isspace() + ): children['detail'] = child.ownerDocument.createCDATASection( - child.nodeValue) + child.nodeValue + ) else: children['detail'].nodeValue += child.nodeValue else: @@ -151,7 +184,8 @@ class GTestXMLTestCase(gtest_test_utils.TestCase): return children def NormalizeXml(self, element): - """ + """Normalizes XML that may change from run to run. + Normalizes Google Test's XML output to eliminate references to transient information that may change from run to run. @@ -168,15 +202,25 @@ class GTestXMLTestCase(gtest_test_utils.TestCase): file's basename and a single asterisk for the line number. * The directory names in file paths are removed. * The stack traces are removed. + + Args: + element: DOM element to normalize """ + if element.tagName == 'testcase': + source_file = element.getAttributeNode('file') + if source_file: + source_file.value = re.sub(r'^.*[/\\](.*)', '\\1', source_file.value) if element.tagName in ('testsuites', 'testsuite', 'testcase'): timestamp = element.getAttributeNode('timestamp') - timestamp.value = re.sub(r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d$', - '*', timestamp.value) + timestamp.value = re.sub( + r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d$', '*', timestamp.value + ) if element.tagName in ('testsuites', 'testsuite', 'testcase'): time = element.getAttributeNode('time') - time.value = re.sub(r'^\d+(\.\d+)?$', '*', time.value) + # The value for exact N seconds has a trailing decimal point (e.g., "10." + # instead of "10") + time.value = re.sub(r'^\d+\.(\d+)?$', '*', time.value) type_param = element.getAttributeNode('type_param') if type_param and type_param.value: type_param.value = '*' @@ -190,8 +234,9 @@ class GTestXMLTestCase(gtest_test_utils.TestCase): # Replaces the source line information with a normalized form. cdata = re.sub(source_line_pat, '\\1*\n', child.nodeValue) # Removes the actual stack trace. - child.nodeValue = re.sub(r'Stack trace:\n(.|\n)*', - 'Stack trace:\n*', cdata) + child.nodeValue = re.sub( + r'Stack trace:\n(.|\n)*', 'Stack trace:\n*', cdata + ) for child in element.childNodes: if child.nodeType == Node.ELEMENT_NODE: self.NormalizeXml(child) |