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'
196 u'name':
u'Succeeds',
198 u'result':
u'COMPLETED',
201 u'classname':
u'MixedResultTest'
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,
221 u'failure':
u'gtest_xml_output_unittest_.cc:*\n'
222 u'Expected equality of these values:\n'
223 u' 2\n 3' + STACK_TRACE_TEMPLATE,
227 u'name':
u'DISABLED_test',
228 u'status':
u'NOTRUN',
229 u'result':
u'SUPPRESSED',
232 u'classname':
u'MixedResultTest'
263 u'failure':
u'gtest_xml_output_unittest_.cc:*\n'
264 u'Failed\nXML output: <?xml encoding="utf-8">'
265 u'<top><![CDATA[cdata text]]></top>' +
266 STACK_TRACE_TEMPLATE,
272 u'InvalidCharactersTest',
287 u'InvalidCharactersInMessage',
297 u'InvalidCharactersTest',
299 u'failure':
u'gtest_xml_output_unittest_.cc:*\n'
300 u'Failed\nInvalid characters in brackets'
301 u' [\x01\x02]' + STACK_TRACE_TEMPLATE,
307 u'PropertyRecordingTest',
322 u'TearDownTestSuite':
325 u'name':
u'OneProperty',
327 u'result':
u'COMPLETED',
330 u'classname':
u'PropertyRecordingTest',
333 u'name':
u'IntValuedProperty',
335 u'result':
u'COMPLETED',
338 u'classname':
u'PropertyRecordingTest',
341 u'name':
u'ThreeProperties',
343 u'result':
u'COMPLETED',
346 u'classname':
u'PropertyRecordingTest',
351 u'name':
u'TwoValuesForOneKeyUsesLastValue',
353 u'result':
u'COMPLETED',
356 u'classname':
u'PropertyRecordingTest',
375 u'name':
u'RecordProperty',
377 u'result':
u'COMPLETED',
380 u'classname':
u'NoFixtureTest',
383 u'name':
u'ExternalUtilityThatCallsRecordIntValuedProperty',
385 u'result':
u'COMPLETED',
388 u'classname':
u'NoFixtureTest',
389 u'key_for_utility_int':
u'1'
391 u'name':
u'ExternalUtilityThatCallsRecordStringValuedProperty',
393 u'result':
u'COMPLETED',
396 u'classname':
u'NoFixtureTest',
397 u'key_for_utility_string':
u'1'
415 u'name':
u'HasTypeParamAttribute',
416 u'type_param':
u'int',
418 u'result':
u'COMPLETED',
421 u'classname':
u'TypedTest/0'
439 u'name':
u'HasTypeParamAttribute',
440 u'type_param':
u'long',
442 u'result':
u'COMPLETED',
445 u'classname':
u'TypedTest/1'
449 u'Single/TypeParameterizedTestSuite/0',
463 u'name':
u'HasTypeParamAttribute',
464 u'type_param':
u'int',
466 u'result':
u'COMPLETED',
469 u'classname':
u'Single/TypeParameterizedTestSuite/0'
473 u'Single/TypeParameterizedTestSuite/1',
487 u'name':
u'HasTypeParamAttribute',
488 u'type_param':
u'long',
490 u'result':
u'COMPLETED',
493 u'classname':
u'Single/TypeParameterizedTestSuite/1'
497 u'Single/ValueParamTest',
511 u'name':
u'HasValueParamAttribute/0',
512 u'value_param':
u'33',
514 u'result':
u'COMPLETED',
517 u'classname':
u'Single/ValueParamTest'
519 u'name':
u'HasValueParamAttribute/1',
520 u'value_param':
u'42',
522 u'result':
u'COMPLETED',
525 u'classname':
u'Single/ValueParamTest'
527 u'name':
u'AnotherTestThatHasValueParamAttribute/0',
528 u'value_param':
u'33',
530 u'result':
u'COMPLETED',
533 u'classname':
u'Single/ValueParamTest'
535 u'name':
u'AnotherTestThatHasValueParamAttribute/1',
536 u'value_param':
u'42',
538 u'result':
u'COMPLETED',
541 u'classname':
u'Single/ValueParamTest'
546 EXPECTED_FILTERED = {
579 u'name':
u'Succeeds',
581 u'result':
u'COMPLETED',
584 u'classname':
u'SuccessfulTest',
596 u'name':
u'AllTests',
603 [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=
False).output
607 """Unit test for Google Test's JSON output functionality.
612 if SUPPORTS_TYPED_TESTS:
615 """Verifies JSON output for a Google Test binary with non-empty output.
617 Runs a test program that generates a non-empty JSON output, and
618 tests that the JSON output is expected.
623 """Verifies JSON output for a Google Test binary without actual tests.
625 Runs a test program that generates an empty JSON output, and
626 tests that the JSON output is expected.
632 """Checks whether the timestamp attribute in the JSON output is valid.
634 Runs a test program that generates an empty JSON output, and checks if
635 the timestamp attribute in the testsuites tag is valid.
638 date_time_str = actual[
'timestamp']
641 match = re.match(
r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
644 'JSON datettime string %s has incorrect format' % date_time_str)
645 date_time_from_json = datetime.datetime(
646 year=
int(match.group(1)), month=
int(match.group(2)),
647 day=
int(match.group(3)), hour=
int(match.group(4)),
648 minute=
int(match.group(5)), second=
int(match.group(6)))
650 time_delta = abs(datetime.datetime.now() - date_time_from_json)
652 self.assertTrue(time_delta < datetime.timedelta(seconds=600),
653 'time_delta is %s' % time_delta)
656 """Verifies the default output file name.
658 Confirms that Google Test produces an JSON output file with the expected
659 default name if no name is explicitly specified.
662 GTEST_DEFAULT_OUTPUT_FILE)
664 'gtest_no_test_unittest')
666 os.remove(output_file)
668 e = sys.exc_info()[1]
669 if e.errno != errno.ENOENT:
673 [gtest_prog_path,
'%s=json' % GTEST_OUTPUT_FLAG],
675 self.assert_(p.exited)
676 self.assertEquals(0, p.exit_code)
677 self.assert_(os.path.isfile(output_file))
680 """Verifies that no JSON output is generated.
682 Tests that no JSON file is generated if the default JSON listener is
683 shut down before RUN_ALL_TESTS is invoked.
687 GTEST_PROGRAM_NAME +
'out.json')
688 if os.path.isfile(json_path):
691 command = [GTEST_PROGRAM_PATH,
692 '%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path),
695 if p.terminated_by_signal:
698 p.terminated_by_signal,
699 '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal))
701 self.assert_(p.exited)
702 self.assertEquals(1, p.exit_code,
703 "'%s' exited with code %s, which doesn't match "
704 'the expected exit code %s.'
705 % (command, p.exit_code, 1))
707 self.assert_(
not os.path.isfile(json_path))
710 """Verifies JSON output when a filter is applied.
712 Runs a test program that executes only some tests and verifies that
713 non-selected tests do not show up in the JSON output.
717 extra_args=[
'%s=SuccessfulTest.*' % GTEST_FILTER_FLAG])
720 """Returns the JSON output generated by running the program gtest_prog_name.
722 Furthermore, the program's exit code must be expected_exit_code.
725 gtest_prog_name: Google Test binary name.
726 extra_args: extra arguments to binary invocation.
727 expected_exit_code: program's exit code.
730 gtest_prog_name +
'out.json')
734 [gtest_prog_path,
'%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path)] +
738 if p.terminated_by_signal:
740 '%s was killed by signal %d' % (gtest_prog_name, p.signal))
742 self.assert_(p.exited)
743 self.assertEquals(expected_exit_code, p.exit_code,
744 "'%s' exited with code %s, which doesn't match "
745 'the expected exit code %s.'
746 % (command, p.exit_code, expected_exit_code))
747 with open(json_path)
as f:
748 actual = json.load(f)
752 expected_exit_code, extra_args=None):
753 """Checks the JSON output generated by the Google Test binary.
755 Asserts that the JSON document generated by running the program
756 gtest_prog_name matches expected_json, a string containing another
757 JSON document. Furthermore, the program's exit code must be
761 gtest_prog_name: Google Test binary name.
762 expected: expected output.
763 expected_exit_code: program's exit code.
764 extra_args: extra arguments to binary invocation.
772 if __name__ ==
'__main__':
773 if NO_STACKTRACE_SUPPORT_FLAG
in sys.argv:
775 sys.argv.remove(NO_STACKTRACE_SUPPORT_FLAG)
777 os.environ[
'GTEST_STACK_TRACE_DEPTH'] =
'1'