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
36import errno
37import os
38import sys
39from xml.dom import minidom, Node
40
41import gtest_test_utils
42import gtest_xml_test_utils
43
44
45GTEST_OUTPUT_FLAG         = "--gtest_output"
46GTEST_DEFAULT_OUTPUT_FILE = "test_detail.xml"
47GTEST_PROGRAM_NAME = "gtest_xml_output_unittest_"
48
49SUPPORTS_STACK_TRACES = False
50
51if SUPPORTS_STACK_TRACES:
52  STACK_TRACE_TEMPLATE = "\nStack trace:\n*"
53else:
54  STACK_TRACE_TEMPLATE = ""
55
56EXPECTED_NON_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
57<testsuites tests="23" failures="4" disabled="2" errors="0" time="*" name="AllTests">
58  <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" errors="0" time="*">
59    <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
60  </testsuite>
61  <testsuite name="FailedTest" tests="1" failures="1" disabled="0" errors="0" time="*">
62    <testcase name="Fails" status="run" time="*" classname="FailedTest">
63      <failure message="Value of: 2&#x0A;Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
64Value of: 2
65Expected: 1%(stack)s]]></failure>
66    </testcase>
67  </testsuite>
68  <testsuite name="MixedResultTest" tests="3" failures="1" disabled="1" errors="0" time="*">
69    <testcase name="Succeeds" status="run" time="*" classname="MixedResultTest"/>
70    <testcase name="Fails" status="run" time="*" classname="MixedResultTest">
71      <failure message="Value of: 2&#x0A;Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
72Value of: 2
73Expected: 1%(stack)s]]></failure>
74      <failure message="Value of: 3&#x0A;Expected: 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
75Value of: 3
76Expected: 2%(stack)s]]></failure>
77    </testcase>
78    <testcase name="DISABLED_test" status="notrun" time="*" classname="MixedResultTest"/>
79  </testsuite>
80  <testsuite name="XmlQuotingTest" tests="1" failures="1" disabled="0" errors="0" time="*">
81    <testcase name="OutputsCData" status="run" time="*" classname="XmlQuotingTest">
82      <failure message="Failed&#x0A;XML output: &lt;?xml encoding=&quot;utf-8&quot;&gt;&lt;top&gt;&lt;![CDATA[cdata text]]&gt;&lt;/top&gt;" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
83Failed
84XML output: <?xml encoding="utf-8"><top><![CDATA[cdata text]]>]]&gt;<![CDATA[</top>%(stack)s]]></failure>
85    </testcase>
86  </testsuite>
87  <testsuite name="InvalidCharactersTest" tests="1" failures="1" disabled="0" errors="0" time="*">
88    <testcase name="InvalidCharactersInMessage" status="run" time="*" classname="InvalidCharactersTest">
89      <failure message="Failed&#x0A;Invalid characters in brackets []" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
90Failed
91Invalid characters in brackets []%(stack)s]]></failure>
92    </testcase>
93  </testsuite>
94  <testsuite name="DisabledTest" tests="1" failures="0" disabled="1" errors="0" time="*">
95    <testcase name="DISABLED_test_not_run" status="notrun" time="*" classname="DisabledTest"/>
96  </testsuite>
97  <testsuite name="PropertyRecordingTest" tests="4" failures="0" disabled="0" errors="0" time="*">
98    <testcase name="OneProperty" status="run" time="*" classname="PropertyRecordingTest" key_1="1"/>
99    <testcase name="IntValuedProperty" status="run" time="*" classname="PropertyRecordingTest" key_int="1"/>
100    <testcase name="ThreeProperties" status="run" time="*" classname="PropertyRecordingTest" key_1="1" key_2="2" key_3="3"/>
101    <testcase name="TwoValuesForOneKeyUsesLastValue" status="run" time="*" classname="PropertyRecordingTest" key_1="2"/>
102  </testsuite>
103  <testsuite name="NoFixtureTest" tests="3" failures="0" disabled="0" errors="0" time="*">
104     <testcase name="RecordProperty" status="run" time="*" classname="NoFixtureTest" key="1"/>
105     <testcase name="ExternalUtilityThatCallsRecordIntValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_int="1"/>
106     <testcase name="ExternalUtilityThatCallsRecordStringValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_string="1"/>
107  </testsuite>
108  <testsuite name="Single/ValueParamTest" tests="4" failures="0" disabled="0" errors="0" time="*">
109    <testcase name="HasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
110    <testcase name="HasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
111    <testcase name="AnotherTestThatHasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
112    <testcase name="AnotherTestThatHasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
113  </testsuite>
114  <testsuite name="TypedTest/0" tests="1" failures="0" disabled="0" errors="0" time="*">
115    <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/0" />
116  </testsuite>
117  <testsuite name="TypedTest/1" tests="1" failures="0" disabled="0" errors="0" time="*">
118    <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/1" />
119  </testsuite>
120  <testsuite name="Single/TypeParameterizedTestCase/0" tests="1" failures="0" disabled="0" errors="0" time="*">
121    <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/0" />
122  </testsuite>
123  <testsuite name="Single/TypeParameterizedTestCase/1" tests="1" failures="0" disabled="0" errors="0" time="*">
124    <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/1" />
125  </testsuite>
126</testsuites>""" % {'stack': STACK_TRACE_TEMPLATE}
127
128
129EXPECTED_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
130<testsuites tests="0" failures="0" disabled="0" errors="0" time="*" name="AllTests">
131</testsuites>"""
132
133
134class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
135  """
136  Unit test for Google Test's XML output functionality.
137  """
138
139  def testNonEmptyXmlOutput(self):
140    """
141    Runs a test program that generates a non-empty XML output, and
142    tests that the XML output is expected.
143    """
144    self._TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY_XML, 1)
145
146  def testEmptyXmlOutput(self):
147    """
148    Runs a test program that generates an empty XML output, and
149    tests that the XML output is expected.
150    """
151
152    self._TestXmlOutput("gtest_no_test_unittest",
153                        EXPECTED_EMPTY_XML, 0)
154
155  def testDefaultOutputFile(self):
156    """
157    Confirms that Google Test produces an XML output file with the expected
158    default name if no name is explicitly specified.
159    """
160    output_file = os.path.join(gtest_test_utils.GetTempDir(),
161                               GTEST_DEFAULT_OUTPUT_FILE)
162    gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
163        "gtest_no_test_unittest")
164    try:
165      os.remove(output_file)
166    except OSError, e:
167      if e.errno != errno.ENOENT:
168        raise
169
170    p = gtest_test_utils.Subprocess(
171        [gtest_prog_path, "%s=xml" % GTEST_OUTPUT_FLAG],
172        working_dir=gtest_test_utils.GetTempDir())
173    self.assert_(p.exited)
174    self.assertEquals(0, p.exit_code)
175    self.assert_(os.path.isfile(output_file))
176
177  def testSuppressedXmlOutput(self):
178    """
179    Tests that no XML file is generated if the default XML listener is
180    shut down before RUN_ALL_TESTS is invoked.
181    """
182
183    xml_path = os.path.join(gtest_test_utils.GetTempDir(),
184                            GTEST_PROGRAM_NAME + "out.xml")
185    if os.path.isfile(xml_path):
186      os.remove(xml_path)
187
188    gtest_prog_path = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
189
190    command = [gtest_prog_path,
191               "%s=xml:%s" % (GTEST_OUTPUT_FLAG, xml_path),
192               "--shut_down_xml"]
193    p = gtest_test_utils.Subprocess(command)
194    if p.terminated_by_signal:
195      self.assert_(False,
196                   "%s was killed by signal %d" % (gtest_prog_name, p.signal))
197    else:
198      self.assert_(p.exited)
199      self.assertEquals(1, p.exit_code,
200                        "'%s' exited with code %s, which doesn't match "
201                        "the expected exit code %s."
202                        % (command, p.exit_code, 1))
203
204    self.assert_(not os.path.isfile(xml_path))
205
206
207  def _TestXmlOutput(self, gtest_prog_name, expected_xml, expected_exit_code):
208    """
209    Asserts that the XML document generated by running the program
210    gtest_prog_name matches expected_xml, a string containing another
211    XML document.  Furthermore, the program's exit code must be
212    expected_exit_code.
213    """
214    xml_path = os.path.join(gtest_test_utils.GetTempDir(),
215                            gtest_prog_name + "out.xml")
216    gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
217
218    command = [gtest_prog_path, "%s=xml:%s" % (GTEST_OUTPUT_FLAG, xml_path)]
219    p = gtest_test_utils.Subprocess(command)
220    if p.terminated_by_signal:
221      self.assert_(False,
222                   "%s was killed by signal %d" % (gtest_prog_name, p.signal))
223    else:
224      self.assert_(p.exited)
225      self.assertEquals(expected_exit_code, p.exit_code,
226                        "'%s' exited with code %s, which doesn't match "
227                        "the expected exit code %s."
228                        % (command, p.exit_code, expected_exit_code))
229
230    expected = minidom.parseString(expected_xml)
231    actual   = minidom.parse(xml_path)
232    self.NormalizeXml(actual.documentElement)
233    self.AssertEquivalentNodes(expected.documentElement,
234                               actual.documentElement)
235    expected.unlink()
236    actual  .unlink()
237
238
239
240if __name__ == '__main__':
241  os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
242  gtest_test_utils.Main()
243