3 # Copyright 2006, Google Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
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
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.
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.
32 """Unit test for the gtest_xml_output module"""
34 __author__ = 'eefacm@gmail.com (Sean Mcafee)'
39 from xml.dom import minidom, Node
41 import gtest_test_utils
42 import gtest_xml_test_utils
45 GTEST_OUTPUT_FLAG = "--gtest_output"
46 GTEST_DEFAULT_OUTPUT_FILE = "test_detail.xml"
47 GTEST_PROGRAM_NAME = "gtest_xml_output_unittest_"
49 SUPPORTS_STACK_TRACES = False
51 if SUPPORTS_STACK_TRACES:
52 STACK_TRACE_TEMPLATE = "\nStack trace:\n*"
54 STACK_TRACE_TEMPLATE = ""
56 EXPECTED_NON_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
57 <testsuites tests="15" 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"/>
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
Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
65 Expected: 1%(stack)s]]></failure>
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
Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
73 Expected: 1%(stack)s]]></failure>
74 <failure message="Value of: 3
Expected: 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
76 Expected: 2%(stack)s]]></failure>
78 <testcase name="DISABLED_test" status="notrun" time="*" classname="MixedResultTest"/>
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
XML output: <?xml encoding="utf-8"><top><![CDATA[cdata text]]></top>" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
84 XML output: <?xml encoding="utf-8"><top><![CDATA[cdata text]]>]]><![CDATA[</top>%(stack)s]]></failure>
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
Invalid characters in brackets []" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
91 Invalid characters in brackets []%(stack)s]]></failure>
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"/>
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"/>
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"/>
108 </testsuites>""" % {'stack': STACK_TRACE_TEMPLATE}
111 EXPECTED_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
112 <testsuites tests="0" failures="0" disabled="0" errors="0" time="*" name="AllTests">
116 class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
118 Unit test for Google Test's XML output functionality.
121 def testNonEmptyXmlOutput(self):
123 Runs a test program that generates a non-empty XML output, and
124 tests that the XML output is expected.
126 self._TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY_XML, 1)
128 def testEmptyXmlOutput(self):
130 Runs a test program that generates an empty XML output, and
131 tests that the XML output is expected.
134 self._TestXmlOutput("gtest_no_test_unittest",
135 EXPECTED_EMPTY_XML, 0)
137 def testDefaultOutputFile(self):
139 Confirms that Google Test produces an XML output file with the expected
140 default name if no name is explicitly specified.
142 output_file = os.path.join(gtest_test_utils.GetTempDir(),
143 GTEST_DEFAULT_OUTPUT_FILE)
144 gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
145 "gtest_no_test_unittest")
147 os.remove(output_file)
149 if e.errno != errno.ENOENT:
152 p = gtest_test_utils.Subprocess(
153 [gtest_prog_path, "%s=xml" % GTEST_OUTPUT_FLAG],
154 working_dir=gtest_test_utils.GetTempDir())
155 self.assert_(p.exited)
156 self.assertEquals(0, p.exit_code)
157 self.assert_(os.path.isfile(output_file))
159 def testSuppressedXmlOutput(self):
161 Tests that no XML file is generated if the default XML listener is
162 shut down before RUN_ALL_TESTS is invoked.
165 xml_path = os.path.join(gtest_test_utils.GetTempDir(),
166 GTEST_PROGRAM_NAME + "out.xml")
167 if os.path.isfile(xml_path):
170 gtest_prog_path = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
172 command = [gtest_prog_path,
173 "%s=xml:%s" % (GTEST_OUTPUT_FLAG, xml_path),
175 p = gtest_test_utils.Subprocess(command)
176 if p.terminated_by_signal:
178 "%s was killed by signal %d" % (gtest_prog_name, p.signal))
180 self.assert_(p.exited)
181 self.assertEquals(1, p.exit_code,
182 "'%s' exited with code %s, which doesn't match "
183 "the expected exit code %s."
184 % (command, p.exit_code, 1))
186 self.assert_(not os.path.isfile(xml_path))
189 def _TestXmlOutput(self, gtest_prog_name, expected_xml, expected_exit_code):
191 Asserts that the XML document generated by running the program
192 gtest_prog_name matches expected_xml, a string containing another
193 XML document. Furthermore, the program's exit code must be
196 xml_path = os.path.join(gtest_test_utils.GetTempDir(),
197 gtest_prog_name + "out.xml")
198 gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
200 command = [gtest_prog_path, "%s=xml:%s" % (GTEST_OUTPUT_FLAG, xml_path)]
201 p = gtest_test_utils.Subprocess(command)
202 if p.terminated_by_signal:
204 "%s was killed by signal %d" % (gtest_prog_name, p.signal))
206 self.assert_(p.exited)
207 self.assertEquals(expected_exit_code, p.exit_code,
208 "'%s' exited with code %s, which doesn't match "
209 "the expected exit code %s."
210 % (command, p.exit_code, expected_exit_code))
212 expected = minidom.parseString(expected_xml)
213 actual = minidom.parse(xml_path)
214 self.NormalizeXml(actual.documentElement)
215 self.AssertEquivalentNodes(expected.documentElement,
216 actual.documentElement)
222 if __name__ == '__main__':
223 os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
224 gtest_test_utils.Main()