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 = {
66 u'ad_hoc_property':
u'42',
70 u'name':
u'SuccessfulTest',
81 u'classname':
u'SuccessfulTest'
86 u'name':
u'FailedTest',
97 u'classname':
u'FailedTest',
101 u'gtest_xml_output_unittest_.cc:*\n'
102 u'Expected equality of these values:\n'
103 u' 1\n 2' + STACK_TRACE_TEMPLATE,
111 u'name':
u'DisabledTest',
119 u'name':
u'DISABLED_test_not_run',
120 u'status':
u'NOTRUN',
122 u'classname':
u'DisabledTest'
127 u'name':
u'SkippedTest',
136 u'status':
u'SKIPPED',
138 u'classname':
u'SkippedTest'
143 u'name':
u'MixedResultTest',
151 u'name':
u'Succeeds',
154 u'classname':
u'MixedResultTest'
160 u'classname':
u'MixedResultTest',
164 u'gtest_xml_output_unittest_.cc:*\n'
165 u'Expected equality of these values:\n'
166 u' 1\n 2' + STACK_TRACE_TEMPLATE,
171 u'gtest_xml_output_unittest_.cc:*\n'
172 u'Expected equality of these values:\n'
173 u' 2\n 3' + STACK_TRACE_TEMPLATE,
179 u'name':
u'DISABLED_test',
180 u'status':
u'NOTRUN',
182 u'classname':
u'MixedResultTest'
187 u'name':
u'XmlQuotingTest',
195 u'name':
u'OutputsCData',
198 u'classname':
u'XmlQuotingTest',
202 u'gtest_xml_output_unittest_.cc:*\n'
203 u'Failed\nXML output: <?xml encoding="utf-8">'
204 u'<top><![CDATA[cdata text]]></top>' +
205 STACK_TRACE_TEMPLATE,
213 u'name':
u'InvalidCharactersTest',
221 u'name':
u'InvalidCharactersInMessage',
224 u'classname':
u'InvalidCharactersTest',
228 u'gtest_xml_output_unittest_.cc:*\n'
229 u'Failed\nInvalid characters in brackets'
230 u' [\x01\x02]' + STACK_TRACE_TEMPLATE,
238 u'name':
u'PropertyRecordingTest',
244 u'SetUpTestSuite':
u'yes',
245 u'TearDownTestSuite':
u'aye',
248 u'name':
u'OneProperty',
251 u'classname':
u'PropertyRecordingTest',
255 u'name':
u'IntValuedProperty',
258 u'classname':
u'PropertyRecordingTest',
262 u'name':
u'ThreeProperties',
265 u'classname':
u'PropertyRecordingTest',
271 u'name':
u'TwoValuesForOneKeyUsesLastValue',
274 u'classname':
u'PropertyRecordingTest',
280 u'name':
u'NoFixtureTest',
288 u'name':
u'RecordProperty',
291 u'classname':
u'NoFixtureTest',
295 u'name':
u'ExternalUtilityThatCallsRecordIntValuedProperty',
298 u'classname':
u'NoFixtureTest',
299 u'key_for_utility_int':
u'1'
303 u'ExternalUtilityThatCallsRecordStringValuedProperty',
306 u'classname':
u'NoFixtureTest',
307 u'key_for_utility_string':
u'1'
312 u'name':
u'TypedTest/0',
320 u'name':
u'HasTypeParamAttribute',
321 u'type_param':
u'int',
324 u'classname':
u'TypedTest/0'
329 u'name':
u'TypedTest/1',
337 u'name':
u'HasTypeParamAttribute',
338 u'type_param':
u'long',
341 u'classname':
u'TypedTest/1'
346 u'name':
u'Single/TypeParameterizedTestSuite/0',
354 u'name':
u'HasTypeParamAttribute',
355 u'type_param':
u'int',
358 u'classname':
u'Single/TypeParameterizedTestSuite/0'
363 u'name':
u'Single/TypeParameterizedTestSuite/1',
371 u'name':
u'HasTypeParamAttribute',
372 u'type_param':
u'long',
375 u'classname':
u'Single/TypeParameterizedTestSuite/1'
380 u'name':
u'Single/ValueParamTest',
388 u'name':
u'HasValueParamAttribute/0',
389 u'value_param':
u'33',
392 u'classname':
u'Single/ValueParamTest'
395 u'name':
u'HasValueParamAttribute/1',
396 u'value_param':
u'42',
399 u'classname':
u'Single/ValueParamTest'
402 u'name':
u'AnotherTestThatHasValueParamAttribute/0',
403 u'value_param':
u'33',
406 u'classname':
u'Single/ValueParamTest'
409 u'name':
u'AnotherTestThatHasValueParamAttribute/1',
410 u'value_param':
u'42',
413 u'classname':
u'Single/ValueParamTest'
420 EXPECTED_FILTERED = {
427 u'name':
u'AllTests',
428 u'ad_hoc_property':
u'42',
430 u'name':
u'SuccessfulTest',
437 u'name':
u'Succeeds',
440 u'classname':
u'SuccessfulTest',
452 u'name':
u'AllTests',
459 [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=
False).output
463 """Unit test for Google Test's JSON output functionality.
468 if SUPPORTS_TYPED_TESTS:
471 """Verifies JSON output for a Google Test binary with non-empty output.
473 Runs a test program that generates a non-empty JSON output, and
474 tests that the JSON output is expected.
479 """Verifies JSON output for a Google Test binary without actual tests.
481 Runs a test program that generates an empty JSON output, and
482 tests that the JSON output is expected.
488 """Checks whether the timestamp attribute in the JSON output is valid.
490 Runs a test program that generates an empty JSON output, and checks if
491 the timestamp attribute in the testsuites tag is valid.
494 date_time_str = actual[
'timestamp']
497 match = re.match(
r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
500 'JSON datettime string %s has incorrect format' % date_time_str)
501 date_time_from_json = datetime.datetime(
502 year=
int(match.group(1)), month=
int(match.group(2)),
503 day=
int(match.group(3)), hour=
int(match.group(4)),
504 minute=
int(match.group(5)), second=
int(match.group(6)))
506 time_delta = abs(datetime.datetime.now() - date_time_from_json)
508 self.assertTrue(time_delta < datetime.timedelta(seconds=600),
509 'time_delta is %s' % time_delta)
512 """Verifies the default output file name.
514 Confirms that Google Test produces an JSON output file with the expected
515 default name if no name is explicitly specified.
518 GTEST_DEFAULT_OUTPUT_FILE)
520 'gtest_no_test_unittest')
522 os.remove(output_file)
524 e = sys.exc_info()[1]
525 if e.errno != errno.ENOENT:
529 [gtest_prog_path,
'%s=json' % GTEST_OUTPUT_FLAG],
531 self.assert_(p.exited)
532 self.assertEquals(0, p.exit_code)
533 self.assert_(os.path.isfile(output_file))
536 """Verifies that no JSON output is generated.
538 Tests that no JSON file is generated if the default JSON listener is
539 shut down before RUN_ALL_TESTS is invoked.
543 GTEST_PROGRAM_NAME +
'out.json')
544 if os.path.isfile(json_path):
547 command = [GTEST_PROGRAM_PATH,
548 '%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path),
551 if p.terminated_by_signal:
554 p.terminated_by_signal,
555 '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal))
557 self.assert_(p.exited)
558 self.assertEquals(1, p.exit_code,
559 "'%s' exited with code %s, which doesn't match "
560 'the expected exit code %s.'
561 % (command, p.exit_code, 1))
563 self.assert_(
not os.path.isfile(json_path))
566 """Verifies JSON output when a filter is applied.
568 Runs a test program that executes only some tests and verifies that
569 non-selected tests do not show up in the JSON output.
573 extra_args=[
'%s=SuccessfulTest.*' % GTEST_FILTER_FLAG])
576 """Returns the JSON output generated by running the program gtest_prog_name.
578 Furthermore, the program's exit code must be expected_exit_code.
581 gtest_prog_name: Google Test binary name.
582 extra_args: extra arguments to binary invocation.
583 expected_exit_code: program's exit code.
586 gtest_prog_name +
'out.json')
590 [gtest_prog_path,
'%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path)] +
594 if p.terminated_by_signal:
596 '%s was killed by signal %d' % (gtest_prog_name, p.signal))
598 self.assert_(p.exited)
599 self.assertEquals(expected_exit_code, p.exit_code,
600 "'%s' exited with code %s, which doesn't match "
601 'the expected exit code %s.'
602 % (command, p.exit_code, expected_exit_code))
603 with open(json_path)
as f:
604 actual = json.load(f)
608 expected_exit_code, extra_args=None):
609 """Checks the JSON output generated by the Google Test binary.
611 Asserts that the JSON document generated by running the program
612 gtest_prog_name matches expected_json, a string containing another
613 JSON document. Furthermore, the program's exit code must be
617 gtest_prog_name: Google Test binary name.
618 expected: expected output.
619 expected_exit_code: program's exit code.
620 extra_args: extra arguments to binary invocation.
628 if __name__ ==
'__main__':
629 if NO_STACKTRACE_SUPPORT_FLAG
in sys.argv:
631 sys.argv.remove(NO_STACKTRACE_SUPPORT_FLAG)
633 os.environ[
'GTEST_STACK_TRACE_DEPTH'] =
'1'