gtest_xml_output_unittest.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. """Unit test for the gtest_xml_output module"""
  32. import datetime
  33. import errno
  34. import os
  35. import re
  36. import sys
  37. from xml.dom import minidom, Node
  38. from googletest.test import gtest_test_utils
  39. from googletest.test import gtest_xml_test_utils
  40. GTEST_FILTER_FLAG = '--gtest_filter'
  41. GTEST_LIST_TESTS_FLAG = '--gtest_list_tests'
  42. GTEST_OUTPUT_FLAG = '--gtest_output'
  43. GTEST_DEFAULT_OUTPUT_FILE = 'test_detail.xml'
  44. GTEST_PROGRAM_NAME = 'gtest_xml_output_unittest_'
  45. # The flag indicating stacktraces are not supported
  46. NO_STACKTRACE_SUPPORT_FLAG = '--no_stacktrace_support'
  47. # The environment variables for test sharding.
  48. TOTAL_SHARDS_ENV_VAR = 'GTEST_TOTAL_SHARDS'
  49. SHARD_INDEX_ENV_VAR = 'GTEST_SHARD_INDEX'
  50. SHARD_STATUS_FILE_ENV_VAR = 'GTEST_SHARD_STATUS_FILE'
  51. SUPPORTS_STACK_TRACES = NO_STACKTRACE_SUPPORT_FLAG not in sys.argv
  52. if SUPPORTS_STACK_TRACES:
  53. STACK_TRACE_TEMPLATE = '\nStack trace:\n*'
  54. STACK_TRACE_ENTITY_TEMPLATE = ''
  55. else:
  56. STACK_TRACE_TEMPLATE = '\n'
  57. STACK_TRACE_ENTITY_TEMPLATE = '
