gtest_xml_test_utils.py 6.78 KB
Newer Older
shiqian's avatar
shiqian committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
#
# Copyright 2006, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Unit test utilities for gtest_xml_output"""

__author__ = 'eefacm@gmail.com (Sean Mcafee)'

import re
import unittest

from xml.dom import minidom, Node

GTEST_OUTPUT_FLAG         = "--gtest_output"
GTEST_DEFAULT_OUTPUT_FILE = "test_detail.xml"

class GTestXMLTestCase(unittest.TestCase):
  """
  Base class for tests of Google Test's XML output functionality.
  """


shiqian's avatar
shiqian committed
50
  def AssertEquivalentNodes(self, expected_node, actual_node):
shiqian's avatar
shiqian committed
51
    """
shiqian's avatar
shiqian committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    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
    elements and actual_node meets all of the following conditions:

    *  It has the same tag name as expected_node.
    *  It has the same set of attributes as expected_node, each with
       the same value as the corresponding attribute of expected_node.
       An exception is any attribute named "time", which needs only be
       convertible to a floating-point number.
    *  It has an equivalent set of child nodes (including elements and
       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.
shiqian's avatar
shiqian committed
66
67
    """

shiqian's avatar
shiqian committed
68
69
70
71
72
73
74
75
76
77
78
    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)
      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)

    expected_attributes = expected_node.attributes
    actual_attributes   = actual_node  .attributes
shiqian's avatar
shiqian committed
79
80
81
82
83
84
85
    self.assertEquals(expected_attributes.length, actual_attributes.length)
    for i in range(expected_attributes.length):
      expected_attr = expected_attributes.item(i)
      actual_attr   = actual_attributes.get(expected_attr.name)
      self.assert_(actual_attr is not None)
      self.assertEquals(expected_attr.value, actual_attr.value)

shiqian's avatar
shiqian committed
86
87
88
89
90
91
92
    expected_children = self._GetChildren(expected_node)
    actual_children = self._GetChildren(actual_node)
    self.assertEquals(len(expected_children), len(actual_children))
    for child_id, child in expected_children.iteritems():
      self.assert_(child_id in actual_children,
                   '<%s> is not in <%s>' % (child_id, actual_children))
      self.AssertEquivalentNodes(child, actual_children[child_id])
shiqian's avatar
shiqian committed
93
94

  identifying_attribute = {
95
    "testsuites": "name",
shiqian's avatar
shiqian committed
96
97
98
99
100
    "testsuite": "name",
    "testcase":  "name",
    "failure":   "message",
    }

shiqian's avatar
shiqian committed
101
  def _GetChildren(self, element):
shiqian's avatar
shiqian committed
102
    """
shiqian's avatar
shiqian committed
103
104
    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
105
106
107
108
109
110
111
112
    children.  For <testsuites>, <testsuite> and <testcase> elements,
    the ID is the value of their "name" attribute; for <failure>
    elements, it is the value of the "message" attribute; for CDATA
    section node, it is "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, other than Text nodes
    containing only whitespace.
shiqian's avatar
shiqian committed
113
    """
shiqian's avatar
shiqian committed
114

shiqian's avatar
shiqian committed
115
116
117
118
119
120
121
122
123
124
    children = {}
    for child in element.childNodes:
      if child.nodeType == Node.ELEMENT_NODE:
        self.assert_(child.tagName in self.identifying_attribute,
                     "Encountered unknown element <%s>" % child.tagName)
        childID = child.getAttribute(self.identifying_attribute[child.tagName])
        self.assert_(childID not in children)
        children[childID] = child
      elif child.nodeType == Node.TEXT_NODE:
        self.assert_(child.nodeValue.isspace())
shiqian's avatar
shiqian committed
125
126
127
      elif child.nodeType == Node.CDATA_SECTION_NODE:
        self.assert_("detail" not in children)
        children["detail"] = child
shiqian's avatar
shiqian committed
128
129
130
131
      else:
        self.fail("Encountered unexpected node type %d" % child.nodeType)
    return children

shiqian's avatar
shiqian committed
132
  def NormalizeXml(self, element):
shiqian's avatar
shiqian committed
133
134
135
136
    """
    Normalizes Google Test's XML output to eliminate references to transient
    information that may change from run to run.

137
138
139
    *  The "time" attribute of <testsuites>, <testsuite> and <testcase>
       elements is replaced with a single asterisk, if it contains
       only digit characters.
shiqian's avatar
shiqian committed
140
141
142
    *  The line number reported in the first line of the "message"
       attribute of <failure> elements is replaced with a single asterisk.
    *  The directory names in file paths are removed.
shiqian's avatar
shiqian committed
143
    *  The stack traces are removed.
shiqian's avatar
shiqian committed
144
    """
shiqian's avatar
shiqian committed
145

146
    if element.tagName in ("testsuites", "testsuite", "testcase"):
shiqian's avatar
shiqian committed
147
      time = element.getAttributeNode("time")
shiqian's avatar
shiqian committed
148
      time.value = re.sub(r"^\d+(\.\d+)?$", "*", time.value)
shiqian's avatar
shiqian committed
149
    elif element.tagName == "failure":
shiqian's avatar
shiqian committed
150
151
152
153
154
155
156
      for child in element.childNodes:
        if child.nodeType == Node.CDATA_SECTION_NODE:
          # Removes the source line number.
          cdata = re.sub(r"^.*/(.*:)\d+\n", "\\1*\n", child.nodeValue)
          # Removes the actual stack trace.
          child.nodeValue = re.sub(r"\nStack trace:\n(.|\n)*",
                                   "", cdata)
shiqian's avatar
shiqian committed
157
158
    for child in element.childNodes:
      if child.nodeType == Node.ELEMENT_NODE:
shiqian's avatar
shiqian committed
159
        self.NormalizeXml(child)