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 from googletest.test import gtest_json_test_utils
41 from googletest.test 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 = {
93 u'file': u'gtest_xml_output_unittest_.cc',
96 u'result': u'COMPLETED',
99 u'classname': u'SuccessfulTest'
120 u'gtest_xml_output_unittest_.cc',
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,
156 u'name': u'DISABLED_test_not_run',
157 u'file': u'gtest_xml_output_unittest_.cc',
159 u'status': u'NOTRUN',
160 u'result': u'SUPPRESSED',
163 u'classname': u'DisabledTest'
182 u'file': 'gtest_xml_output_unittest_.cc',
185 u'result': u'SKIPPED',
188 u'classname': u'SkippedTest'
190 u'name': u'SkippedWithMessage',
191 u'file': 'gtest_xml_output_unittest_.cc',
194 u'result': u'SKIPPED',
197 u'classname': u'SkippedTest'
200 u'SkippedAfterFailure',
202 'gtest_xml_output_unittest_.cc',
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,
238 u'name': u'Succeeds',
239 u'file': 'gtest_xml_output_unittest_.cc',
242 u'result': u'COMPLETED',
245 u'classname': u'MixedResultTest'
250 u'gtest_xml_output_unittest_.cc',
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,
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,
275 u'name': u'DISABLED_test',
276 u'file': u'gtest_xml_output_unittest_.cc',
278 u'status': u'NOTRUN',
279 u'result': u'SUPPRESSED',
282 u'classname': u'MixedResultTest'
303 u'gtest_xml_output_unittest_.cc',
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,
326 u'InvalidCharactersTest',
341 u'InvalidCharactersInMessage',
343 u'gtest_xml_output_unittest_.cc',
355 u'InvalidCharactersTest',
357 u'failure': u'gtest_xml_output_unittest_.cc:*\n'
358 u'Failed\nInvalid characters in brackets'
359 u' [\x01\x02]' + STACK_TRACE_TEMPLATE,
365 u'PropertyRecordingTest',
380 u'TearDownTestSuite':
383 u'name': u'OneProperty',
384 u'file': u'gtest_xml_output_unittest_.cc',
387 u'result': u'COMPLETED',
390 u'classname': u'PropertyRecordingTest',
393 u'name': u'IntValuedProperty',
394 u'file': u'gtest_xml_output_unittest_.cc',
397 u'result': u'COMPLETED',
400 u'classname': u'PropertyRecordingTest',
403 u'name': u'ThreeProperties',
404 u'file': u'gtest_xml_output_unittest_.cc',
407 u'result': u'COMPLETED',
410 u'classname': u'PropertyRecordingTest',
415 u'name': u'TwoValuesForOneKeyUsesLastValue',
416 u'file': u'gtest_xml_output_unittest_.cc',
419 u'result': u'COMPLETED',
422 u'classname': u'PropertyRecordingTest',
441 u'name': u'RecordProperty',
442 u'file': u'gtest_xml_output_unittest_.cc',
445 u'result': u'COMPLETED',
448 u'classname': u'NoFixtureTest',
451 u'name': u'ExternalUtilityThatCallsRecordIntValuedProperty',
452 u'file': u'gtest_xml_output_unittest_.cc',
455 u'result': u'COMPLETED',
458 u'classname': u'NoFixtureTest',
459 u'key_for_utility_int': u'1'
461 u'name': u'ExternalUtilityThatCallsRecordStringValuedProperty',
462 u'file': u'gtest_xml_output_unittest_.cc',
465 u'result': u'COMPLETED',
468 u'classname': u'NoFixtureTest',
469 u'key_for_utility_string': u'1'
487 u'name': u'HasTypeParamAttribute',
488 u'type_param': u'int',
489 u'file': u'gtest_xml_output_unittest_.cc',
492 u'result': u'COMPLETED',
495 u'classname': u'TypedTest/0'
513 u'name': u'HasTypeParamAttribute',
514 u'type_param': u'long',
515 u'file': u'gtest_xml_output_unittest_.cc',
518 u'result': u'COMPLETED',
521 u'classname': u'TypedTest/1'
525 u'Single/TypeParameterizedTestSuite/0',
539 u'name': u'HasTypeParamAttribute',
540 u'type_param': u'int',
541 u'file': u'gtest_xml_output_unittest_.cc',
544 u'result': u'COMPLETED',
547 u'classname': u'Single/TypeParameterizedTestSuite/0'
551 u'Single/TypeParameterizedTestSuite/1',
565 u'name': u'HasTypeParamAttribute',
566 u'type_param': u'long',
567 u'file': u'gtest_xml_output_unittest_.cc',
570 u'result': u'COMPLETED',
573 u'classname': u'Single/TypeParameterizedTestSuite/1'
577 u'Single/ValueParamTest',
591 u'name': u'HasValueParamAttribute/0',
592 u'value_param': u'33',
593 u'file': u'gtest_xml_output_unittest_.cc',
596 u'result': u'COMPLETED',
599 u'classname': u'Single/ValueParamTest'
601 u'name': u'HasValueParamAttribute/1',
602 u'value_param': u'42',
603 u'file': u'gtest_xml_output_unittest_.cc',
606 u'result': u'COMPLETED',
609 u'classname': u'Single/ValueParamTest'
611 u'name': u'AnotherTestThatHasValueParamAttribute/0',
612 u'value_param': u'33',
613 u'file': u'gtest_xml_output_unittest_.cc',
616 u'result': u'COMPLETED',
619 u'classname': u'Single/ValueParamTest'
621 u'name': u'AnotherTestThatHasValueParamAttribute/1',
622 u'value_param': u'42',
623 u'file': u'gtest_xml_output_unittest_.cc',
626 u'result': u'COMPLETED',
629 u'classname': u'Single/ValueParamTest'
634 EXPECTED_FILTERED = {
667 u'name': u'Succeeds',
668 u'file': u'gtest_xml_output_unittest_.cc',
671 u'result': u'COMPLETED',
674 u'classname': u'SuccessfulTest',
696 u'NonTestSuiteFailure',
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,
734 GTEST_PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
736 SUPPORTS_TYPED_TESTS = 'TypedTest' in gtest_test_utils.Subprocess(
737 [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=False).output
740 class GTestJsonOutputUnitTest(gtest_test_utils.TestCase):
741 """Unit test for Google Test's JSON output functionality.
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:
748 def testNonEmptyJsonOutput(self):
749 """Verifies JSON output for a Google Test binary with non-empty output.
751 Runs a test program that generates a non-empty JSON output, and
752 tests that the JSON output is expected.
754 self._TestJsonOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY, 1)
756 def testNoTestJsonOutput(self):
757 """Verifies JSON output for a Google Test binary without actual tests.
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.
763 self._TestJsonOutput('gtest_no_test_unittest', EXPECTED_NO_TEST, 0)
765 def testTimestampValue(self):
766 """Checks whether the timestamp attribute in the JSON output is valid.
768 Runs a test program that generates an empty JSON output, and checks if
769 the timestamp attribute in the testsuites tag is valid.
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)
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)))
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)
789 def testDefaultOutputFile(self):
790 """Verifies the default output file name.
792 Confirms that Google Test produces an JSON output file with the expected
793 default name if no name is explicitly specified.
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')
800 os.remove(output_file)
802 e = sys.exc_info()[1]
803 if e.errno != errno.ENOENT:
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))
813 def testSuppressedJsonOutput(self):
814 """Verifies that no JSON output is generated.
816 Tests that no JSON file is generated if the default JSON listener is
817 shut down before RUN_ALL_TESTS is invoked.
820 json_path = os.path.join(gtest_test_utils.GetTempDir(),
821 GTEST_PROGRAM_NAME + 'out.json')
822 if os.path.isfile(json_path):
825 command = [GTEST_PROGRAM_PATH,
826 '%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path),
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.
832 p.terminated_by_signal,
833 '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal))
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))
841 self.assert_(not os.path.isfile(json_path))
843 def testFilteredTestJsonOutput(self):
844 """Verifies JSON output when a filter is applied.
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.
850 self._TestJsonOutput(GTEST_PROGRAM_NAME, EXPECTED_FILTERED, 0,
851 extra_args=['%s=SuccessfulTest.*' % GTEST_FILTER_FLAG])
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.
856 Furthermore, the program's exit code must be expected_exit_code.
859 gtest_prog_name: Google Test binary name.
860 extra_args: extra arguments to binary invocation.
861 expected_exit_code: program's exit code.
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)
868 [gtest_prog_path, '%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path)] +
871 p = gtest_test_utils.Subprocess(command)
872 if p.terminated_by_signal:
874 '%s was killed by signal %d' % (gtest_prog_name, p.signal))
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)
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.
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
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.
901 actual = self._GetJsonOutput(gtest_prog_name, extra_args or [],
903 self.assertEqual(expected, gtest_json_test_utils.normalize(actual))
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)
911 os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
912 gtest_test_utils.Main()