2 # Copyright 2018, Google Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
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
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.
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.
31 """Unit test for the gtest_json_output module."""
40 import gtest_json_test_utils
41 import gtest_test_utils
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_'
49 # The flag indicating stacktraces are not supported
50 NO_STACKTRACE_SUPPORT_FLAG
= '--no_stacktrace_support'
52 SUPPORTS_STACK_TRACES
= NO_STACKTRACE_SUPPORT_FLAG
not in sys
.argv
54 if SUPPORTS_STACK_TRACES
:
55 STACK_TRACE_TEMPLATE
= '\nStack trace:\n*'
57 STACK_TRACE_TEMPLATE
= ''
59 EXPECTED_NON_EMPTY
= {
94 u
'result': u
'COMPLETED',
97 u
'classname': u
'SuccessfulTest'
128 u
'failure': u
'gtest_xml_output_unittest_.cc:*\n'
129 u
'Expected equality of these values:\n'
130 u
' 1\n 2' + STACK_TRACE_TEMPLATE
,
150 u
'name': u
'DISABLED_test_not_run',
151 u
'status': u
'NOTRUN',
152 u
'result': u
'SUPPRESSED',
155 u
'classname': u
'DisabledTest'
175 u
'result': u
'SKIPPED',
178 u
'classname': u
'SkippedTest'
180 u
'name': u
'SkippedWithMessage',
182 u
'result': u
'SKIPPED',
185 u
'classname': u
'SkippedTest'
188 u
'SkippedAfterFailure',
200 u
'failure': u
'gtest_xml_output_unittest_.cc:*\n'
201 u
'Expected equality of these values:\n'
202 u
' 1\n 2' + STACK_TRACE_TEMPLATE
,
222 u
'name': u
'Succeeds',
224 u
'result': u
'COMPLETED',
227 u
'classname': u
'MixedResultTest'
242 u
'failure': u
'gtest_xml_output_unittest_.cc:*\n'
243 u
'Expected equality of these values:\n'
244 u
' 1\n 2' + STACK_TRACE_TEMPLATE
,
247 u
'failure': u
'gtest_xml_output_unittest_.cc:*\n'
248 u
'Expected equality of these values:\n'
249 u
' 2\n 3' + STACK_TRACE_TEMPLATE
,
253 u
'name': u
'DISABLED_test',
254 u
'status': u
'NOTRUN',
255 u
'result': u
'SUPPRESSED',
258 u
'classname': u
'MixedResultTest'
289 u
'failure': u
'gtest_xml_output_unittest_.cc:*\n'
290 u
'Failed\nXML output: <?xml encoding="utf-8">'
291 u
'<top><![CDATA[cdata text]]></top>' +
292 STACK_TRACE_TEMPLATE
,
298 u
'InvalidCharactersTest',
313 u
'InvalidCharactersInMessage',
323 u
'InvalidCharactersTest',
325 u
'failure': u
'gtest_xml_output_unittest_.cc:*\n'
326 u
'Failed\nInvalid characters in brackets'
327 u
' [\x01\x02]' + STACK_TRACE_TEMPLATE
,
333 u
'PropertyRecordingTest',
348 u
'TearDownTestSuite':
351 u
'name': u
'OneProperty',
353 u
'result': u
'COMPLETED',
356 u
'classname': u
'PropertyRecordingTest',
359 u
'name': u
'IntValuedProperty',
361 u
'result': u
'COMPLETED',
364 u
'classname': u
'PropertyRecordingTest',
367 u
'name': u
'ThreeProperties',
369 u
'result': u
'COMPLETED',
372 u
'classname': u
'PropertyRecordingTest',
377 u
'name': u
'TwoValuesForOneKeyUsesLastValue',
379 u
'result': u
'COMPLETED',
382 u
'classname': u
'PropertyRecordingTest',
401 u
'name': u
'RecordProperty',
403 u
'result': u
'COMPLETED',
406 u
'classname': u
'NoFixtureTest',
409 u
'name': u
'ExternalUtilityThatCallsRecordIntValuedProperty',
411 u
'result': u
'COMPLETED',
414 u
'classname': u
'NoFixtureTest',
415 u
'key_for_utility_int': u
'1'
417 u
'name': u
'ExternalUtilityThatCallsRecordStringValuedProperty',
419 u
'result': u
'COMPLETED',
422 u
'classname': u
'NoFixtureTest',
423 u
'key_for_utility_string': u
'1'
441 u
'name': u
'HasTypeParamAttribute',
442 u
'type_param': u
'int',
444 u
'result': u
'COMPLETED',
447 u
'classname': u
'TypedTest/0'
465 u
'name': u
'HasTypeParamAttribute',
466 u
'type_param': u
'long',
468 u
'result': u
'COMPLETED',
471 u
'classname': u
'TypedTest/1'
475 u
'Single/TypeParameterizedTestSuite/0',
489 u
'name': u
'HasTypeParamAttribute',
490 u
'type_param': u
'int',
492 u
'result': u
'COMPLETED',
495 u
'classname': u
'Single/TypeParameterizedTestSuite/0'
499 u
'Single/TypeParameterizedTestSuite/1',
513 u
'name': u
'HasTypeParamAttribute',
514 u
'type_param': u
'long',
516 u
'result': u
'COMPLETED',
519 u
'classname': u
'Single/TypeParameterizedTestSuite/1'
523 u
'Single/ValueParamTest',
537 u
'name': u
'HasValueParamAttribute/0',
538 u
'value_param': u
'33',
540 u
'result': u
'COMPLETED',
543 u
'classname': u
'Single/ValueParamTest'
545 u
'name': u
'HasValueParamAttribute/1',
546 u
'value_param': u
'42',
548 u
'result': u
'COMPLETED',
551 u
'classname': u
'Single/ValueParamTest'
553 u
'name': u
'AnotherTestThatHasValueParamAttribute/0',
554 u
'value_param': u
'33',
556 u
'result': u
'COMPLETED',
559 u
'classname': u
'Single/ValueParamTest'
561 u
'name': u
'AnotherTestThatHasValueParamAttribute/1',
562 u
'value_param': u
'42',
564 u
'result': u
'COMPLETED',
567 u
'classname': u
'Single/ValueParamTest'
572 EXPECTED_FILTERED
= {
605 u
'name': u
'Succeeds',
607 u
'result': u
'COMPLETED',
610 u
'classname': u
'SuccessfulTest',
622 u
'name': u
'AllTests',
626 GTEST_PROGRAM_PATH
= gtest_test_utils
.GetTestExecutablePath(GTEST_PROGRAM_NAME
)
628 SUPPORTS_TYPED_TESTS
= 'TypedTest' in gtest_test_utils
.Subprocess(
629 [GTEST_PROGRAM_PATH
, GTEST_LIST_TESTS_FLAG
], capture_stderr
=False).output
632 class GTestJsonOutputUnitTest(gtest_test_utils
.TestCase
):
633 """Unit test for Google Test's JSON output functionality.
636 # This test currently breaks on platforms that do not support typed and
637 # type-parameterized tests, so we don't run it under them.
638 if SUPPORTS_TYPED_TESTS
:
640 def testNonEmptyJsonOutput(self
):
641 """Verifies JSON output for a Google Test binary with non-empty output.
643 Runs a test program that generates a non-empty JSON output, and
644 tests that the JSON output is expected.
646 self
._TestJsonOutput
(GTEST_PROGRAM_NAME
, EXPECTED_NON_EMPTY
, 1)
648 def testEmptyJsonOutput(self
):
649 """Verifies JSON output for a Google Test binary without actual tests.
651 Runs a test program that generates an empty JSON output, and
652 tests that the JSON output is expected.
655 self
._TestJsonOutput
('gtest_no_test_unittest', EXPECTED_EMPTY
, 0)
657 def testTimestampValue(self
):
658 """Checks whether the timestamp attribute in the JSON output is valid.
660 Runs a test program that generates an empty JSON output, and checks if
661 the timestamp attribute in the testsuites tag is valid.
663 actual
= self
._GetJsonOutput
('gtest_no_test_unittest', [], 0)
664 date_time_str
= actual
['timestamp']
665 # datetime.strptime() is only available in Python 2.5+ so we have to
666 # parse the expected datetime manually.
667 match
= re
.match(r
'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str
)
670 'JSON datettime string %s has incorrect format' % date_time_str
)
671 date_time_from_json
= datetime
.datetime(
672 year
=int(match
.group(1)), month
=int(match
.group(2)),
673 day
=int(match
.group(3)), hour
=int(match
.group(4)),
674 minute
=int(match
.group(5)), second
=int(match
.group(6)))
676 time_delta
= abs(datetime
.datetime
.now() - date_time_from_json
)
677 # timestamp value should be near the current local time
678 self
.assertTrue(time_delta
< datetime
.timedelta(seconds
=600),
679 'time_delta is %s' % time_delta
)
681 def testDefaultOutputFile(self
):
682 """Verifies the default output file name.
684 Confirms that Google Test produces an JSON output file with the expected
685 default name if no name is explicitly specified.
687 output_file
= os
.path
.join(gtest_test_utils
.GetTempDir(),
688 GTEST_DEFAULT_OUTPUT_FILE
)
689 gtest_prog_path
= gtest_test_utils
.GetTestExecutablePath(
690 'gtest_no_test_unittest')
692 os
.remove(output_file
)
694 e
= sys
.exc_info()[1]
695 if e
.errno
!= errno
.ENOENT
:
698 p
= gtest_test_utils
.Subprocess(
699 [gtest_prog_path
, '%s=json' % GTEST_OUTPUT_FLAG
],
700 working_dir
=gtest_test_utils
.GetTempDir())
701 self
.assert_(p
.exited
)
702 self
.assertEquals(0, p
.exit_code
)
703 self
.assert_(os
.path
.isfile(output_file
))
705 def testSuppressedJsonOutput(self
):
706 """Verifies that no JSON output is generated.
708 Tests that no JSON file is generated if the default JSON listener is
709 shut down before RUN_ALL_TESTS is invoked.
712 json_path
= os
.path
.join(gtest_test_utils
.GetTempDir(),
713 GTEST_PROGRAM_NAME
+ 'out.json')
714 if os
.path
.isfile(json_path
):
717 command
= [GTEST_PROGRAM_PATH
,
718 '%s=json:%s' % (GTEST_OUTPUT_FLAG
, json_path
),
720 p
= gtest_test_utils
.Subprocess(command
)
721 if p
.terminated_by_signal
:
722 # p.signal is available only if p.terminated_by_signal is True.
724 p
.terminated_by_signal
,
725 '%s was killed by signal %d' % (GTEST_PROGRAM_NAME
, p
.signal
))
727 self
.assert_(p
.exited
)
728 self
.assertEquals(1, p
.exit_code
,
729 "'%s' exited with code %s, which doesn't match "
730 'the expected exit code %s.'
731 % (command
, p
.exit_code
, 1))
733 self
.assert_(not os
.path
.isfile(json_path
))
735 def testFilteredTestJsonOutput(self
):
736 """Verifies JSON output when a filter is applied.
738 Runs a test program that executes only some tests and verifies that
739 non-selected tests do not show up in the JSON output.
742 self
._TestJsonOutput
(GTEST_PROGRAM_NAME
, EXPECTED_FILTERED
, 0,
743 extra_args
=['%s=SuccessfulTest.*' % GTEST_FILTER_FLAG
])
745 def _GetJsonOutput(self
, gtest_prog_name
, extra_args
, expected_exit_code
):
746 """Returns the JSON output generated by running the program gtest_prog_name.
748 Furthermore, the program's exit code must be expected_exit_code.
751 gtest_prog_name: Google Test binary name.
752 extra_args: extra arguments to binary invocation.
753 expected_exit_code: program's exit code.
755 json_path
= os
.path
.join(gtest_test_utils
.GetTempDir(),
756 gtest_prog_name
+ 'out.json')
757 gtest_prog_path
= gtest_test_utils
.GetTestExecutablePath(gtest_prog_name
)
760 [gtest_prog_path
, '%s=json:%s' % (GTEST_OUTPUT_FLAG
, json_path
)] +
763 p
= gtest_test_utils
.Subprocess(command
)
764 if p
.terminated_by_signal
:
766 '%s was killed by signal %d' % (gtest_prog_name
, p
.signal
))
768 self
.assert_(p
.exited
)
769 self
.assertEquals(expected_exit_code
, p
.exit_code
,
770 "'%s' exited with code %s, which doesn't match "
771 'the expected exit code %s.'
772 % (command
, p
.exit_code
, expected_exit_code
))
773 with
open(json_path
) as f
:
774 actual
= json
.load(f
)
777 def _TestJsonOutput(self
, gtest_prog_name
, expected
,
778 expected_exit_code
, extra_args
=None):
779 """Checks the JSON output generated by the Google Test binary.
781 Asserts that the JSON document generated by running the program
782 gtest_prog_name matches expected_json, a string containing another
783 JSON document. Furthermore, the program's exit code must be
787 gtest_prog_name: Google Test binary name.
788 expected: expected output.
789 expected_exit_code: program's exit code.
790 extra_args: extra arguments to binary invocation.
793 actual
= self
._GetJsonOutput
(gtest_prog_name
, extra_args
or [],
795 self
.assertEqual(expected
, gtest_json_test_utils
.normalize(actual
))
798 if __name__
== '__main__':
799 if NO_STACKTRACE_SUPPORT_FLAG
in sys
.argv
:
800 # unittest.main() can't handle unknown flags
801 sys
.argv
.remove(NO_STACKTRACE_SUPPORT_FLAG
)
803 os
.environ
['GTEST_STACK_TRACE_DEPTH'] = '1'
804 gtest_test_utils
.Main()