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"""
39 from xml
.dom
import minidom
, Node
41 import gtest_test_utils
42 import gtest_xml_test_utils
44 GTEST_FILTER_FLAG
= '--gtest_filter'
45 GTEST_LIST_TESTS_FLAG
= '--gtest_list_tests'
46 GTEST_OUTPUT_FLAG
= '--gtest_output'
47 GTEST_DEFAULT_OUTPUT_FILE
= 'test_detail.xml'
48 GTEST_PROGRAM_NAME
= 'gtest_xml_output_unittest_'
50 # The environment variables for test sharding.
51 TOTAL_SHARDS_ENV_VAR
= 'GTEST_TOTAL_SHARDS'
52 SHARD_INDEX_ENV_VAR
= 'GTEST_SHARD_INDEX'
53 SHARD_STATUS_FILE_ENV_VAR
= 'GTEST_SHARD_STATUS_FILE'
55 SUPPORTS_STACK_TRACES
= False
57 if SUPPORTS_STACK_TRACES
:
58 STACK_TRACE_TEMPLATE
= '\nStack trace:\n*'
60 STACK_TRACE_TEMPLATE
= ''
62 EXPECTED_NON_EMPTY_XML
= """<?xml version="1.0" encoding="UTF-8"?>
63 <testsuites tests="23" failures="4" disabled="2" errors="0" time="*" timestamp="*" name="AllTests" ad_hoc_property="42">
64 <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" errors="0" time="*">
65 <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
67 <testsuite name="FailedTest" tests="1" failures="1" disabled="0" errors="0" time="*">
68 <testcase name="Fails" status="run" time="*" classname="FailedTest">
69 <failure message="gtest_xml_output_unittest_.cc:*
Expected equality of these values:
 1
 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
70 Expected equality of these values:
72 2%(stack)s]]></failure>
75 <testsuite name="MixedResultTest" tests="3" failures="1" disabled="1" errors="0" time="*">
76 <testcase name="Succeeds" status="run" time="*" classname="MixedResultTest"/>
77 <testcase name="Fails" status="run" time="*" classname="MixedResultTest">
78 <failure message="gtest_xml_output_unittest_.cc:*
Expected equality of these values:
 1
 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
79 Expected equality of these values:
81 2%(stack)s]]></failure>
82 <failure message="gtest_xml_output_unittest_.cc:*
Expected equality of these values:
 2
 3" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
