~ubuntu-branches/ubuntu/oneiric/protobuf/oneiric

« back to all changes in this revision

Viewing changes to gtest/test/gtest_xml_output_unittest.py

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2009-11-16 10:41:33 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20091116104133-ykhy3deg5l4975tw
Tags: 2.1.0-1ubuntu1
* Merge from Debian testing.
* Remaining Ubuntu changes:
  - Disable the death tests on IA64, now as a quilt patch.
  - Don't use python2.4, also as a quilt patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 for the gtest_xml_output module"""
 
33
 
 
34
__author__ = 'eefacm@gmail.com (Sean Mcafee)'
 
35
 
 
36
import errno
 
37
import gtest_test_utils
 
38
import os
 
39
import sys
 
40
import tempfile
 
41
import unittest
 
42
 
 
43
from xml.dom import minidom, Node
 
44
 
 
45
import gtest_xml_test_utils
 
46
 
 
47
GTEST_OUTPUT_FLAG         = "--gtest_output"
 
48
GTEST_DEFAULT_OUTPUT_FILE = "test_detail.xml"
 
49
 
 
50
EXPECTED_NON_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
 
51
<testsuites tests="13" failures="2" disabled="2" errors="0" time="*" name="AllTests">
 
52
  <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" errors="0" time="*">
 
53
    <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
 
54
  </testsuite>
 
55
  <testsuite name="FailedTest" tests="1" failures="1" disabled="0" errors="0" time="*">
 
56
    <testcase name="Fails" status="run" time="*" classname="FailedTest">
 
57
      <failure message="Value of: 2&#x0A;Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
 
58
Value of: 2
 
59
Expected: 1]]></failure>
 
60
    </testcase>
 
61
  </testsuite>
 
62
  <testsuite name="MixedResultTest" tests="3" failures="1" disabled="1" errors="0" time="*">
 
63
    <testcase name="Succeeds" status="run" time="*" classname="MixedResultTest"/>
 
64
    <testcase name="Fails" status="run" time="*" classname="MixedResultTest">
 
65
      <failure message="Value of: 2&#x0A;Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
 
66
Value of: 2
 
67
Expected: 1]]></failure>
 
68
      <failure message="Value of: 3&#x0A;Expected: 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
 
69
Value of: 3
 
70
Expected: 2]]></failure>
 
71
    </testcase>
 
72
    <testcase name="DISABLED_test" status="notrun" time="*" classname="MixedResultTest"/>
 
73
  </testsuite>
 
74
  <testsuite name="DisabledTest" tests="1" failures="0" disabled="1" errors="0" time="*">
 
75
    <testcase name="DISABLED_test_not_run" status="notrun" time="*" classname="DisabledTest"/>
 
76
  </testsuite>
 
77
  <testsuite name="PropertyRecordingTest" tests="4" failures="0" disabled="0" errors="0" time="*">
 
78
    <testcase name="OneProperty" status="run" time="*" classname="PropertyRecordingTest" key_1="1"/>
 
79
    <testcase name="IntValuedProperty" status="run" time="*" classname="PropertyRecordingTest" key_int="1"/>
 
80
    <testcase name="ThreeProperties" status="run" time="*" classname="PropertyRecordingTest" key_1="1" key_2="2" key_3="3"/>
 
81
    <testcase name="TwoValuesForOneKeyUsesLastValue" status="run" time="*" classname="PropertyRecordingTest" key_1="2"/>
 
82
  </testsuite>
 
83
  <testsuite name="NoFixtureTest" tests="3" failures="0" disabled="0" errors="0" time="*">
 
84
     <testcase name="RecordProperty" status="run" time="*" classname="NoFixtureTest" key="1"/>
 
85
     <testcase name="ExternalUtilityThatCallsRecordIntValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_int="1"/>
 
86
     <testcase name="ExternalUtilityThatCallsRecordStringValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_string="1"/>
 
