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 = '\n'
59 EXPECTED_NON_EMPTY = {
66 'ad_hoc_property': '42',
70 'name': 'SuccessfulTest',
79 'file': 'gtest_xml_output_unittest_.cc',
82 'result': 'COMPLETED',
85 'classname': 'SuccessfulTest',
98 'file': 'gtest_xml_output_unittest_.cc',
101 'result': 'COMPLETED',
104 'classname': 'FailedTest',
107 'gtest_xml_output_unittest_.cc:*\n'
108 'Expected equality of these values:\n'
110 + STACK_TRACE_TEMPLATE
117 'name': 'DisabledTest',
125 'name': 'DISABLED_test_not_run',
126 'file': 'gtest_xml_output_unittest_.cc',
129 'result': 'SUPPRESSED',
132 'classname': 'DisabledTest',
136 'name': 'SkippedTest',
146 'file': 'gtest_xml_output_unittest_.cc',
152 'classname': 'SkippedTest',
155 'name': 'SkippedWithMessage',
156 'file': 'gtest_xml_output_unittest_.cc',
162 'classname': 'SkippedTest',
165 'name': 'SkippedAfterFailure',
166 'file': 'gtest_xml_output_unittest_.cc',
169 'result': 'COMPLETED',
172 'classname': 'SkippedTest',
175 'gtest_xml_output_unittest_.cc:*\n'
176 'Expected equality of these values:\n'
178 + STACK_TRACE_TEMPLATE
186 'name': 'MixedResultTest',
196 'file': 'gtest_xml_output_unittest_.cc',
199 'result': 'COMPLETED',
202 'classname': 'MixedResultTest',
206 'file': 'gtest_xml_output_unittest_.cc',
209 'result': 'COMPLETED',
212 'classname': 'MixedResultTest',
216 'gtest_xml_output_unittest_.cc:*\n'
217 'Expected equality of these values:\n'
219 + STACK_TRACE_TEMPLATE
225 'gtest_xml_output_unittest_.cc:*\n'
226 'Expected equality of these values:\n'
228 + STACK_TRACE_TEMPLATE
235 'name': 'DISABLED_test',
236 'file': 'gtest_xml_output_unittest_.cc',
239 'result': 'SUPPRESSED',
242 'classname': 'MixedResultTest',
247 'name': 'XmlQuotingTest',
255 'name': 'OutputsCData',
256 'file': 'gtest_xml_output_unittest_.cc',
259 'result': 'COMPLETED',
262 'classname': 'XmlQuotingTest',
265 'gtest_xml_output_unittest_.cc:*\n'
266 'Failed\nXML output: <?xml encoding="utf-8">'
267 '<top><![CDATA[cdata text]]></top>'
268 + STACK_TRACE_TEMPLATE
275 'name': 'InvalidCharactersTest',
283 'name': 'InvalidCharactersInMessage',
284 'file': 'gtest_xml_output_unittest_.cc',
287 'result': 'COMPLETED',
290 'classname': 'InvalidCharactersTest',
293 'gtest_xml_output_unittest_.cc:*\n'
294 'Failed\nInvalid characters in brackets'
296 + STACK_TRACE_TEMPLATE
303 'name': 'PropertyRecordingTest',
310 'SetUpTestSuite': 'yes',
311 'TearDownTestSuite': 'aye',
314 'name': 'OneProperty',
315 'file': 'gtest_xml_output_unittest_.cc',
318 'result': 'COMPLETED',
321 'classname': 'PropertyRecordingTest',
325 'name': 'IntValuedProperty',
326 'file': 'gtest_xml_output_unittest_.cc',
329 'result': 'COMPLETED',
332 'classname': 'PropertyRecordingTest',
336 'name': 'ThreeProperties',
337 'file': 'gtest_xml_output_unittest_.cc',
340 'result': 'COMPLETED',
343 'classname': 'PropertyRecordingTest',
349 'name': 'TwoValuesForOneKeyUsesLastValue',
350 'file': 'gtest_xml_output_unittest_.cc',
353 'result': 'COMPLETED',
356 'classname': 'PropertyRecordingTest',
362 'name': 'NoFixtureTest',
371 'name': 'RecordProperty',
372 'file': 'gtest_xml_output_unittest_.cc',
375 'result': 'COMPLETED',
378 'classname': 'NoFixtureTest',
382 'name': 'ExternalUtilityThatCallsRecordIntValuedProperty',
383 'file': 'gtest_xml_output_unittest_.cc',
386 'result': 'COMPLETED',
389 'classname': 'NoFixtureTest',
390 'key_for_utility_int': '1',
394 'ExternalUtilityThatCallsRecordStringValuedProperty'
396 'file': 'gtest_xml_output_unittest_.cc',
399 'result': 'COMPLETED',
402 'classname': 'NoFixtureTest',
403 'key_for_utility_string': '1',
408 'name': 'TypedTest/0',
416 'name': 'HasTypeParamAttribute',
418 'file': 'gtest_xml_output_unittest_.cc',
421 'result': 'COMPLETED',
424 'classname': 'TypedTest/0',
428 'name': 'TypedTest/1',
436 'name': 'HasTypeParamAttribute',
437 'type_param': 'long',
438 'file': 'gtest_xml_output_unittest_.cc',
441 'result': 'COMPLETED',
444 'classname': 'TypedTest/1',
448 'name': 'Single/TypeParameterizedTestSuite/0',
456 'name': 'HasTypeParamAttribute',
458 'file': 'gtest_xml_output_unittest_.cc',
461 'result': 'COMPLETED',
464 'classname': 'Single/TypeParameterizedTestSuite/0',
468 'name': 'Single/TypeParameterizedTestSuite/1',
476 'name': 'HasTypeParamAttribute',
477 'type_param': 'long',
478 'file': 'gtest_xml_output_unittest_.cc',
481 'result': 'COMPLETED',
484 'classname': 'Single/TypeParameterizedTestSuite/1',
488 'name': 'Single/ValueParamTest',
497 'name': 'HasValueParamAttribute/0',
499 'file': 'gtest_xml_output_unittest_.cc',
502 'result': 'COMPLETED',
505 'classname': 'Single/ValueParamTest',
508 'name': 'HasValueParamAttribute/1',
510 'file': 'gtest_xml_output_unittest_.cc',
513 'result': 'COMPLETED',
516 'classname': 'Single/ValueParamTest',
519 'name': 'AnotherTestThatHasValueParamAttribute/0',
521 'file': 'gtest_xml_output_unittest_.cc',
524 'result': 'COMPLETED',
527 'classname': 'Single/ValueParamTest',
530 'name': 'AnotherTestThatHasValueParamAttribute/1',
532 'file': 'gtest_xml_output_unittest_.cc',
535 'result': 'COMPLETED',
538 'classname': 'Single/ValueParamTest',
545 EXPECTED_FILTERED = {
553 'ad_hoc_property': '42',
555 'name': 'SuccessfulTest',
564 'file': 'gtest_xml_output_unittest_.cc',
567 'result': 'COMPLETED',
570 'classname': 'SuccessfulTest',
584 'name': 'NonTestSuiteFailure',
595 'result': 'COMPLETED',
601 'gtest_no_test_unittest.cc:*\n'
602 'Expected equality of these values:\n'
604 + STACK_TRACE_TEMPLATE
612 GTEST_PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
614 SUPPORTS_TYPED_TESTS = (
616 in gtest_test_utils.Subprocess(
617 [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=False
622 class GTestJsonOutputUnitTest(gtest_test_utils.TestCase):
623 """Unit test for Google Test's JSON output functionality."""
625 # This test currently breaks on platforms that do not support typed and
626 # type-parameterized tests, so we don't run it under them.
627 if SUPPORTS_TYPED_TESTS:
629 def testNonEmptyJsonOutput(self):
630 """Verifies JSON output for a Google Test binary with non-empty output.
632 Runs a test program that generates a non-empty JSON output, and
633 tests that the JSON output is expected.
635 self._TestJsonOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY, 1)
637 def testNoTestJsonOutput(self):
638 """Verifies JSON output for a Google Test binary without actual tests.
640 Runs a test program that generates an JSON output for a binary with no
641 tests, and tests that the JSON output is expected.
644 self._TestJsonOutput('gtest_no_test_unittest', EXPECTED_NO_TEST, 0)
646 def testTimestampValue(self):
647 """Checks whether the timestamp attribute in the JSON output is valid.
649 Runs a test program that generates an empty JSON output, and checks if
650 the timestamp attribute in the testsuites tag is valid.
652 actual = self._GetJsonOutput('gtest_no_test_unittest', [], 0)
653 date_time_str = actual['timestamp']
654 # datetime.strptime() is only available in Python 2.5+ so we have to
655 # parse the expected datetime manually.
656 match = re.match(r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
659 'JSON datettime string %s has incorrect format' % date_time_str,
661 date_time_from_json = datetime.datetime(
662 year=int(match.group(1)),
663 month=int(match.group(2)),
664 day=int(match.group(3)),
665 hour=int(match.group(4)),
666 minute=int(match.group(5)),
667 second=int(match.group(6)),
670 time_delta = abs(datetime.datetime.now() - date_time_from_json)
671 # timestamp value should be near the current local time
673 time_delta < datetime.timedelta(seconds=600),
674 'time_delta is %s' % time_delta,
677 def testDefaultOutputFile(self):
678 """Verifies the default output file name.
680 Confirms that Google Test produces an JSON output file with the expected
681 default name if no name is explicitly specified.
683 output_file = os.path.join(
684 gtest_test_utils.GetTempDir(), GTEST_DEFAULT_OUTPUT_FILE
686 gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
687 'gtest_no_test_unittest'
690 os.remove(output_file)
692 e = sys.exc_info()[1]
693 if e.errno != errno.ENOENT:
696 p = gtest_test_utils.Subprocess(
697 [gtest_prog_path, '%s=json' % GTEST_OUTPUT_FLAG],
698 working_dir=gtest_test_utils.GetTempDir(),
700 self.assertTrue(p.exited)
701 self.assertEqual(0, p.exit_code)
702 self.assertTrue(os.path.isfile(output_file))
704 def testSuppressedJsonOutput(self):
705 """Verifies that no JSON output is generated.
707 Tests that no JSON file is generated if the default JSON listener is
708 shut down before RUN_ALL_TESTS is invoked.
711 json_path = os.path.join(
712 gtest_test_utils.GetTempDir(), GTEST_PROGRAM_NAME + 'out.json'
714 if os.path.isfile(json_path):
719 '%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path),
722 p = gtest_test_utils.Subprocess(command)
723 if p.terminated_by_signal:
724 # p.signal is available only if p.terminated_by_signal is True.
726 p.terminated_by_signal,
727 '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal),
730 self.assertTrue(p.exited)
734 "'%s' exited with code %s, which doesn't match "
735 'the expected exit code %s.' % (command, p.exit_code, 1),
738 self.assertTrue(not os.path.isfile(json_path))
740 def testFilteredTestJsonOutput(self):
741 """Verifies JSON output when a filter is applied.
743 Runs a test program that executes only some tests and verifies that
744 non-selected tests do not show up in the JSON output.
747 self._TestJsonOutput(
751 extra_args=['%s=SuccessfulTest.*' % GTEST_FILTER_FLAG],
754 def _GetJsonOutput(self, gtest_prog_name, extra_args, expected_exit_code):
755 """Returns the JSON output generated by running the program gtest_prog_name.
757 Furthermore, the program's exit code must be expected_exit_code.
760 gtest_prog_name: Google Test binary name.
761 extra_args: extra arguments to binary invocation.
762 expected_exit_code: program's exit code.
764 json_path = os.path.join(
765 gtest_test_utils.GetTempDir(), gtest_prog_name + 'out.json'
767 gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
771 '%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path),
773 p = gtest_test_utils.Subprocess(command)
774 if p.terminated_by_signal:
776 False, '%s was killed by signal %d' % (gtest_prog_name, p.signal)
779 self.assertTrue(p.exited)
783 "'%s' exited with code %s, which doesn't match "
784 'the expected exit code %s.'
785 % (command, p.exit_code, expected_exit_code),
787 with open(json_path) as f:
788 actual = json.load(f)
792 self, gtest_prog_name, expected, expected_exit_code, extra_args=None
794 """Checks the JSON output generated by the Google Test binary.
796 Asserts that the JSON document generated by running the program
797 gtest_prog_name matches expected_json, a string containing another
798 JSON document. Furthermore, the program's exit code must be
802 gtest_prog_name: Google Test binary name.
803 expected: expected output.
804 expected_exit_code: program's exit code.
805 extra_args: extra arguments to binary invocation.
808 actual = self._GetJsonOutput(
809 gtest_prog_name, extra_args or [], expected_exit_code
811 self.assertEqual(expected, gtest_json_test_utils.normalize(actual))
814 if __name__ == '__main__':
815 if NO_STACKTRACE_SUPPORT_FLAG in sys.argv:
816 # unittest.main() can't handle unknown flags
817 sys.argv.remove(NO_STACKTRACE_SUPPORT_FLAG)
819 os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
820 gtest_test_utils.Main()