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