'
  58. # unittest.main() can't handle unknown flags
  59. sys.argv.remove(NO_STACKTRACE_SUPPORT_FLAG)
  60. EXPECTED_NON_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
  61. <testsuites tests="26" failures="5" disabled="2" errors="0" time="*" timestamp="*" name="AllTests" ad_hoc_property="42">
  62. <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" skipped="0" errors="0" time="*" timestamp="*">
  63. <testcase name="Succeeds" file="gtest_xml_output_unittest_.cc" line="53" status="run" result="completed" time="*" timestamp="*" classname="SuccessfulTest"/>
  64. </testsuite>
  65. <testsuite name="FailedTest" tests="1" failures="1" disabled="0" skipped="0" errors="0" time="*" timestamp="*">
  66. <testcase name="Fails" file="gtest_xml_output_unittest_.cc" line="61" status="run" result="completed" time="*" timestamp="*" classname="FailedTest">
  67. <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Expected equality of these values:&#x0A; 1&#x0A; 2%(stack_entity)s" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
  68. Expected equality of these values:
  69. 1
  70. 2%(stack)s]]></failure>
  71. </testcase>
  72. </testsuite>
  73. <testsuite name="MixedResultTest" tests="3" failures="1" disabled="1" skipped="0" errors="0" time="*" timestamp="*">
  74. <testcase name="Succeeds" file="gtest_xml_output_unittest_.cc" line="88" status="run" result="completed" time="*" timestamp="*" classname="MixedResultTest"/>
  75. <testcase name="Fails" file="gtest_xml_output_unittest_.cc" line="93" status="run" result="completed" time="*" timestamp="*" classname="MixedResultTest">
  76. <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Expected equality of these values:&#x0A; 1&#x0A; 2%(stack_entity)s" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
  77. Expected equality of these values:
  78. 1
  79. 2%(stack)s]]></failure>
  80. <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Expected equality of these values:&#x0A; 2&#x0A; 3%(stack_entity)s" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
  81. Expected equality of these values:
  82. 2
  83. 3%(stack)s]]></failure>
  84. </testcase>
  85. <testcase name="DISABLED_test" file="gtest_xml_output_unittest_.cc" line="98" status="notrun" result="suppressed" time="*" timestamp="*" classname="MixedResultTest"/>
  86. </testsuite>
  87. <testsuite name="XmlQuotingTest" tests="1" failures="1" disabled="0" skipped="0" errors="0" time="*" timestamp="*">
  88. <testcase name="OutputsCData" file="gtest_xml_output_unittest_.cc" line="102" status="run" result="completed" time="*" timestamp="*" classname="XmlQuotingTest">
  89. <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Failed&#x0A;XML output: &lt;?xml encoding=&quot;utf-8&quot;&gt;&lt;top&gt;&lt;![CDATA[cdata text]]&gt;&lt;/top&gt;%(stack_entity)s" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
  90. Failed
  91. XML output: <?xml encoding="utf-8"><top><![CDATA[cdata text]]>]]&gt;<![CDATA[</top>%(stack)s]]></failure>
  92. </testcase>
  93. </testsuite>
  94. <testsuite name="InvalidCharactersTest" tests="1" failures="1" disabled="0" skipped="0" errors="0" time="*" timestamp="*">
  95. <testcase name="InvalidCharactersInMessage" file="gtest_xml_output_unittest_.cc" line="109" status="run" result="completed" time="*" timestamp="*" classname="InvalidCharactersTest">
  96. <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Failed&#x0A;Invalid characters in brackets []%(stack_entity)s" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
  97. Failed
  98. Invalid characters in brackets []%(stack)s]]></failure>
  99. </testcase>
  100. </testsuite>
  101. <testsuite name="DisabledTest" tests="1" failures="0" disabled="1" skipped="0" errors="0" time="*" timestamp="*">
  102. <testcase name="DISABLED_test_not_run" file="gtest_xml_output_unittest_.cc" line="68" status="notrun" result="suppressed" time="*" timestamp="*" classname="DisabledTest"/>
  103. </testsuite>
  104. <testsuite name="SkippedTest" tests="3" failures="1" disabled="0" skipped="2" errors="0" time="*" timestamp="*">
  105. <testcase name="Skipped" status="run" file="gtest_xml_output_unittest_.cc" line="75" result="skipped" time="*" timestamp="*" classname="SkippedTest">
  106. <skipped message="gtest_xml_output_unittest_.cc:*&#x0A;%(stack_entity)s"><![CDATA[gtest_xml_output_unittest_.cc:*
  107. %(stack)s]]></skipped>
  108. </testcase>
  109. <testcase name="SkippedWithMessage" file="gtest_xml_output_unittest_.cc" line="79" status="run" result="skipped" time="*" timestamp="*" classname="SkippedTest">
  110. <skipped message="gtest_xml_output_unittest_.cc:*&#x0A;It is good practice to tell why you skip a test.%(stack_entity)s"><![CDATA[gtest_xml_output_unittest_.cc:*
  111. It is good practice to tell why you skip a test.%(stack)s]]></skipped>
  112. </testcase>
  113. <testcase name="SkippedAfterFailure" file="gtest_xml_output_unittest_.cc" line="83" status="run" result="completed" time="*" timestamp="*" classname="SkippedTest">
  114. <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Expected equality of these values:&#x0A; 1&#x0A; 2%(stack_entity)s" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
  115. Expected equality of these values:
  116. 1
  117. 2%(stack)s]]></failure>
  118. <skipped message="gtest_xml_output_unittest_.cc:*&#x0A;It is good practice to tell why you skip a test.%(stack_entity)s"><![CDATA[gtest_xml_output_unittest_.cc:*
  119. It is good practice to tell why you skip a test.%(stack)s]]></skipped>
  120. </testcase>
  121. </testsuite>
  122. <testsuite name="PropertyRecordingTest" tests="4" failures="0" disabled="0" skipped="0" errors="0" time="*" timestamp="*" SetUpTestSuite="yes" TearDownTestSuite="aye">
  123. <testcase name="OneProperty" file="gtest_xml_output_unittest_.cc" line="121" status="run" result="completed" time="*" timestamp="*" classname="PropertyRecordingTest">
  124. <properties>
  125. <property name="key_1" value="1"/>
  126. </properties>
  127. </testcase>
  128. <testcase name="IntValuedProperty" file="gtest_xml_output_unittest_.cc" line="125" status="run" result="completed" time="*" timestamp="*" classname="PropertyRecordingTest">
  129. <properties>
  130. <property name="key_int" value="1"/>
  131. </properties>
  132. </testcase>
  133. <testcase name="ThreeProperties" file="gtest_xml_output_unittest_.cc" line="129" status="run" result="completed" time="*" timestamp="*" classname="PropertyRecordingTest">
  134. <properties>
  135. <property name="key_1" value="1"/>
  136. <property name="key_2" value="2"/>
  137. <property name="key_3" value="3"/>
  138. </properties>
  139. </testcase>
  140. <testcase name="TwoValuesForOneKeyUsesLastValue" file="gtest_xml_output_unittest_.cc" line="135" status="run" result="completed" time="*" timestamp="*" classname="PropertyRecordingTest">
  141. <properties>
  142. <property name="key_1" value="2"/>
  143. </properties>
  144. </testcase>
  145. </testsuite>
  146. <testsuite name="NoFixtureTest" tests="3" failures="0" disabled="0" skipped="0" errors="0" time="*" timestamp="*">
  147. <testcase name="RecordProperty" file="gtest_xml_output_unittest_.cc" line="140" status="run" result="completed" time="*" timestamp="*" classname="NoFixtureTest">
  148. <properties>
  149. <property name="key" value="1"/>
  150. </properties>
  151. </testcase>
  152. <testcase name="ExternalUtilityThatCallsRecordIntValuedProperty" file="gtest_xml_output_unittest_.cc" line="153" status="run" result="completed" time="*" timestamp="*" classname="NoFixtureTest">
  153. <properties>
  154. <property name="key_for_utility_int" value="1"/>
  155. </properties>
  156. </testcase>
  157. <testcase name="ExternalUtilityThatCallsRecordStringValuedProperty" file="gtest_xml_output_unittest_.cc" line="157" status="run" result="completed" time="*" timestamp="*" classname="NoFixtureTest">
  158. <properties>
  159. <property name="key_for_utility_string" value="1"/>
  160. </properties>
  161. </testcase>
  162. </testsuite>
  163. <testsuite name="Single/ValueParamTest" tests="4" failures="0" disabled="0" skipped="0" errors="0" time="*" timestamp="*">
  164. <testcase name="HasValueParamAttribute/0" file="gtest_xml_output_unittest_.cc" line="164" value_param="33" status="run" result="completed" time="*" timestamp="*" classname="Single/ValueParamTest" />
  165. <testcase name="HasValueParamAttribute/1" file="gtest_xml_output_unittest_.cc" line="164" value_param="42" status="run" result="completed" time="*" timestamp="*" classname="Single/ValueParamTest" />
  166. <testcase name="AnotherTestThatHasValueParamAttribute/0" file="gtest_xml_output_unittest_.cc" line="165" value_param="33" status="run" result="completed" time="*" timestamp="*" classname="Single/ValueParamTest" />
  167. <testcase name="AnotherTestThatHasValueParamAttribute/1" file="gtest_xml_output_unittest_.cc" line="165" value_param="42" status="run" result="completed" time="*" timestamp="*" classname="Single/ValueParamTest" />
  168. </testsuite>
  169. <testsuite name="TypedTest/0" tests="1" failures="0" disabled="0" skipped="0" errors="0" time="*" timestamp="*">
  170. <testcase name="HasTypeParamAttribute" file="gtest_xml_output_unittest_.cc" line="173" type_param="*" status="run" result="completed" time="*" timestamp="*" classname="TypedTest/0" />
  171. </testsuite>
  172. <testsuite name="TypedTest/1" tests="1" failures="0" disabled="0" skipped="0" errors="0" time="*" timestamp="*">
  173. <testcase name="HasTypeParamAttribute" file="gtest_xml_output_unittest_.cc" line="173" type_param="*" status="run" result="completed" time="*" timestamp="*" classname="TypedTest/1" />
  174. </testsuite>
  175. <testsuite name="Single/TypeParameterizedTestSuite/0" tests="1" failures="0" disabled="0" skipped="0" errors="0" time="*" timestamp="*">
  176. <testcase name="HasTypeParamAttribute" file="gtest_xml_output_unittest_.cc" line="180" type_param="*" status="run" result="completed" time="*" timestamp="*" classname="Single/TypeParameterizedTestSuite/0" />
  177. </testsuite>
  178. <testsuite name="Single/TypeParameterizedTestSuite/1" tests="1" failures="0" disabled="0" skipped="0" errors="0" time="*" timestamp="*">
  179. <testcase name="HasTypeParamAttribute" file="gtest_xml_output_unittest_.cc" line="180" type_param="*" status="run" result="completed" time="*" timestamp="*" classname="Single/TypeParameterizedTestSuite/1" />
  180. </testsuite>
  181. </testsuites>""" % {
  182. 'stack': STACK_TRACE_TEMPLATE,
  183. 'stack_entity': STACK_TRACE_ENTITY_TEMPLATE,
  184. }
  185. EXPECTED_FILTERED_TEST_XML = """<?xml version="1.0" encoding="UTF-8"?>
  186. <testsuites tests="1" failures="0" disabled="0" errors="0" time="*"
  187. timestamp="*" name="AllTests" ad_hoc_property="42">
  188. <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" skipped="0"
  189. errors="0" time="*" timestamp="*">
  190. <testcase name="Succeeds" file="gtest_xml_output_unittest_.cc" line="53" status="run" result="completed" time="*" timestamp="*" classname="SuccessfulTest"/>
  191. </testsuite>
  192. </testsuites>"""
  193. EXPECTED_SHARDED_TEST_XML = """<?xml version="1.0" encoding="UTF-8"?>
  194. <testsuites tests="3" failures="0" disabled="0" errors="0" time="*" timestamp="*" name="AllTests" ad_hoc_property="42">
  195. <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" skipped="0" errors="0" time="*" timestamp="*">
  196. <testcase name="Succeeds" file="gtest_xml_output_unittest_.cc" line="53" status="run" result="completed" time="*" timestamp="*" classname="SuccessfulTest"/>
  197. </testsuite>
  198. <testsuite name="PropertyRecordingTest" tests="1" failures="0" disabled="0" skipped="0" errors="0" time="*" timestamp="*" SetUpTestSuite="yes" TearDownTestSuite="aye">
  199. <testcase name="IntValuedProperty" file="gtest_xml_output_unittest_.cc" line="125" status="run" result="completed" time="*" timestamp="*" classname="PropertyRecordingTest">
  200. <properties>
  201. <property name="key_int" value="1"/>
  202. </properties>
  203. </testcase>
  204. </testsuite>
  205. <testsuite name="Single/ValueParamTest" tests="1" failures="0" disabled="0" skipped="0" errors="0" time="*" timestamp="*">
  206. <testcase name="HasValueParamAttribute/0" file="gtest_xml_output_unittest_.cc" line="164" value_param="33" status="run" result="completed" time="*" timestamp="*" classname="Single/ValueParamTest" />
  207. </testsuite>
  208. </testsuites>"""
  209. EXPECTED_NO_TEST_XML = """<?xml version="1.0" encoding="UTF-8"?>
  210. <testsuites tests="0" failures="0" disabled="0" errors="0" time="*"
  211. timestamp="*" name="AllTests">
  212. <testsuite name="NonTestSuiteFailure" tests="1" failures="1" disabled="0" skipped="0" errors="0" time="*" timestamp="*">
  213. <testcase name="" status="run" result="completed" time="*" timestamp="*" classname="">
  214. <failure message="gtest_no_test_unittest.cc:*&#x0A;Expected equality of these values:&#x0A; 1&#x0A; 2%(stack_entity)s" type=""><![CDATA[gtest_no_test_unittest.cc:*
  215. Expected equality of these values:
  216. 1
  217. 2%(stack)s]]></failure>
  218. </testcase>
  219. </testsuite>
  220. </testsuites>""" % {
  221. 'stack': STACK_TRACE_TEMPLATE,
  222. 'stack_entity': STACK_TRACE_ENTITY_TEMPLATE,
  223. }
  224. GTEST_PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
  225. SUPPORTS_TYPED_TESTS = (
  226. 'TypedTest'
  227. in gtest_test_utils.Subprocess(
  228. [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=False
  229. ).output
  230. )
  231. class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
  232. """Unit test for Google Test's XML output functionality."""
  233. # This test currently breaks on platforms that do not support typed and
  234. # type-parameterized tests, so we don't run it under them.
  235. if SUPPORTS_TYPED_TESTS:
  236. def testNonEmptyXmlOutput(self):
  237. """Generates non-empty XML and verifies it matches the expected output.
  238. Runs a test program that generates a non-empty XML output, and
  239. tests that the XML output is expected.
  240. """
  241. self._TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY_XML, 1)
  242. def testNoTestXmlOutput(self):
  243. """Verifies XML output for a Google Test binary without actual tests.
  244. Runs a test program that generates an XML output for a binary without tests,
  245. and tests that the XML output is expected.
  246. """
  247. self._TestXmlOutput('gtest_no_test_unittest', EXPECTED_NO_TEST_XML, 0)
  248. def testTimestampValue(self):
  249. """Checks whether the timestamp attribute in the XML output is valid.
  250. Runs a test program that generates an empty XML output, and checks if
  251. the timestamp attribute in the testsuites tag is valid.
  252. """
  253. actual = self._GetXmlOutput('gtest_no_test_unittest', [], {}, 0)
  254. date_time_str = actual.documentElement.getAttributeNode('timestamp').value
  255. # datetime.strptime() is only available in Python 2.5+ so we have to
  256. # parse the expected datetime manually.
  257. match = re.match(r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
  258. self.assertTrue(
  259. re.match, 'XML datettime string %s has incorrect format' % date_time_str
  260. )
  261. date_time_from_xml = datetime.datetime(
  262. year=int(match.group(1)),
  263. month=int(match.group(2)),
  264. day=int(match.group(3)),
  265. hour=int(match.group(4)),
  266. minute=int(match.group(5)),
  267. second=int(match.group(6)),
  268. )
  269. time_delta = abs(datetime.datetime.now() - date_time_from_xml)
  270. # timestamp value should be near the current local time
  271. self.assertLess(time_delta, datetime.timedelta(seconds=600))
  272. actual.unlink()
  273. def testDefaultOutputFile(self):
  274. """Tests XML file with default name is created when name is not specified.
  275. Confirms that Google Test produces an XML output file with the expected
  276. default name if no name is explicitly specified.
  277. """
  278. output_file = os.path.join(
  279. gtest_test_utils.GetTempDir(), GTEST_DEFAULT_OUTPUT_FILE
  280. )
  281. gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
  282. 'gtest_no_test_unittest'
  283. )
  284. try:
  285. os.remove(output_file)
  286. except OSError:
  287. e = sys.exc_info()[1]
  288. if e.errno != errno.ENOENT:
  289. raise
  290. p = gtest_test_utils.Subprocess(
  291. [gtest_prog_path, '%s=xml' % GTEST_OUTPUT_FLAG],
  292. working_dir=gtest_test_utils.GetTempDir(),
  293. )
  294. self.assertTrue(p.exited)
  295. self.assertEqual(0, p.exit_code)
  296. self.assertTrue(os.path.isfile(output_file))
  297. def testSuppressedXmlOutput(self):
  298. """Verifies XML output is suppressed if default listener is shut down.
  299. Tests that no XML file is generated if the default XML listener is
  300. shut down before RUN_ALL_TESTS is invoked.
  301. """
  302. xml_path = os.path.join(
  303. gtest_test_utils.GetTempDir(), GTEST_PROGRAM_NAME + 'out.xml'
  304. )
  305. if os.path.isfile(xml_path):
  306. os.remove(xml_path)
  307. command = [
  308. GTEST_PROGRAM_PATH,
  309. '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path),
  310. '--shut_down_xml',
  311. ]
  312. p = gtest_test_utils.Subprocess(command)
  313. if p.terminated_by_signal:
  314. # p.signal is available only if p.terminated_by_signal is True.
  315. self.assertFalse(
  316. p.terminated_by_signal,
  317. '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal),
  318. )
  319. else:
  320. self.assertTrue(p.exited)
  321. self.assertEqual(
  322. 1,
  323. p.exit_code,
  324. "'%s' exited with code %s, which doesn't match "
  325. 'the expected exit code %s.' % (command, p.exit_code, 1),
  326. )
  327. self.assertFalse(os.path.isfile(xml_path))
  328. def testFilteredTestXmlOutput(self):
  329. """Verifies XML output when a filter is applied.
  330. Runs a test program that executes only some tests and verifies that
  331. non-selected tests do not show up in the XML output.
  332. """
  333. self._TestXmlOutput(
  334. GTEST_PROGRAM_NAME,
  335. EXPECTED_FILTERED_TEST_XML,
  336. 0,
  337. extra_args=['%s=SuccessfulTest.*' % GTEST_FILTER_FLAG],
  338. )
  339. def testShardedTestXmlOutput(self):
  340. """Verifies XML output when run using multiple shards.
  341. Runs a test program that executes only one shard and verifies that tests
  342. from other shards do not show up in the XML output.
  343. """
  344. self._TestXmlOutput(
  345. GTEST_PROGRAM_NAME,
  346. EXPECTED_SHARDED_TEST_XML,
  347. 0,
  348. extra_env={SHARD_INDEX_ENV_VAR: '0', TOTAL_SHARDS_ENV_VAR: '10'},
  349. )
  350. def _GetXmlOutput(
  351. self, gtest_prog_name, extra_args, extra_env, expected_exit_code
  352. ):
  353. """Returns the XML output generated by running the program gtest_prog_name.
  354. Furthermore, the program's exit code must be expected_exit_code.
  355. Args:
  356. gtest_prog_name: Program to run.
  357. extra_args: Optional arguments to pass to program.
  358. extra_env: Optional environment variables to set.
  359. expected_exit_code: Expected exit code from running gtest_prog_name.
  360. """
  361. xml_path = os.path.join(
  362. gtest_test_utils.GetTempDir(), gtest_prog_name + 'out.xml'
  363. )
  364. gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
  365. command = [
  366. gtest_prog_path,
  367. '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path),
  368. ] + extra_args
  369. environ_copy = os.environ.copy()
  370. if extra_env:
  371. environ_copy.update(extra_env)
  372. p = gtest_test_utils.Subprocess(command, env=environ_copy)
  373. if p.terminated_by_signal:
  374. self.assertTrue(
  375. False, '%s was killed by signal %d' % (gtest_prog_name, p.signal)
  376. )
  377. else:
  378. self.assertTrue(p.exited)
  379. self.assertEqual(
  380. expected_exit_code,
  381. p.exit_code,
  382. "'%s' exited with code %s, which doesn't match "
  383. 'the expected exit code %s.'
  384. % (command, p.exit_code, expected_exit_code),
  385. )
  386. actual = minidom.parse(xml_path)
  387. return actual
  388. def _TestXmlOutput(
  389. self,
  390. gtest_prog_name,
  391. expected_xml,
  392. expected_exit_code,
  393. extra_args=None,
  394. extra_env=None,
  395. ):
  396. """Asserts that the XML document matches.
  397. Asserts that the XML document generated by running the program
  398. gtest_prog_name matches expected_xml, a string containing another
  399. XML document. Furthermore, the program's exit code must be
  400. expected_exit_code.
  401. Args:
  402. gtest_prog_name: Program to run.
  403. expected_xml: Path to XML document to match.
  404. expected_exit_code: Expected exit code from running gtest_prog_name.
  405. extra_args: Optional arguments to pass to program.
  406. extra_env: Optional environment variables to set.
  407. """
  408. actual = self._GetXmlOutput(
  409. gtest_prog_name, extra_args or [], extra_env or {}, expected_exit_code
  410. )
  411. expected = minidom.parseString(expected_xml)
  412. self.NormalizeXml(actual.documentElement)
  413. self.AssertEquivalentNodes(expected.documentElement, actual.documentElement)
  414. expected.unlink()
  415. actual.unlink()
  416. if __name__ == '__main__':
  417. os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
  418. gtest_test_utils.Main()