32 """Unit test for the gtest_xml_output module"""
34 __author__ =
'eefacm@gmail.com (Sean Mcafee)'
41 from xml.dom
import minidom, Node
43 import gtest_test_utils
44 import gtest_xml_test_utils
47 GTEST_FILTER_FLAG =
'--gtest_filter'
48 GTEST_LIST_TESTS_FLAG =
'--gtest_list_tests'
49 GTEST_OUTPUT_FLAG =
"--gtest_output"
50 GTEST_DEFAULT_OUTPUT_FILE =
"test_detail.xml"
51 GTEST_PROGRAM_NAME =
"gtest_xml_output_unittest_"
53 SUPPORTS_STACK_TRACES =
False
55 if SUPPORTS_STACK_TRACES:
56 STACK_TRACE_TEMPLATE =
'\nStack trace:\n*'
58 STACK_TRACE_TEMPLATE =
''
60 EXPECTED_NON_EMPTY_XML =
"""<?xml version="1.0" encoding="UTF-8"?>
61 <testsuites tests="23" failures="4" disabled="2" errors="0" time="*" timestamp="*" name="AllTests" ad_hoc_property="42">
62 <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" errors="0" time="*">
63 <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
65 <testsuite name="FailedTest" tests="1" failures="1" disabled="0" errors="0" time="*">
66 <testcase name="Fails" status="run" time="*" classname="FailedTest">
67 <failure message="gtest_xml_output_unittest_.cc:*
 Expected: 1
To be equal to: 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
69 To be equal to: 2%(stack)s]]></failure>
72 <testsuite name="MixedResultTest" tests="3" failures="1" disabled="1" errors="0" time="*">
73 <testcase name="Succeeds" status="run" time="*" classname="MixedResultTest"/>
74 <testcase name="Fails" status="run" time="*" classname="MixedResultTest">
75 <failure message="gtest_xml_output_unittest_.cc:*
 Expected: 1
To be equal to: 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
77 To be equal to: 2%(stack)s]]></failure>
78 <failure message="gtest_xml_output_unittest_.cc:*
 Expected: 2
To be equal to: 3" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
80 To be equal to: 3%(stack)s]]></failure>
82 <testcase name="DISABLED_test" status="notrun" time="*" classname="MixedResultTest"/>
84 <testsuite name="XmlQuotingTest" tests="1" failures="1" disabled="0" errors="0" time="*">
85 <testcase name="OutputsCData" status="run" time="*" classname="XmlQuotingTest">
86 <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:*
88 XML output: <?xml encoding="utf-8"><top><![CDATA[cdata text]]>]]><![CDATA[</top>%(stack)s]]></failure>
91 <testsuite name="InvalidCharactersTest" tests="1" failures="1" disabled="0" errors="0" time="*">
92 <testcase name="InvalidCharactersInMessage" status="run" time="*" classname="InvalidCharactersTest">
93 <failure message="gtest_xml_output_unittest_.cc:*
Failed
Invalid characters in brackets []" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
95 Invalid characters in brackets []%(stack)s]]></failure>
98 <testsuite name="DisabledTest" tests="1" failures="0" disabled="1" errors="0" time="*">
99 <testcase name="DISABLED_test_not_run" status="notrun" time="*" classname="DisabledTest"/>
101 <testsuite name="PropertyRecordingTest" tests="4" failures="0" disabled="0" errors="0" time="*" SetUpTestCase="yes" TearDownTestCase="aye">
102 <testcase name="OneProperty" status="run" time="*" classname="PropertyRecordingTest" key_1="1"/>
103 <testcase name="IntValuedProperty" status="run" time="*" classname="PropertyRecordingTest" key_int="1"/>
104 <testcase name="ThreeProperties" status="run" time="*" classname="PropertyRecordingTest" key_1="1" key_2="2" key_3="3"/>
105 <testcase name="TwoValuesForOneKeyUsesLastValue" status="run" time="*" classname="PropertyRecordingTest" key_1="2"/>
107 <testsuite name="NoFixtureTest" tests="3" failures="0" disabled="0" errors="0" time="*">
108 <testcase name="RecordProperty" status="run" time="*" classname="NoFixtureTest" key="1"/>
109 <testcase name="ExternalUtilityThatCallsRecordIntValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_int="1"/>
110 <testcase name="ExternalUtilityThatCallsRecordStringValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_string="1"/>
112 <testsuite name="Single/ValueParamTest" tests="4" failures="0" disabled="0" errors="0" time="*">
113 <testcase name="HasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
114 <testcase name="HasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
115 <testcase name="AnotherTestThatHasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
116 <testcase name="AnotherTestThatHasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
118 <testsuite name="TypedTest/0" tests="1" failures="0" disabled="0" errors="0" time="*">
119 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/0" />
121 <testsuite name="TypedTest/1" tests="1" failures="0" disabled="0" errors="0" time="*">
122 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/1" />
124 <testsuite name="Single/TypeParameterizedTestCase/0" tests="1" failures="0" disabled="0" errors="0" time="*">
125 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/0" />
127 <testsuite name="Single/TypeParameterizedTestCase/1" tests="1" failures="0" disabled="0" errors="0" time="*">
128 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/1" />
130 </testsuites>""" % {
'stack': STACK_TRACE_TEMPLATE}
132 EXPECTED_FILTERED_TEST_XML =
"""<?xml version="1.0" encoding="UTF-8"?>
133 <testsuites tests="1" failures="0" disabled="0" errors="0" time="*"
134 timestamp="*" name="AllTests" ad_hoc_property="42">
135 <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0"
137 <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
141 EXPECTED_EMPTY_XML =
"""<?xml version="1.0" encoding="UTF-8"?>
142 <testsuites tests="0" failures="0" disabled="0" errors="0" time="*"
143 timestamp="*" name="AllTests">
149 [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=
False).output
154 Unit test for Google Test's XML output functionality.
159 if SUPPORTS_TYPED_TESTS:
162 Runs a test program that generates a non-empty XML output, and
163 tests that the XML output is expected.
165 self.
_TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY_XML, 1)
168 """Verifies XML output for a Google Test binary without actual tests.
170 Runs a test program that generates an empty XML output, and
171 tests that the XML output is expected.
174 self.
_TestXmlOutput(
'gtest_no_test_unittest', EXPECTED_EMPTY_XML, 0)
177 """Checks whether the timestamp attribute in the XML output is valid.
179 Runs a test program that generates an empty XML output, and checks if
180 the timestamp attribute in the testsuites tag is valid.
183 date_time_str = actual.documentElement.getAttributeNode(
'timestamp').value
186 match = re.match(
r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
189 'XML datettime string %s has incorrect format' % date_time_str)
190 date_time_from_xml = datetime.datetime(
191 year=int(match.group(1)), month=int(match.group(2)),
192 day=int(match.group(3)), hour=int(match.group(4)),
193 minute=int(match.group(5)), second=int(match.group(6)))
195 time_delta = abs(datetime.datetime.now() - date_time_from_xml)
197 self.assertTrue(time_delta < datetime.timedelta(seconds=600),
198 'time_delta is %s' % time_delta)
203 Confirms that Google Test produces an XML output file with the expected
204 default name if no name is explicitly specified.
207 GTEST_DEFAULT_OUTPUT_FILE)
209 'gtest_no_test_unittest')
211 os.remove(output_file)
213 e = sys.exc_info()[1]
214 if e.errno != errno.ENOENT:
218 [gtest_prog_path,
'%s=xml' % GTEST_OUTPUT_FLAG],
220 self.assert_(p.exited)
221 self.assertEquals(0, p.exit_code)
222 self.assert_(os.path.isfile(output_file))
226 Tests that no XML file is generated if the default XML listener is
227 shut down before RUN_ALL_TESTS is invoked.
231 GTEST_PROGRAM_NAME +
'out.xml')
232 if os.path.isfile(xml_path):
235 command = [GTEST_PROGRAM_PATH,
236 '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path),
239 if p.terminated_by_signal:
242 p.terminated_by_signal,
243 '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal))
245 self.assert_(p.exited)
246 self.assertEquals(1, p.exit_code,
247 "'%s' exited with code %s, which doesn't match "
248 'the expected exit code %s.'
249 % (command, p.exit_code, 1))
251 self.assert_(
not os.path.isfile(xml_path))
254 """Verifies XML output when a filter is applied.
256 Runs a test program that executes only some tests and verifies that
257 non-selected tests do not show up in the XML output.
260 self.
_TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_FILTERED_TEST_XML, 0,
261 extra_args=[
'%s=SuccessfulTest.*' % GTEST_FILTER_FLAG])
265 Returns the xml output generated by running the program gtest_prog_name.
266 Furthermore, the program's exit code must be expected_exit_code.
269 gtest_prog_name +
'out.xml')
272 command = ([gtest_prog_path,
'%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path)] +
275 if p.terminated_by_signal:
277 '%s was killed by signal %d' % (gtest_prog_name, p.signal))
279 self.assert_(p.exited)
280 self.assertEquals(expected_exit_code, p.exit_code,
281 "'%s' exited with code %s, which doesn't match "
282 'the expected exit code %s.'
283 % (command, p.exit_code, expected_exit_code))
284 actual = minidom.parse(xml_path)
288 expected_exit_code, extra_args=
None):
290 Asserts that the XML document generated by running the program
291 gtest_prog_name matches expected_xml, a string containing another
292 XML document. Furthermore, the program's exit code must be
296 actual = self.
_GetXmlOutput(gtest_prog_name, extra_args
or [],
298 expected = minidom.parseString(expected_xml)
301 actual.documentElement)
306 if __name__ ==
'__main__':
307 os.environ[
'GTEST_STACK_TRACE_DEPTH'] =
'1'