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:*
Value of: 2
Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:* 69 Expected: 1%(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:*
Value of: 2
Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:* 77 Expected: 1%(stack)s]]></failure> 78 <failure message="gtest_xml_output_unittest_.cc:*
Value of: 3
Expected: 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:* 80 Expected: 2%(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 if e.errno != errno.ENOENT:
217 [gtest_prog_path,
'%s=xml' % GTEST_OUTPUT_FLAG],
219 self.assertTrue(p.exited)
220 self.assertEqual(0, p.exit_code)
221 self.assertTrue(os.path.isfile(output_file))
225 Tests that no XML file is generated if the default XML listener is 226 shut down before RUN_ALL_TESTS is invoked. 230 GTEST_PROGRAM_NAME +
'out.xml')
231 if os.path.isfile(xml_path):
234 command = [GTEST_PROGRAM_PATH,
235 '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path),
238 if p.terminated_by_signal:
241 p.terminated_by_signal,
242 '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal))
244 self.assertTrue(p.exited)
245 self.assertEqual(1, p.exit_code,
246 "'%s' exited with code %s, which doesn't match " 247 'the expected exit code %s.' 248 % (command, p.exit_code, 1))
250 self.assertTrue(
not os.path.isfile(xml_path))
253 """Verifies XML output when a filter is applied. 255 Runs a test program that executes only some tests and verifies that 256 non-selected tests do not show up in the XML output. 259 self.
_TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_FILTERED_TEST_XML, 0,
260 extra_args=[
'%s=SuccessfulTest.*' % GTEST_FILTER_FLAG])
264 Returns the xml output generated by running the program gtest_prog_name. 265 Furthermore, the program's exit code must be expected_exit_code. 268 gtest_prog_name +
'out.xml')
271 command = ([gtest_prog_path,
'%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path)] +
274 if p.terminated_by_signal:
275 self.assertTrue(
False,
276 '%s was killed by signal %d' % (gtest_prog_name, p.signal))
278 self.assertTrue(p.exited)
279 self.assertEqual(expected_exit_code, p.exit_code,
280 "'%s' exited with code %s, which doesn't match " 281 'the expected exit code %s.' 282 % (command, p.exit_code, expected_exit_code))
283 actual = minidom.parse(xml_path)
287 expected_exit_code, extra_args=
None):
289 Asserts that the XML document generated by running the program 290 gtest_prog_name matches expected_xml, a string containing another 291 XML document. Furthermore, the program's exit code must be 295 actual = self.
_GetXmlOutput(gtest_prog_name, extra_args
or [],
297 expected = minidom.parseString(expected_xml)
300 actual.documentElement)
305 if __name__ ==
'__main__':
306 os.environ[
'GTEST_STACK_TRACE_DEPTH'] =
'1' def GetTestExecutablePath(executable_name, build_dir=None)
def AssertEquivalentNodes(self, expected_node, actual_node)
def testNonEmptyXmlOutput(self)
def testSuppressedXmlOutput(self)
def _TestXmlOutput(self, gtest_prog_name, expected_xml, expected_exit_code, extra_args=None)
def testFilteredTestXmlOutput(self)
def _GetXmlOutput(self, gtest_prog_name, extra_args, expected_exit_code)
def testDefaultOutputFile(self)
def NormalizeXml(self, element)
def testEmptyXmlOutput(self)
def testTimestampValue(self)