87
  </testsuite>
 
88
</testsuites>"""
 
89
 
 
90
 
 
91
EXPECTED_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
 
92
<testsuites tests="0" failures="0" disabled="0" errors="0" time="*" name="AllTests">
 
93
</testsuites>"""
 
94
 
 
95
 
 
96
class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
 
97
  """
 
98
  Unit test for Google Test's XML output functionality.
 
99
  """
 
100
 
 
101
  def testNonEmptyXmlOutput(self):
 
102
    """
 
103
    Runs a test program that generates a non-empty XML output, and
 
104
    tests that the XML output is expected.
 
105
    """
 
106
    self._TestXmlOutput("gtest_xml_output_unittest_",
 
107
                        EXPECTED_NON_EMPTY_XML, 1)
 
108
 
 
109
  def testEmptyXmlOutput(self):
 
110
    """
 
111
    Runs a test program that generates an empty XML output, and
 
112
    tests that the XML output is expected.
 
113
    """
 
114
 
 
115
    self._TestXmlOutput("gtest_no_test_unittest",
 
116
                        EXPECTED_EMPTY_XML, 0)
 
117
 
 
118
  def testDefaultOutputFile(self):
 
119
    """
 
120
    Confirms that Google Test produces an XML output file with the expected
 
121
    default name if no name is explicitly specified.
 
122
    """
 
123
    temp_dir = tempfile.mkdtemp()
 
124
    output_file     = os.path.join(temp_dir, GTEST_DEFAULT_OUTPUT_FILE)
 
125
    gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
 
126
        "gtest_no_test_unittest")
 
127
    try:
 
128
      os.remove(output_file)
 
129
    except OSError, e:
 
130
      if e.errno != errno.ENOENT:
 
131
        raise
 
132
 
 
133
    p = gtest_test_utils.Subprocess(
 
134
        [gtest_prog_path, "%s=xml" % GTEST_OUTPUT_FLAG],
 
135
        working_dir=temp_dir)
 
136
    self.assert_(p.exited)
 
137
    self.assertEquals(0, p.exit_code)
 
138
    self.assert_(os.path.isfile(output_file))
 
139
 
 
140
 
 
141
  def _TestXmlOutput(self, gtest_prog_name, expected_xml, expected_exit_code):
 
142
    """
 
143
    Asserts that the XML document generated by running the program
 
144
    gtest_prog_name matches expected_xml, a string containing another
 
145
    XML document.  Furthermore, the program's exit code must be
 
146
    expected_exit_code.
 
147
    """
 
148
 
 
149
    xml_path = os.path.join(tempfile.mkdtemp(), gtest_prog_name + "out.xml")
 
150
    gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
 
151
 
 
152
    command = [gtest_prog_path, "%s=xml:%s" % (GTEST_OUTPUT_FLAG, xml_path)]
 
153
    p = gtest_test_utils.Subprocess(command)
 
154
    if p.terminated_by_signal:
 
155
      self.assert_(False,
 
156
                   "%s was killed by signal %d" % (gtest_prog_name, p.signal))
 
157
    else:
 
158
      self.assert_(p.exited)
 
159
      self.assertEquals(expected_exit_code, p.exit_code,
 
160
                        "'%s' exited with code %s, which doesn't match "
 
161
                        "the expected exit code %s."
 
162
                        % (command, p.exit_code, expected_exit_code))
 
163
 
 
164
    expected = minidom.parseString(expected_xml)
 
165
    actual   = minidom.parse(xml_path)
 
166
    self.NormalizeXml(actual.documentElement)
 
167
    self.AssertEquivalentNodes(expected.documentElement,
 
168
                               actual.documentElement)
 
169
    expected.unlink()
 
170
    actual  .unlink()
 
171
 
 
172
 
 
173
 
 
174
if __name__ == '__main__':
 
175
  os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
 
176
  gtest_test_utils.Main()