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_'
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',
629 [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=
False).output
633 """Unit test for Google Test's JSON output functionality.
638 if SUPPORTS_TYPED_TESTS:
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.
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.
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.
664 date_time_str = actual[
'timestamp']
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)
678 self.assertTrue(time_delta < datetime.timedelta(seconds=600),
679 'time_delta is %s' % time_delta)
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.
688 GTEST_DEFAULT_OUTPUT_FILE)
690 'gtest_no_test_unittest')
692 os.remove(output_file)
694 e = sys.exc_info()[1]
695 if e.errno != errno.ENOENT:
699 [gtest_prog_path,
'%s=json' % GTEST_OUTPUT_FLAG],
701 self.assert_(p.exited)
702 self.assertEquals(0, p.exit_code)
703 self.assert_(os.path.isfile(output_file))
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.
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),
721 if p.terminated_by_signal:
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))
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.
743 extra_args=[
'%s=SuccessfulTest.*' % GTEST_FILTER_FLAG])
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.
756 gtest_prog_name +
'out.json')
760 [gtest_prog_path,
'%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path)] +
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)
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.
798 if __name__ ==
'__main__':
799 if NO_STACKTRACE_SUPPORT_FLAG
in sys.argv:
801 sys.argv.remove(NO_STACKTRACE_SUPPORT_FLAG)
803 os.environ[
'GTEST_STACK_TRACE_DEPTH'] =
'1'