You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

778 lines
22 KiB

  1. #!/usr/bin/env python
  2. # Copyright 2018, Google Inc.
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Google Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. """Unit test for the gtest_json_output module."""
  31. import datetime
  32. import errno
  33. import json
  34. import os
  35. import re
  36. import sys
  37. import gtest_json_test_utils
  38. import gtest_test_utils
  39. GTEST_FILTER_FLAG = '--gtest_filter'
  40. GTEST_LIST_TESTS_FLAG = '--gtest_list_tests'
  41. GTEST_OUTPUT_FLAG = '--gtest_output'
  42. GTEST_DEFAULT_OUTPUT_FILE = 'test_detail.json'
  43. GTEST_PROGRAM_NAME = 'gtest_xml_output_unittest_'
  44. # The flag indicating stacktraces are not supported
  45. NO_STACKTRACE_SUPPORT_FLAG = '--no_stacktrace_support'
  46. SUPPORTS_STACK_TRACES = NO_STACKTRACE_SUPPORT_FLAG not in sys.argv
  47. if SUPPORTS_STACK_TRACES:
  48. STACK_TRACE_TEMPLATE = '\nStack trace:\n*'
  49. else:
  50. STACK_TRACE_TEMPLATE = ''
  51. EXPECTED_NON_EMPTY = {
  52. u'tests':
  53. 24,
  54. u'failures':
  55. 4,
  56. u'disabled':
  57. 2,
  58. u'errors':
  59. 0,
  60. u'timestamp':
  61. u'*',
  62. u'time':
  63. u'*',
  64. u'ad_hoc_property':
  65. u'42',
  66. u'name':
  67. u'AllTests',
  68. u'testsuites': [{
  69. u'name':
  70. u'SuccessfulTest',
  71. u'tests':
  72. 1,
  73. u'failures':
  74. 0,
  75. u'disabled':
  76. 0,
  77. u'errors':
  78. 0,
  79. u'time':
  80. u'*',
  81. u'timestamp':
  82. u'*',
  83. u'testsuite': [{
  84. u'name': u'Succeeds',
  85. u'status': u'RUN',
  86. u'result': u'COMPLETED',
  87. u'time': u'*',
  88. u'timestamp': u'*',
  89. u'classname': u'SuccessfulTest'
  90. }]
  91. }, {
  92. u'name':
  93. u'FailedTest',
  94. u'tests':
  95. 1,
  96. u'failures':
  97. 1,
  98. u'disabled':
  99. 0,
  100. u'errors':
  101. 0,
  102. u'time':
  103. u'*',
  104. u'timestamp':
  105. u'*',
  106. u'testsuite': [{
  107. u'name':
  108. u'Fails',
  109. u'status':
  110. u'RUN',
  111. u'result':
  112. u'COMPLETED',
  113. u'time':
  114. u'*',
  115. u'timestamp':
  116. u'*',
  117. u'classname':
  118. u'FailedTest',
  119. u'failures': [{
  120. u'failure': u'gtest_xml_output_unittest_.cc:*\n'
  121. u'Expected equality of these values:\n'
  122. u' 1\n 2' + STACK_TRACE_TEMPLATE,
  123. u'type': u''
  124. }]
  125. }]
  126. }, {
  127. u'name':
  128. u'DisabledTest',
  129. u'tests':
  130. 1,
  131. u'failures':
  132. 0,
  133. u'disabled':
  134. 1,
  135. u'errors':
  136. 0,
  137. u'time':
  138. u'*',
  139. u'timestamp':
  140. u'*',
  141. u'testsuite': [{
  142. u'name': u'DISABLED_test_not_run',
  143. u'status': u'NOTRUN',
  144. u'result': u'SUPPRESSED',
  145. u'time': u'*',
  146. u'timestamp': u'*',
  147. u'classname': u'DisabledTest'
  148. }]
  149. }, {
  150. u'name':
  151. u'SkippedTest',
  152. u'tests':
  153. 1,
  154. u'failures':
  155. 0,
  156. u'disabled':
  157. 0,
  158. u'errors':
  159. 0,
  160. u'time':
  161. u'*',
  162. u'timestamp':
  163. u'*',
  164. u'testsuite': [{
  165. u'name': u'Skipped',
  166. u'status': u'RUN',
  167. u'result': u'SKIPPED',
  168. u'time': u'*',
  169. u'timestamp': u'*',
  170. u'classname': u'SkippedTest'
  171. }]
  172. }, {
  173. u'name':
  174. u'MixedResultTest',
  175. u'tests':
  176. 3,
  177. u'failures':
  178. 1,
  179. u'disabled':
  180. 1,
  181. u'errors':
  182. 0,
  183. u'time':
  184. u'*',
  185. u'timestamp':
  186. u'*',
  187. u'testsuite': [{
  188. u'name': u'Succeeds',
  189. u'status': u'RUN',
  190. u'result': u'COMPLETED',
  191. u'time': u'*',
  192. u'timestamp': u'*',
  193. u'classname': u'MixedResultTest'
  194. }, {
  195. u'name':
  196. u'Fails',
  197. u'status':
  198. u'RUN',
  199. u'result':
  200. u'COMPLETED',
  201. u'time':
  202. u'*',
  203. u'timestamp':
  204. u'*',
  205. u'classname':
  206. u'MixedResultTest',
  207. u'failures': [{
  208. u'failure': u'gtest_xml_output_unittest_.cc:*\n'
  209. u'Expected equality of these values:\n'
  210. u' 1\n 2' + STACK_TRACE_TEMPLATE,
  211. u'type': u''
  212. }, {
  213. u'failure': u'gtest_xml_output_unittest_.cc:*\n'
  214. u'Expected equality of these values:\n'
  215. u' 2\n 3' + STACK_TRACE_TEMPLATE,
  216. u'type': u''
  217. }]
  218. }, {
  219. u'name': u'DISABLED_test',
  220. u'status': u'NOTRUN',
  221. u'result': u'SUPPRESSED',
  222. u'time': u'*',
  223. u'timestamp': u'*',
  224. u'classname': u'MixedResultTest'
  225. }]
  226. }, {
  227. u'name':
  228. u'XmlQuotingTest',
  229. u'tests':
  230. 1,
  231. u'failures':
  232. 1,
  233. u'disabled':
  234. 0,
  235. u'errors':
  236. 0,
  237. u'time':
  238. u'*',
  239. u'timestamp':
  240. u'*',
  241. u'testsuite': [{
  242. u'name':
  243. u'OutputsCData',
  244. u'status':
  245. u'RUN',
  246. u'result':
  247. u'COMPLETED',
  248. u'time':
  249. u'*',
  250. u'timestamp':
  251. u'*',
  252. u'classname':
  253. u'XmlQuotingTest',
  254. u'failures': [{
  255. u'failure': u'gtest_xml_output_unittest_.cc:*\n'
  256. u'Failed\nXML output: <?xml encoding="utf-8">'
  257. u'<top><![CDATA[cdata text]]></top>' +
  258. STACK_TRACE_TEMPLATE,
  259. u'type': u''
  260. }]
  261. }]
  262. }, {
  263. u'name':
  264. u'InvalidCharactersTest',
  265. u'tests':
  266. 1,
  267. u'failures':
  268. 1,
  269. u'disabled':
  270. 0,
  271. u'errors':
  272. 0,
  273. u'time':
  274. u'*',
  275. u'timestamp':
  276. u'*',
  277. u'testsuite': [{
  278. u'name':
  279. u'InvalidCharactersInMessage',
  280. u'status':
  281. u'RUN',
  282. u'result':
  283. u'COMPLETED',
  284. u'time':
  285. u'*',
  286. u'timestamp':
  287. u'*',
  288. u'classname':
  289. u'InvalidCharactersTest',
  290. u'failures': [{
  291. u'failure': u'gtest_xml_output_unittest_.cc:*\n'
  292. u'Failed\nInvalid characters in brackets'
  293. u' [\x01\x02]' + STACK_TRACE_TEMPLATE,
  294. u'type': u''
  295. }]
  296. }]
  297. }, {
  298. u'name':
  299. u'PropertyRecordingTest',
  300. u'tests':
  301. 4,
  302. u'failures':
  303. 0,
  304. u'disabled':
  305. 0,
  306. u'errors':
  307. 0,
  308. u'time':
  309. u'*',
  310. u'timestamp':
  311. u'*',
  312. u'SetUpTestSuite':
  313. u'yes',
  314. u'TearDownTestSuite':
  315. u'aye',
  316. u'testsuite': [{
  317. u'name': u'OneProperty',
  318. u'status': u'RUN',
  319. u'result': u'COMPLETED',
  320. u'time': u'*',
  321. u'timestamp': u'*',
  322. u'classname': u'PropertyRecordingTest',
  323. u'key_1': u'1'
  324. }, {
  325. u'name': u'IntValuedProperty',
  326. u'status': u'RUN',
  327. u'result': u'COMPLETED',
  328. u'time': u'*',
  329. u'timestamp': u'*',
  330. u'classname': u'PropertyRecordingTest',
  331. u'key_int': u'1'
  332. }, {
  333. u'name': u'ThreeProperties',
  334. u'status': u'RUN',
  335. u'result': u'COMPLETED',
  336. u'time': u'*',
  337. u'timestamp': u'*',
  338. u'classname': u'PropertyRecordingTest',
  339. u'key_1': u'1',
  340. u'key_2': u'2',
  341. u'key_3': u'3'
  342. }, {
  343. u'name': u'TwoValuesForOneKeyUsesLastValue',
  344. u'status': u'RUN',
  345. u'result': u'COMPLETED',
  346. u'time': u'*',
  347. u'timestamp': u'*',
  348. u'classname': u'PropertyRecordingTest',
  349. u'key_1': u'2'
  350. }]
  351. }, {
  352. u'name':
  353. u'NoFixtureTest',
  354. u'tests':
  355. 3,
  356. u'failures':
  357. 0,
  358. u'disabled':
  359. 0,
  360. u'errors':
  361. 0,
  362. u'time':
  363. u'*',
  364. u'timestamp':
  365. u'*',
  366. u'testsuite': [{
  367. u'name': u'RecordProperty',
  368. u'status': u'RUN',
  369. u'result': u'COMPLETED',
  370. u'time': u'*',
  371. u'timestamp': u'*',
  372. u'classname': u'NoFixtureTest',
  373. u'key': u'1'
  374. }, {
  375. u'name': u'ExternalUtilityThatCallsRecordIntValuedProperty',
  376. u'status': u'RUN',
  377. u'result': u'COMPLETED',
  378. u'time': u'*',
  379. u'timestamp': u'*',
  380. u'classname': u'NoFixtureTest',
  381. u'key_for_utility_int': u'1'
  382. }, {
  383. u'name': u'ExternalUtilityThatCallsRecordStringValuedProperty',
  384. u'status': u'RUN',
  385. u'result': u'COMPLETED',
  386. u'time': u'*',
  387. u'timestamp': u'*',
  388. u'classname': u'NoFixtureTest',
  389. u'key_for_utility_string': u'1'
  390. }]
  391. }, {
  392. u'name':
  393. u'TypedTest/0',
  394. u'tests':
  395. 1,
  396. u'failures':
  397. 0,
  398. u'disabled':
  399. 0,
  400. u'errors':
  401. 0,
  402. u'time':
  403. u'*',
  404. u'timestamp':
  405. u'*',
  406. u'testsuite': [{
  407. u'name': u'HasTypeParamAttribute',
  408. u'type_param': u'int',
  409. u'status': u'RUN',
  410. u'result': u'COMPLETED',
  411. u'time': u'*',
  412. u'timestamp': u'*',
  413. u'classname': u'TypedTest/0'
  414. }]
  415. }, {
  416. u'name':
  417. u'TypedTest/1',
  418. u'tests':
  419. 1,
  420. u'failures':
  421. 0,
  422. u'disabled':
  423. 0,
  424. u'errors':
  425. 0,
  426. u'time':
  427. u'*',
  428. u'timestamp':
  429. u'*',
  430. u'testsuite': [{
  431. u'name': u'HasTypeParamAttribute',
  432. u'type_param': u'long',
  433. u'status': u'RUN',
  434. u'result': u'COMPLETED',
  435. u'time': u'*',
  436. u'timestamp': u'*',
  437. u'classname': u'TypedTest/1'
  438. }]
  439. }, {
  440. u'name':
  441. u'Single/TypeParameterizedTestSuite/0',
  442. u'tests':
  443. 1,
  444. u'failures':
  445. 0,
  446. u'disabled':
  447. 0,
  448. u'errors':
  449. 0,
  450. u'time':
  451. u'*',
  452. u'timestamp':
  453. u'*',
  454. u'testsuite': [{
  455. u'name': u'HasTypeParamAttribute',
  456. u'type_param': u'int',
  457. u'status': u'RUN',
  458. u'result': u'COMPLETED',
  459. u'time': u'*',
  460. u'timestamp': u'*',
  461. u'classname': u'Single/TypeParameterizedTestSuite/0'
  462. }]
  463. }, {
  464. u'name':
  465. u'Single/TypeParameterizedTestSuite/1',
  466. u'tests':
  467. 1,
  468. u'failures':
  469. 0,
  470. u'disabled':
  471. 0,
  472. u'errors':
  473. 0,
  474. u'time':
  475. u'*',
  476. u'timestamp':
  477. u'*',
  478. u'testsuite': [{
  479. u'name': u'HasTypeParamAttribute',
  480. u'type_param': u'long',
  481. u'status': u'RUN',
  482. u'result': u'COMPLETED',
  483. u'time': u'*',
  484. u'timestamp': u'*',
  485. u'classname': u'Single/TypeParameterizedTestSuite/1'
  486. }]
  487. }, {
  488. u'name':
  489. u'Single/ValueParamTest',
  490. u'tests':
  491. 4,
  492. u'failures':
  493. 0,
  494. u'disabled':
  495. 0,
  496. u'errors':
  497. 0,
  498. u'time':
  499. u'*',
  500. u'timestamp':
  501. u'*',
  502. u'testsuite': [{
  503. u'name': u'HasValueParamAttribute/0',
  504. u'value_param': u'33',
  505. u'status': u'RUN',
  506. u'result': u'COMPLETED',
  507. u'time': u'*',
  508. u'timestamp': u'*',
  509. u'classname': u'Single/ValueParamTest'
  510. }, {
  511. u'name': u'HasValueParamAttribute/1',
  512. u'value_param': u'42',
  513. u'status': u'RUN',
  514. u'result': u'COMPLETED',
  515. u'time': u'*',
  516. u'timestamp': u'*',
  517. u'classname': u'Single/ValueParamTest'
  518. }, {
  519. u'name': u'AnotherTestThatHasValueParamAttribute/0',
  520. u'value_param': u'33',
  521. u'status': u'RUN',
  522. u'result': u'COMPLETED',
  523. u'time': u'*',
  524. u'timestamp': u'*',
  525. u'classname': u'Single/ValueParamTest'
  526. }, {
  527. u'name': u'AnotherTestThatHasValueParamAttribute/1',
  528. u'value_param': u'42',
  529. u'status': u'RUN',
  530. u'result': u'COMPLETED',
  531. u'time': u'*',
  532. u'timestamp': u'*',
  533. u'classname': u'Single/ValueParamTest'
  534. }]
  535. }]
  536. }
  537. EXPECTED_FILTERED = {
  538. u'tests':
  539. 1,
  540. u'failures':
  541. 0,
  542. u'disabled':
  543. 0,
  544. u'errors':
  545. 0,
  546. u'time':
  547. u'*',
  548. u'timestamp':
  549. u'*',
  550. u'name':
  551. u'AllTests',
  552. u'ad_hoc_property':
  553. u'42',
  554. u'testsuites': [{
  555. u'name':
  556. u'SuccessfulTest',
  557. u'tests':
  558. 1,
  559. u'failures':
  560. 0,
  561. u'disabled':
  562. 0,
  563. u'errors':
  564. 0,
  565. u'time':
  566. u'*',
  567. u'timestamp':
  568. u'*',
  569. u'testsuite': [{
  570. u'name': u'Succeeds',
  571. u'status': u'RUN',
  572. u'result': u'COMPLETED',
  573. u'time': u'*',
  574. u'timestamp': u'*',
  575. u'classname': u'SuccessfulTest',
  576. }]
  577. }],
  578. }
  579. EXPECTED_EMPTY = {
  580. u'tests': 0,
  581. u'failures': 0,
  582. u'disabled': 0,
  583. u'errors': 0,
  584. u'time': u'*',
  585. u'timestamp': u'*',
  586. u'name': u'AllTests',
  587. u'testsuites': [],
  588. }
  589. GTEST_PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
  590. SUPPORTS_TYPED_TESTS = 'TypedTest' in gtest_test_utils.Subprocess(
  591. [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=False).output
  592. class GTestJsonOutputUnitTest(gtest_test_utils.TestCase):
  593. """Unit test for Google Test's JSON output functionality.
  594. """
  595. # This test currently breaks on platforms that do not support typed and
  596. # type-parameterized tests, so we don't run it under them.
  597. if SUPPORTS_TYPED_TESTS:
  598. def testNonEmptyJsonOutput(self):
  599. """Verifies JSON output for a Google Test binary with non-empty output.
  600. Runs a test program that generates a non-empty JSON output, and
  601. tests that the JSON output is expected.
  602. """
  603. self._TestJsonOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY, 1)
  604. def testEmptyJsonOutput(self):
  605. """Verifies JSON output for a Google Test binary without actual tests.
  606. Runs a test program that generates an empty JSON output, and
  607. tests that the JSON output is expected.
  608. """
  609. self._TestJsonOutput('gtest_no_test_unittest', EXPECTED_EMPTY, 0)
  610. def testTimestampValue(self):
  611. """Checks whether the timestamp attribute in the JSON output is valid.
  612. Runs a test program that generates an empty JSON output, and checks if
  613. the timestamp attribute in the testsuites tag is valid.
  614. """
  615. actual = self._GetJsonOutput('gtest_no_test_unittest', [], 0)
  616. date_time_str = actual['timestamp']
  617. # datetime.strptime() is only available in Python 2.5+ so we have to
  618. # parse the expected datetime manually.
  619. match = re.match(r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
  620. self.assertTrue(
  621. re.match,
  622. 'JSON datettime string %s has incorrect format' % date_time_str)
  623. date_time_from_json = datetime.datetime(
  624. year=int(match.group(1)), month=int(match.group(2)),
  625. day=int(match.group(3)), hour=int(match.group(4)),
  626. minute=int(match.group(5)), second=int(match.group(6)))
  627. time_delta = abs(datetime.datetime.now() - date_time_from_json)
  628. # timestamp value should be near the current local time
  629. self.assertTrue(time_delta < datetime.timedelta(seconds=600),
  630. 'time_delta is %s' % time_delta)
  631. def testDefaultOutputFile(self):
  632. """Verifies the default output file name.
  633. Confirms that Google Test produces an JSON output file with the expected
  634. default name if no name is explicitly specified.
  635. """
  636. output_file = os.path.join(gtest_test_utils.GetTempDir(),
  637. GTEST_DEFAULT_OUTPUT_FILE)
  638. gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
  639. 'gtest_no_test_unittest')
  640. try:
  641. os.remove(output_file)
  642. except OSError:
  643. e = sys.exc_info()[1]
  644. if e.errno != errno.ENOENT:
  645. raise
  646. p = gtest_test_utils.Subprocess(
  647. [gtest_prog_path, '%s=json' % GTEST_OUTPUT_FLAG],
  648. working_dir=gtest_test_utils.GetTempDir())
  649. self.assert_(p.exited)
  650. self.assertEquals(0, p.exit_code)
  651. self.assert_(os.path.isfile(output_file))
  652. def testSuppressedJsonOutput(self):
  653. """Verifies that no JSON output is generated.
  654. Tests that no JSON file is generated if the default JSON listener is
  655. shut down before RUN_ALL_TESTS is invoked.
  656. """
  657. json_path = os.path.join(gtest_test_utils.GetTempDir(),
  658. GTEST_PROGRAM_NAME + 'out.json')
  659. if os.path.isfile(json_path):
  660. os.remove(json_path)
  661. command = [GTEST_PROGRAM_PATH,
  662. '%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path),
  663. '--shut_down_xml']
  664. p = gtest_test_utils.Subprocess(command)
  665. if p.terminated_by_signal:
  666. # p.signal is available only if p.terminated_by_signal is True.
  667. self.assertFalse(
  668. p.terminated_by_signal,
  669. '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal))
  670. else:
  671. self.assert_(p.exited)
  672. self.assertEquals(1, p.exit_code,
  673. "'%s' exited with code %s, which doesn't match "
  674. 'the expected exit code %s.'
  675. % (command, p.exit_code, 1))
  676. self.assert_(not os.path.isfile(json_path))
  677. def testFilteredTestJsonOutput(self):
  678. """Verifies JSON output when a filter is applied.
  679. Runs a test program that executes only some tests and verifies that
  680. non-selected tests do not show up in the JSON output.
  681. """
  682. self._TestJsonOutput(GTEST_PROGRAM_NAME, EXPECTED_FILTERED, 0,
  683. extra_args=['%s=SuccessfulTest.*' % GTEST_FILTER_FLAG])
  684. def _GetJsonOutput(self, gtest_prog_name, extra_args, expected_exit_code):
  685. """Returns the JSON output generated by running the program gtest_prog_name.
  686. Furthermore, the program's exit code must be expected_exit_code.
  687. Args:
  688. gtest_prog_name: Google Test binary name.
  689. extra_args: extra arguments to binary invocation.
  690. expected_exit_code: program's exit code.
  691. """
  692. json_path = os.path.join(gtest_test_utils.GetTempDir(),
  693. gtest_prog_name + 'out.json')
  694. gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
  695. command = (
  696. [gtest_prog_path, '%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path)] +
  697. extra_args
  698. )
  699. p = gtest_test_utils.Subprocess(command)
  700. if p.terminated_by_signal:
  701. self.assert_(False,
  702. '%s was killed by signal %d' % (gtest_prog_name, p.signal))
  703. else:
  704. self.assert_(p.exited)
  705. self.assertEquals(expected_exit_code, p.exit_code,
  706. "'%s' exited with code %s, which doesn't match "
  707. 'the expected exit code %s.'
  708. % (command, p.exit_code, expected_exit_code))
  709. with open(json_path) as f:
  710. actual = json.load(f)
  711. return actual
  712. def _TestJsonOutput(self, gtest_prog_name, expected,
  713. expected_exit_code, extra_args=None):
  714. """Checks the JSON output generated by the Google Test binary.
  715. Asserts that the JSON document generated by running the program
  716. gtest_prog_name matches expected_json, a string containing another
  717. JSON document. Furthermore, the program's exit code must be
  718. expected_exit_code.
  719. Args:
  720. gtest_prog_name: Google Test binary name.
  721. expected: expected output.
  722. expected_exit_code: program's exit code.
  723. extra_args: extra arguments to binary invocation.
  724. """
  725. actual = self._GetJsonOutput(gtest_prog_name, extra_args or [],
  726. expected_exit_code)
  727. self.assertEqual(expected, gtest_json_test_utils.normalize(actual))
  728. if __name__ == '__main__':
  729. if NO_STACKTRACE_SUPPORT_FLAG in sys.argv:
  730. # unittest.main() can't handle unknown flags
  731. sys.argv.remove(NO_STACKTRACE_SUPPORT_FLAG)
  732. os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
  733. gtest_test_utils.Main()