googletest-json-output-unittest.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. from googletest.test import gtest_json_test_utils
  38. from googletest.test 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 = '\n'
  51. EXPECTED_NON_EMPTY = {
  52. 'tests': 28,
  53. 'failures': 5,
  54. 'disabled': 2,
  55. 'errors': 0,
  56. 'timestamp': '*',
  57. 'time': '*',
  58. 'ad_hoc_property': '42',
  59. 'name': 'AllTests',
  60. 'testsuites': [
  61. {
  62. 'name': 'SuccessfulTest',
  63. 'tests': 1,
  64. 'failures': 0,
  65. 'disabled': 0,
  66. 'errors': 0,
  67. 'time': '*',
  68. 'timestamp': '*',
  69. 'testsuite': [{
  70. 'name': 'Succeeds',
  71. 'file': 'gtest_xml_output_unittest_.cc',
  72. 'line': 53,
  73. 'status': 'RUN',
  74. 'result': 'COMPLETED',
  75. 'time': '*',
  76. 'timestamp': '*',
  77. 'classname': 'SuccessfulTest',
  78. }],
  79. },
  80. {
  81. 'name': 'FailedTest',
  82. 'tests': 1,
  83. 'failures': 1,
  84. 'disabled': 0,
  85. 'errors': 0,
  86. 'time': '*',
  87. 'timestamp': '*',
  88. 'testsuite': [{
  89. 'name': 'Fails',
  90. 'file': 'gtest_xml_output_unittest_.cc',
  91. 'line': 61,
  92. 'status': 'RUN',
  93. 'result': 'COMPLETED',
  94. 'time': '*',
  95. 'timestamp': '*',
  96. 'classname': 'FailedTest',
  97. 'failures': [{
  98. 'failure': (
  99. 'gtest_xml_output_unittest_.cc:*\n'
  100. 'Expected equality of these values:\n'
  101. ' 1\n 2'
  102. + STACK_TRACE_TEMPLATE
  103. ),
  104. 'type': '',
  105. }],
  106. }],
  107. },
  108. {
  109. 'name': 'DisabledTest',
  110. 'tests': 1,
  111. 'failures': 0,
  112. 'disabled': 1,
  113. 'errors': 0,
  114. 'time': '*',
  115. 'timestamp': '*',
  116. 'testsuite': [{
  117. 'name': 'DISABLED_test_not_run',
  118. 'file': 'gtest_xml_output_unittest_.cc',
  119. 'line': 68,
  120. 'status': 'NOTRUN',
  121. 'result': 'SUPPRESSED',
  122. 'time': '*',
  123. 'timestamp': '*',
  124. 'classname': 'DisabledTest',
  125. }],
  126. },
  127. {
  128. 'name': 'SkippedTest',
  129. 'tests': 3,
  130. 'failures': 1,
  131. 'disabled': 0,
  132. 'errors': 0,
  133. 'time': '*',
  134. 'timestamp': '*',
  135. 'testsuite': [
  136. {
  137. 'name': 'Skipped',
  138. 'file': 'gtest_xml_output_unittest_.cc',
  139. 'line': 75,
  140. 'status': 'RUN',
  141. 'result': 'SKIPPED',
  142. 'time': '*',
  143. 'timestamp': '*',
  144. 'classname': 'SkippedTest',
  145. 'skipped': [
  146. {'message': 'gtest_xml_output_unittest_.cc:*\n\n'}
  147. ],
  148. },
  149. {
  150. 'name': 'SkippedWithMessage',
  151. 'file': 'gtest_xml_output_unittest_.cc',
  152. 'line': 79,
  153. 'status': 'RUN',
  154. 'result': 'SKIPPED',
  155. 'time': '*',
  156. 'timestamp': '*',
  157. 'classname': 'SkippedTest',
  158. 'skipped': [{
  159. 'message': (
  160. 'gtest_xml_output_unittest_.cc:*\n'
  161. 'It is good practice to tell why you skip a test.\n'
  162. )
  163. }],
  164. },
  165. {
  166. 'name': 'SkippedAfterFailure',
  167. 'file': 'gtest_xml_output_unittest_.cc',
  168. 'line': 83,
  169. 'status': 'RUN',
  170. 'result': 'COMPLETED',
  171. 'time': '*',
  172. 'timestamp': '*',
  173. 'classname': 'SkippedTest',
  174. 'failures': [{
  175. 'failure': (
  176. 'gtest_xml_output_unittest_.cc:*\n'
  177. 'Expected equality of these values:\n'
  178. ' 1\n 2'
  179. + STACK_TRACE_TEMPLATE
  180. ),
  181. 'type': '',
  182. }],
  183. 'skipped': [{
  184. 'message': (
  185. 'gtest_xml_output_unittest_.cc:*\n'
  186. 'It is good practice to tell why you skip a test.\n'
  187. )
  188. }],
  189. },
  190. ],
  191. },
  192. {
  193. 'name': 'MixedResultTest',
  194. 'tests': 3,
  195. 'failures': 1,
  196. 'disabled': 1,
  197. 'errors': 0,
  198. 'time': '*',
  199. 'timestamp': '*',
  200. 'testsuite': [
  201. {
  202. 'name': 'Succeeds',
  203. 'file': 'gtest_xml_output_unittest_.cc',
  204. 'line': 88,
  205. 'status': 'RUN',
  206. 'result': 'COMPLETED',
  207. 'time': '*',
  208. 'timestamp': '*',
  209. 'classname': 'MixedResultTest',
  210. },
  211. {
  212. 'name': 'Fails',
  213. 'file': 'gtest_xml_output_unittest_.cc',
  214. 'line': 93,
  215. 'status': 'RUN',
  216. 'result': 'COMPLETED',
  217. 'time': '*',
  218. 'timestamp': '*',
  219. 'classname': 'MixedResultTest',
  220. 'failures': [
  221. {
  222. 'failure': (
  223. 'gtest_xml_output_unittest_.cc:*\n'
  224. 'Expected equality of these values:\n'
  225. ' 1\n 2'
  226. + STACK_TRACE_TEMPLATE
  227. ),
  228. 'type': '',
  229. },
  230. {
  231. 'failure': (
  232. 'gtest_xml_output_unittest_.cc:*\n'
  233. 'Expected equality of these values:\n'
  234. ' 2\n 3'
  235. + STACK_TRACE_TEMPLATE
  236. ),
  237. 'type': '',
  238. },
  239. ],
  240. },
  241. {
  242. 'name': 'DISABLED_test',
  243. 'file': 'gtest_xml_output_unittest_.cc',
  244. 'line': 98,
  245. 'status': 'NOTRUN',
  246. 'result': 'SUPPRESSED',
  247. 'time': '*',
  248. 'timestamp': '*',
  249. 'classname': 'MixedResultTest',
  250. },
  251. ],
  252. },
  253. {
  254. 'name': 'XmlQuotingTest',
  255. 'tests': 1,
  256. 'failures': 1,
  257. 'disabled': 0,
  258. 'errors': 0,
  259. 'time': '*',
  260. 'timestamp': '*',
  261. 'testsuite': [{
  262. 'name': 'OutputsCData',
  263. 'file': 'gtest_xml_output_unittest_.cc',
  264. 'line': 102,
  265. 'status': 'RUN',
  266. 'result': 'COMPLETED',
  267. 'time': '*',
  268. 'timestamp': '*',
  269. 'classname': 'XmlQuotingTest',
  270. 'failures': [{
  271. 'failure': (
  272. 'gtest_xml_output_unittest_.cc:*\n'
  273. 'Failed\nXML output: <?xml encoding="utf-8">'
  274. '<top><![CDATA[cdata text]]></top>'
  275. + STACK_TRACE_TEMPLATE
  276. ),
  277. 'type': '',
  278. }],
  279. }],
  280. },
  281. {
  282. 'name': 'InvalidCharactersTest',
  283. 'tests': 1,
  284. 'failures': 1,
  285. 'disabled': 0,
  286. 'errors': 0,
  287. 'time': '*',
  288. 'timestamp': '*',
  289. 'testsuite': [{
  290. 'name': 'InvalidCharactersInMessage',
  291. 'file': 'gtest_xml_output_unittest_.cc',
  292. 'line': 109,
  293. 'status': 'RUN',
  294. 'result': 'COMPLETED',
  295. 'time': '*',
  296. 'timestamp': '*',
  297. 'classname': 'InvalidCharactersTest',
  298. 'failures': [{
  299. 'failure': (
  300. 'gtest_xml_output_unittest_.cc:*\n'
  301. 'Failed\nInvalid characters in brackets'
  302. ' [\x01\x02]'
  303. + STACK_TRACE_TEMPLATE
  304. ),
  305. 'type': '',
  306. }],
  307. }],
  308. },
  309. {
  310. 'name': 'PropertyRecordingTest',
  311. 'tests': 4,
  312. 'failures': 0,
  313. 'disabled': 0,
  314. 'errors': 0,
  315. 'time': '*',
  316. 'timestamp': '*',
  317. 'SetUpTestSuite': 'yes',
  318. 'SetUpTestSuite (with whitespace)': 'yes and yes',
  319. 'TearDownTestSuite': 'aye',
  320. 'TearDownTestSuite (with whitespace)': 'aye and aye',
  321. 'testsuite': [
  322. {
  323. 'name': 'OneProperty',
  324. 'file': 'gtest_xml_output_unittest_.cc',
  325. 'line': 125,
  326. 'status': 'RUN',
  327. 'result': 'COMPLETED',
  328. 'time': '*',
  329. 'timestamp': '*',
  330. 'classname': 'PropertyRecordingTest',
  331. 'key_1': '1',
  332. },
  333. {
  334. 'name': 'IntValuedProperty',
  335. 'file': 'gtest_xml_output_unittest_.cc',
  336. 'line': 129,
  337. 'status': 'RUN',
  338. 'result': 'COMPLETED',
  339. 'time': '*',
  340. 'timestamp': '*',
  341. 'classname': 'PropertyRecordingTest',
  342. 'key_int': '1',
  343. },
  344. {
  345. 'name': 'ThreeProperties',
  346. 'file': 'gtest_xml_output_unittest_.cc',
  347. 'line': 133,
  348. 'status': 'RUN',
  349. 'result': 'COMPLETED',
  350. 'time': '*',
  351. 'timestamp': '*',
  352. 'classname': 'PropertyRecordingTest',
  353. 'key_1': '1',
  354. 'key_2': '2',
  355. 'key_3': '3',
  356. },
  357. {
  358. 'name': 'TwoValuesForOneKeyUsesLastValue',
  359. 'file': 'gtest_xml_output_unittest_.cc',
  360. 'line': 139,
  361. 'status': 'RUN',
  362. 'result': 'COMPLETED',
  363. 'time': '*',
  364. 'timestamp': '*',
  365. 'classname': 'PropertyRecordingTest',
  366. 'key_1': '2',
  367. },
  368. ],
  369. },
  370. {
  371. 'name': 'NoFixtureTest',
  372. 'tests': 3,
  373. 'failures': 0,
  374. 'disabled': 0,
  375. 'errors': 0,
  376. 'time': '*',
  377. 'timestamp': '*',
  378. 'testsuite': [
  379. {
  380. 'name': 'RecordProperty',
  381. 'file': 'gtest_xml_output_unittest_.cc',
  382. 'line': 144,
  383. 'status': 'RUN',
  384. 'result': 'COMPLETED',
  385. 'time': '*',
  386. 'timestamp': '*',
  387. 'classname': 'NoFixtureTest',
  388. 'key': '1',
  389. },
  390. {
  391. 'name': 'ExternalUtilityThatCallsRecordIntValuedProperty',
  392. 'file': 'gtest_xml_output_unittest_.cc',
  393. 'line': 157,
  394. 'status': 'RUN',
  395. 'result': 'COMPLETED',
  396. 'time': '*',
  397. 'timestamp': '*',
  398. 'classname': 'NoFixtureTest',
  399. 'key_for_utility_int': '1',
  400. },
  401. {
  402. 'name': (
  403. 'ExternalUtilityThatCallsRecordStringValuedProperty'
  404. ),
  405. 'file': 'gtest_xml_output_unittest_.cc',
  406. 'line': 161,
  407. 'status': 'RUN',
  408. 'result': 'COMPLETED',
  409. 'time': '*',
  410. 'timestamp': '*',
  411. 'classname': 'NoFixtureTest',
  412. 'key_for_utility_string': '1',
  413. },
  414. ],
  415. },
  416. {
  417. 'name': 'SetupFailTest',
  418. 'tests': 1,
  419. 'failures': 0,
  420. 'disabled': 0,
  421. 'errors': 0,
  422. 'time': '*',
  423. 'timestamp': '*',
  424. 'testsuite': [
  425. {
  426. 'name': 'NoopPassingTest',
  427. 'file': 'gtest_xml_output_unittest_.cc',
  428. 'line': 172,
  429. 'status': 'RUN',
  430. 'result': 'SKIPPED',
  431. 'timestamp': '*',
  432. 'time': '*',
  433. 'classname': 'SetupFailTest',
  434. 'skipped': [
  435. {'message': 'gtest_xml_output_unittest_.cc:*\n'}
  436. ],
  437. },
  438. {
  439. 'name': '',
  440. 'status': 'RUN',
  441. 'result': 'COMPLETED',
  442. 'timestamp': '*',
  443. 'time': '*',
  444. 'classname': '',
  445. 'failures': [{
  446. 'failure': (
  447. 'gtest_xml_output_unittest_.cc:*\nExpected equality'
  448. ' of these values:\n 1\n 2'
  449. + STACK_TRACE_TEMPLATE
  450. ),
  451. 'type': '',
  452. }],
  453. },
  454. ],
  455. },
  456. {
  457. 'name': 'TearDownFailTest',
  458. 'tests': 1,
  459. 'failures': 0,
  460. 'disabled': 0,
  461. 'errors': 0,
  462. 'timestamp': '*',
  463. 'time': '*',
  464. 'testsuite': [
  465. {
  466. 'name': 'NoopPassingTest',
  467. 'file': 'gtest_xml_output_unittest_.cc',
  468. 'line': 179,
  469. 'status': 'RUN',
  470. 'result': 'COMPLETED',
  471. 'timestamp': '*',
  472. 'time': '*',
  473. 'classname': 'TearDownFailTest',
  474. },
  475. {
  476. 'name': '',
  477. 'status': 'RUN',
  478. 'result': 'COMPLETED',
  479. 'timestamp': '*',
  480. 'time': '*',
  481. 'classname': '',
  482. 'failures': [{
  483. 'failure': (
  484. 'gtest_xml_output_unittest_.cc:*\nExpected equality'
  485. ' of these values:\n 1\n 2'
  486. + STACK_TRACE_TEMPLATE
  487. ),
  488. 'type': '',
  489. }],
  490. },
  491. ],
  492. },
  493. {
  494. 'name': 'TypedTest/0',
  495. 'tests': 1,
  496. 'failures': 0,
  497. 'disabled': 0,
  498. 'errors': 0,
  499. 'time': '*',
  500. 'timestamp': '*',
  501. 'testsuite': [{
  502. 'name': 'HasTypeParamAttribute',
  503. 'type_param': 'int',
  504. 'file': 'gtest_xml_output_unittest_.cc',
  505. 'line': 193,
  506. 'status': 'RUN',
  507. 'result': 'COMPLETED',
  508. 'time': '*',
  509. 'timestamp': '*',
  510. 'classname': 'TypedTest/0',
  511. }],
  512. },
  513. {
  514. 'name': 'TypedTest/1',
  515. 'tests': 1,
  516. 'failures': 0,
  517. 'disabled': 0,
  518. 'errors': 0,
  519. 'time': '*',
  520. 'timestamp': '*',
  521. 'testsuite': [{
  522. 'name': 'HasTypeParamAttribute',
  523. 'type_param': 'long',
  524. 'file': 'gtest_xml_output_unittest_.cc',
  525. 'line': 193,
  526. 'status': 'RUN',
  527. 'result': 'COMPLETED',
  528. 'time': '*',
  529. 'timestamp': '*',
  530. 'classname': 'TypedTest/1',
  531. }],
  532. },
  533. {
  534. 'name': 'Single/TypeParameterizedTestSuite/0',
  535. 'tests': 1,
  536. 'failures': 0,
  537. 'disabled': 0,
  538. 'errors': 0,
  539. 'time': '*',
  540. 'timestamp': '*',
  541. 'testsuite': [{
  542. 'name': 'HasTypeParamAttribute',
  543. 'type_param': 'int',
  544. 'file': 'gtest_xml_output_unittest_.cc',
  545. 'line': 200,
  546. 'status': 'RUN',
  547. 'result': 'COMPLETED',
  548. 'time': '*',
  549. 'timestamp': '*',
  550. 'classname': 'Single/TypeParameterizedTestSuite/0',
  551. }],
  552. },
  553. {
  554. 'name': 'Single/TypeParameterizedTestSuite/1',
  555. 'tests': 1,
  556. 'failures': 0,
  557. 'disabled': 0,
  558. 'errors': 0,
  559. 'time': '*',
  560. 'timestamp': '*',
  561. 'testsuite': [{
  562. 'name': 'HasTypeParamAttribute',
  563. 'type_param': 'long',
  564. 'file': 'gtest_xml_output_unittest_.cc',
  565. 'line': 200,
  566. 'status': 'RUN',
  567. 'result': 'COMPLETED',
  568. 'time': '*',
  569. 'timestamp': '*',
  570. 'classname': 'Single/TypeParameterizedTestSuite/1',
  571. }],
  572. },
  573. {
  574. 'name': 'Single/ValueParamTest',
  575. 'tests': 4,
  576. 'failures': 0,
  577. 'disabled': 0,
  578. 'errors': 0,
  579. 'time': '*',
  580. 'timestamp': '*',
  581. 'testsuite': [
  582. {
  583. 'name': 'HasValueParamAttribute/0',
  584. 'value_param': '33',
  585. 'file': 'gtest_xml_output_unittest_.cc',
  586. 'line': 184,
  587. 'status': 'RUN',
  588. 'result': 'COMPLETED',
  589. 'time': '*',
  590. 'timestamp': '*',
  591. 'classname': 'Single/ValueParamTest',
  592. },
  593. {
  594. 'name': 'HasValueParamAttribute/1',
  595. 'value_param': '42',
  596. 'file': 'gtest_xml_output_unittest_.cc',
  597. 'line': 184,
  598. 'status': 'RUN',
  599. 'result': 'COMPLETED',
  600. 'time': '*',
  601. 'timestamp': '*',
  602. 'classname': 'Single/ValueParamTest',
  603. },
  604. {
  605. 'name': 'AnotherTestThatHasValueParamAttribute/0',
  606. 'value_param': '33',
  607. 'file': 'gtest_xml_output_unittest_.cc',
  608. 'line': 185,
  609. 'status': 'RUN',
  610. 'result': 'COMPLETED',
  611. 'time': '*',
  612. 'timestamp': '*',
  613. 'classname': 'Single/ValueParamTest',
  614. },
  615. {
  616. 'name': 'AnotherTestThatHasValueParamAttribute/1',
  617. 'value_param': '42',
  618. 'file': 'gtest_xml_output_unittest_.cc',
  619. 'line': 185,
  620. 'status': 'RUN',
  621. 'result': 'COMPLETED',
  622. 'time': '*',
  623. 'timestamp': '*',
  624. 'classname': 'Single/ValueParamTest',
  625. },
  626. ],
  627. },
  628. ],
  629. }
  630. EXPECTED_FILTERED = {
  631. 'tests': 1,
  632. 'failures': 0,
  633. 'disabled': 0,
  634. 'errors': 0,
  635. 'time': '*',
  636. 'timestamp': '*',
  637. 'name': 'AllTests',
  638. 'ad_hoc_property': '42',
  639. 'testsuites': [{
  640. 'name': 'SuccessfulTest',
  641. 'tests': 1,
  642. 'failures': 0,
  643. 'disabled': 0,
  644. 'errors': 0,
  645. 'time': '*',
  646. 'timestamp': '*',
  647. 'testsuite': [{
  648. 'name': 'Succeeds',
  649. 'file': 'gtest_xml_output_unittest_.cc',
  650. 'line': 53,
  651. 'status': 'RUN',
  652. 'result': 'COMPLETED',
  653. 'time': '*',
  654. 'timestamp': '*',
  655. 'classname': 'SuccessfulTest',
  656. }],
  657. }],
  658. }
  659. EXPECTED_NO_TEST = {
  660. 'tests': 0,
  661. 'failures': 0,
  662. 'disabled': 0,
  663. 'errors': 0,
  664. 'time': '*',
  665. 'timestamp': '*',
  666. 'name': 'AllTests',
  667. 'testsuites': [{
  668. 'name': 'NonTestSuiteFailure',
  669. 'tests': 1,
  670. 'failures': 1,
  671. 'disabled': 0,
  672. 'skipped': 0,
  673. 'errors': 0,
  674. 'time': '*',
  675. 'timestamp': '*',
  676. 'testsuite': [{
  677. 'name': '',
  678. 'status': 'RUN',
  679. 'result': 'COMPLETED',
  680. 'time': '*',
  681. 'timestamp': '*',
  682. 'classname': '',
  683. 'failures': [{
  684. 'failure': (
  685. 'gtest_no_test_unittest.cc:*\n'
  686. 'Expected equality of these values:\n'
  687. ' 1\n 2'
  688. + STACK_TRACE_TEMPLATE
  689. ),
  690. 'type': '',
  691. }],
  692. }],
  693. }],
  694. }
  695. GTEST_PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
  696. SUPPORTS_TYPED_TESTS = (
  697. 'TypedTest'
  698. in gtest_test_utils.Subprocess(
  699. [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=False
  700. ).output
  701. )
  702. class GTestJsonOutputUnitTest(gtest_test_utils.TestCase):
  703. """Unit test for Google Test's JSON output functionality."""
  704. # This test currently breaks on platforms that do not support typed and
  705. # type-parameterized tests, so we don't run it under them.
  706. if SUPPORTS_TYPED_TESTS:
  707. def testNonEmptyJsonOutput(self):
  708. """Verifies JSON output for a Google Test binary with non-empty output.
  709. Runs a test program that generates a non-empty JSON output, and
  710. tests that the JSON output is expected.
  711. """
  712. self._TestJsonOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY, 1)
  713. def testNoTestJsonOutput(self):
  714. """Verifies JSON output for a Google Test binary without actual tests.
  715. Runs a test program that generates an JSON output for a binary with no
  716. tests, and tests that the JSON output is expected.
  717. """
  718. self._TestJsonOutput('gtest_no_test_unittest', EXPECTED_NO_TEST, 0)
  719. def testTimestampValue(self):
  720. """Checks whether the timestamp attribute in the JSON output is valid.
  721. Runs a test program that generates an empty JSON output, and checks if
  722. the timestamp attribute in the testsuites tag is valid.
  723. """
  724. actual = self._GetJsonOutput('gtest_no_test_unittest', [], 0)
  725. date_time_str = actual['timestamp']
  726. # datetime.strptime() is only available in Python 2.5+ so we have to
  727. # parse the expected datetime manually.
  728. match = re.match(r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
  729. self.assertTrue(
  730. re.match,
  731. 'JSON datettime string %s has incorrect format' % date_time_str,
  732. )
  733. date_time_from_json = datetime.datetime(
  734. year=int(match.group(1)),
  735. month=int(match.group(2)),
  736. day=int(match.group(3)),
  737. hour=int(match.group(4)),
  738. minute=int(match.group(5)),
  739. second=int(match.group(6)),
  740. )
  741. time_delta = abs(datetime.datetime.now() - date_time_from_json)
  742. # timestamp value should be near the current local time
  743. self.assertTrue(
  744. time_delta < datetime.timedelta(seconds=600),
  745. 'time_delta is %s' % time_delta,
  746. )
  747. def testDefaultOutputFile(self):
  748. """Verifies the default output file name.
  749. Confirms that Google Test produces an JSON output file with the expected
  750. default name if no name is explicitly specified.
  751. """
  752. output_file = os.path.join(
  753. gtest_test_utils.GetTempDir(), GTEST_DEFAULT_OUTPUT_FILE
  754. )
  755. gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
  756. 'gtest_no_test_unittest'
  757. )
  758. try:
  759. os.remove(output_file)
  760. except OSError:
  761. e = sys.exc_info()[1]
  762. if e.errno != errno.ENOENT:
  763. raise
  764. p = gtest_test_utils.Subprocess(
  765. [gtest_prog_path, '%s=json' % GTEST_OUTPUT_FLAG],
  766. working_dir=gtest_test_utils.GetTempDir(),
  767. )
  768. self.assertTrue(p.exited)
  769. self.assertEqual(0, p.exit_code)
  770. self.assertTrue(os.path.isfile(output_file))
  771. def testSuppressedJsonOutput(self):
  772. """Verifies that no JSON output is generated.
  773. Tests that no JSON file is generated if the default JSON listener is
  774. shut down before RUN_ALL_TESTS is invoked.
  775. """
  776. json_path = os.path.join(
  777. gtest_test_utils.GetTempDir(), GTEST_PROGRAM_NAME + 'out.json'
  778. )
  779. if os.path.isfile(json_path):
  780. os.remove(json_path)
  781. command = [
  782. GTEST_PROGRAM_PATH,
  783. '%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path),
  784. '--shut_down_xml',
  785. ]
  786. p = gtest_test_utils.Subprocess(command)
  787. if p.terminated_by_signal:
  788. # p.signal is available only if p.terminated_by_signal is True.
  789. self.assertFalse(
  790. p.terminated_by_signal,
  791. '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal),
  792. )
  793. else:
  794. self.assertTrue(p.exited)
  795. self.assertEqual(
  796. 1,
  797. p.exit_code,
  798. "'%s' exited with code %s, which doesn't match "
  799. 'the expected exit code %s.' % (command, p.exit_code, 1),
  800. )
  801. self.assertTrue(not os.path.isfile(json_path))
  802. def testFilteredTestJsonOutput(self):
  803. """Verifies JSON output when a filter is applied.
  804. Runs a test program that executes only some tests and verifies that
  805. non-selected tests do not show up in the JSON output.
  806. """
  807. self._TestJsonOutput(
  808. GTEST_PROGRAM_NAME,
  809. EXPECTED_FILTERED,
  810. 0,
  811. extra_args=['%s=SuccessfulTest.*' % GTEST_FILTER_FLAG],
  812. )
  813. def _GetJsonOutput(self, gtest_prog_name, extra_args, expected_exit_code):
  814. """Returns the JSON output generated by running the program gtest_prog_name.
  815. Furthermore, the program's exit code must be expected_exit_code.
  816. Args:
  817. gtest_prog_name: Google Test binary name.
  818. extra_args: extra arguments to binary invocation.
  819. expected_exit_code: program's exit code.
  820. """
  821. json_path = os.path.join(
  822. gtest_test_utils.GetTempDir(), gtest_prog_name + 'out.json'
  823. )
  824. gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
  825. command = [
  826. gtest_prog_path,
  827. '%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path),
  828. ] + extra_args
  829. p = gtest_test_utils.Subprocess(command)
  830. if p.terminated_by_signal:
  831. self.assertTrue(
  832. False, '%s was killed by signal %d' % (gtest_prog_name, p.signal)
  833. )
  834. else:
  835. self.assertTrue(p.exited)
  836. self.assertEqual(
  837. expected_exit_code,
  838. p.exit_code,
  839. "'%s' exited with code %s, which doesn't match "
  840. 'the expected exit code %s.'
  841. % (command, p.exit_code, expected_exit_code),
  842. )
  843. with open(json_path) as f:
  844. actual = json.load(f)
  845. return actual
  846. def _TestJsonOutput(
  847. self, gtest_prog_name, expected, expected_exit_code, extra_args=None
  848. ):
  849. """Checks the JSON output generated by the Google Test binary.
  850. Asserts that the JSON document generated by running the program
  851. gtest_prog_name matches expected_json, a string containing another
  852. JSON document. Furthermore, the program's exit code must be
  853. expected_exit_code.
  854. Args:
  855. gtest_prog_name: Google Test binary name.
  856. expected: expected output.
  857. expected_exit_code: program's exit code.
  858. extra_args: extra arguments to binary invocation.
  859. """
  860. actual = self._GetJsonOutput(
  861. gtest_prog_name, extra_args or [], expected_exit_code
  862. )
  863. self.assertEqual(expected, gtest_json_test_utils.normalize(actual))
  864. if __name__ == '__main__':
  865. if NO_STACKTRACE_SUPPORT_FLAG in sys.argv:
  866. # unittest.main() can't handle unknown flags
  867. sys.argv.remove(NO_STACKTRACE_SUPPORT_FLAG)
  868. os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
  869. gtest_test_utils.Main()