83 Expected equality of these values:
85 3%(stack)s]]></failure>
87 <testcase name="DISABLED_test" status="notrun" time="*" classname="MixedResultTest"/>
89 <testsuite name="XmlQuotingTest" tests="1" failures="1" disabled="0" errors="0" time="*">
90 <testcase name="OutputsCData" status="run" time="*" classname="XmlQuotingTest">
91 <failure message="gtest_xml_output_unittest_.cc:*
Failed
XML output: <?xml encoding="utf-8"><top><![CDATA[cdata text]]></top>" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
93 XML output: <?xml encoding="utf-8"><top><![CDATA[cdata text]]>]]><![CDATA[</top>%(stack)s]]></failure>
96 <testsuite name="InvalidCharactersTest" tests="1" failures="1" disabled="0" errors="0" time="*">
97 <testcase name="InvalidCharactersInMessage" status="run" time="*" classname="InvalidCharactersTest">
98 <failure message="gtest_xml_output_unittest_.cc:*
Failed
Invalid characters in brackets []" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
100 Invalid characters in brackets []%(stack)s]]></failure>
103 <testsuite name="DisabledTest" tests="1" failures="0" disabled="1" errors="0" time="*">
104 <testcase name="DISABLED_test_not_run" status="notrun" time="*" classname="DisabledTest"/>
106 <testsuite name="PropertyRecordingTest" tests="4" failures="0" disabled="0" errors="0" time="*" SetUpTestCase="yes" TearDownTestCase="aye">
107 <testcase name="OneProperty" status="run" time="*" classname="PropertyRecordingTest">
109 <property name="key_1" value="1"/>
112 <testcase name="IntValuedProperty" status="run" time="*" classname="PropertyRecordingTest">
114 <property name="key_int" value="1"/>
117 <testcase name="ThreeProperties" status="run" time="*" classname="PropertyRecordingTest">
119 <property name="key_1" value="1"/>
120 <property name="key_2" value="2"/>
121 <property name="key_3" value="3"/>
124 <testcase name="TwoValuesForOneKeyUsesLastValue" status="run" time="*" classname="PropertyRecordingTest">
126 <property name="key_1" value="2"/>
130 <testsuite name="NoFixtureTest" tests="3" failures="0" disabled="0" errors="0" time="*">
131 <testcase name="RecordProperty" status="run" time="*" classname="NoFixtureTest">
133 <property name="key" value="1"/>
136 <testcase name="ExternalUtilityThatCallsRecordIntValuedProperty" status="run" time="*" classname="NoFixtureTest">
138 <property name="key_for_utility_int" value="1"/>
141 <testcase name="ExternalUtilityThatCallsRecordStringValuedProperty" status="run" time="*" classname="NoFixtureTest">
143 <property name="key_for_utility_string" value="1"/>
147 <testsuite name="Single/ValueParamTest" tests="4" failures="0" disabled="0" errors="0" time="*">
148 <testcase name="HasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
149 <testcase name="HasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
150 <testcase name="AnotherTestThatHasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
151 <testcase name="AnotherTestThatHasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
153 <testsuite name="TypedTest/0" tests="1" failures="0" disabled="0" errors="0" time="*">
154 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/0" />
156 <testsuite name="TypedTest/1" tests="1" failures="0" disabled="0" errors="0" time="*">
157 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/1" />
159 <testsuite name="Single/TypeParameterizedTestCase/0" tests="1" failures="0" disabled="0" errors="0" time="*">
160 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/0" />
162 <testsuite name="Single/TypeParameterizedTestCase/1" tests="1" failures="0" disabled="0" errors="0" time="*">
163 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/1" />
165 </testsuites>""" % {'stack': STACK_TRACE_TEMPLATE
}
167 EXPECTED_FILTERED_TEST_XML
= """<?xml version="1.0" encoding="UTF-8"?>
168 <testsuites tests="1" failures="0" disabled="0" errors="0" time="*"
169 timestamp="*" name="AllTests" ad_hoc_property="42">
170 <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0"
172 <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
176 EXPECTED_SHARDED_TEST_XML
= """<?xml version="1.0" encoding="UTF-8"?>
177 <testsuites tests="3" failures="0" disabled="0" errors="0" time="*" timestamp="*" name="AllTests" ad_hoc_property="42">
178 <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" errors="0" time="*">
179 <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
181 <testsuite name="NoFixtureTest" tests="1" failures="0" disabled="0" errors="0" time="*">
182 <testcase name="RecordProperty" status="run" time="*" classname="NoFixtureTest">
184 <property name="key" value="1"/>
188 <testsuite name="Single/ValueParamTest" tests="1" failures="0" disabled="0" errors="0" time="*">
189 <testcase name="AnotherTestThatHasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
193 EXPECTED_EMPTY_XML
= """<?xml version="1.0" encoding="UTF-8"?>
194 <testsuites tests="0" failures="0" disabled="0" errors="0" time="*"
195 timestamp="*" name="AllTests">
198 GTEST_PROGRAM_PATH
= gtest_test_utils
.GetTestExecutablePath(GTEST_PROGRAM_NAME
)
200 SUPPORTS_TYPED_TESTS
= 'TypedTest' in gtest_test_utils
.Subprocess(
201 [GTEST_PROGRAM_PATH
, GTEST_LIST_TESTS_FLAG
], capture_stderr
=False).output
204 class GTestXMLOutputUnitTest(gtest_xml_test_utils
.GTestXMLTestCase
):
206 Unit test for Google Test's XML output functionality.
209 # This test currently breaks on platforms that do not support typed and
210 # type-parameterized tests, so we don't run it under them.
211 if SUPPORTS_TYPED_TESTS
:
212 def testNonEmptyXmlOutput(self
):
214 Runs a test program that generates a non-empty XML output, and
215 tests that the XML output is expected.
217 self
._TestXmlOutput
(GTEST_PROGRAM_NAME
, EXPECTED_NON_EMPTY_XML
, 1)
219 def testEmptyXmlOutput(self
):
220 """Verifies XML output for a Google Test binary without actual tests.
222 Runs a test program that generates an empty XML output, and
223 tests that the XML output is expected.
226 self
._TestXmlOutput
('gtest_no_test_unittest', EXPECTED_EMPTY_XML
, 0)
228 def testTimestampValue(self
):
229 """Checks whether the timestamp attribute in the XML output is valid.
231 Runs a test program that generates an empty XML output, and checks if
232 the timestamp attribute in the testsuites tag is valid.
234 actual
= self
._GetXmlOutput
('gtest_no_test_unittest', [], {}, 0)
235 date_time_str
= actual
.documentElement
.getAttributeNode('timestamp').value
236 # datetime.strptime() is only available in Python 2.5+ so we have to
237 # parse the expected datetime manually.
238 match
= re
.match(r
'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str
)
241 'XML datettime string %s has incorrect format' % date_time_str
)
242 date_time_from_xml
= datetime
.datetime(
243 year
=int(match
.group(1)), month
=int(match
.group(2)),
244 day
=int(match
.group(3)), hour
=int(match
.group(4)),
245 minute
=int(match
.group(5)), second
=int(match
.group(6)))
247 time_delta
= abs(datetime
.datetime
.now() - date_time_from_xml
)
248 # timestamp value should be near the current local time
249 self
.assertTrue(time_delta
< datetime
.timedelta(seconds
=600),
250 'time_delta is %s' % time_delta
)
253 def testDefaultOutputFile(self
):
255 Confirms that Google Test produces an XML output file with the expected
256 default name if no name is explicitly specified.
258 output_file
= os
.path
.join(gtest_test_utils
.GetTempDir(),
259 GTEST_DEFAULT_OUTPUT_FILE
)
260 gtest_prog_path
= gtest_test_utils
.GetTestExecutablePath(
261 'gtest_no_test_unittest')
263 os
.remove(output_file
)
265 if e
.errno
!= errno
.ENOENT
:
268 p
= gtest_test_utils
.Subprocess(
269 [gtest_prog_path
, '%s=xml' % GTEST_OUTPUT_FLAG
],
270 working_dir
=gtest_test_utils
.GetTempDir())
271 self
.assert_(p
.exited
)
272 self
.assertEquals(0, p
.exit_code
)
273 self
.assert_(os
.path
.isfile(output_file
))
275 def testSuppressedXmlOutput(self
):
277 Tests that no XML file is generated if the default XML listener is
278 shut down before RUN_ALL_TESTS is invoked.
281 xml_path
= os
.path
.join(gtest_test_utils
.GetTempDir(),
282 GTEST_PROGRAM_NAME
+ 'out.xml')
283 if os
.path
.isfile(xml_path
):
286 command
= [GTEST_PROGRAM_PATH
,
287 '%s=xml:%s' % (GTEST_OUTPUT_FLAG
, xml_path
),
289 p
= gtest_test_utils
.Subprocess(command
)
290 if p
.terminated_by_signal
:
291 # p.signal is available only if p.terminated_by_signal is True.
293 p
.terminated_by_signal
,
294 '%s was killed by signal %d' % (GTEST_PROGRAM_NAME
, p
.signal
))
296 self
.assert_(p
.exited
)
297 self
.assertEquals(1, p
.exit_code
,
298 "'%s' exited with code %s, which doesn't match "
299 'the expected exit code %s.'
300 % (command
, p
.exit_code
, 1))
302 self
.assert_(not os
.path
.isfile(xml_path
))
304 def testFilteredTestXmlOutput(self
):
305 """Verifies XML output when a filter is applied.
307 Runs a test program that executes only some tests and verifies that
308 non-selected tests do not show up in the XML output.
311 self
._TestXmlOutput
(GTEST_PROGRAM_NAME
, EXPECTED_FILTERED_TEST_XML
, 0,
312 extra_args
=['%s=SuccessfulTest.*' % GTEST_FILTER_FLAG
])
314 def testShardedTestXmlOutput(self
):
315 """Verifies XML output when run using multiple shards.
317 Runs a test program that executes only one shard and verifies that tests
318 from other shards do not show up in the XML output.
323 EXPECTED_SHARDED_TEST_XML
,
325 extra_env
={SHARD_INDEX_ENV_VAR
: '0',
326 TOTAL_SHARDS_ENV_VAR
: '10'})
328 def _GetXmlOutput(self
, gtest_prog_name
, extra_args
, extra_env
,
331 Returns the xml output generated by running the program gtest_prog_name.
332 Furthermore, the program's exit code must be expected_exit_code.
334 xml_path
= os
.path
.join(gtest_test_utils
.GetTempDir(),
335 gtest_prog_name
+ 'out.xml')
336 gtest_prog_path
= gtest_test_utils
.GetTestExecutablePath(gtest_prog_name
)
338 command
= ([gtest_prog_path
, '%s=xml:%s' % (GTEST_OUTPUT_FLAG
, xml_path
)] +
340 environ_copy
= os
.environ
.copy()
342 environ_copy
.update(extra_env
)
343 p
= gtest_test_utils
.Subprocess(command
, env
=environ_copy
)
345 if p
.terminated_by_signal
:
347 '%s was killed by signal %d' % (gtest_prog_name
, p
.signal
))
349 self
.assert_(p
.exited
)
350 self
.assertEquals(expected_exit_code
, p
.exit_code
,
351 "'%s' exited with code %s, which doesn't match "
352 'the expected exit code %s.'
353 % (command
, p
.exit_code
, expected_exit_code
))
354 actual
= minidom
.parse(xml_path
)
357 def _TestXmlOutput(self
, gtest_prog_name
, expected_xml
,
358 expected_exit_code
, extra_args
=None, extra_env
=None):
360 Asserts that the XML document generated by running the program
361 gtest_prog_name matches expected_xml, a string containing another
362 XML document. Furthermore, the program's exit code must be
366 actual
= self
._GetXmlOutput
(gtest_prog_name
, extra_args
or [],
367 extra_env
or {}, expected_exit_code
)
368 expected
= minidom
.parseString(expected_xml
)
369 self
.NormalizeXml(actual
.documentElement
)
370 self
.AssertEquivalentNodes(expected
.documentElement
,
371 actual
.documentElement
)
376 if __name__
== '__main__':
377 os
.environ
['GTEST_STACK_TRACE_DEPTH'] = '1'
378 gtest_test_utils
.Main()