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 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_'
49 SUPPORTS_STACK_TRACES
= False
51 if SUPPORTS_STACK_TRACES
:
52 STACK_TRACE_TEMPLATE
= '\nStack trace:\n*'
54 STACK_TRACE_TEMPLATE
= ''
56 EXPECTED_NON_EMPTY
= {
63 u
'ad_hoc_property': u
'42',
67 u
'name': u
'SuccessfulTest',
78 u
'classname': u
'SuccessfulTest'
83 u
'name': u
'FailedTest',
94 u
'classname': u
'FailedTest',
98 u
'gtest_xml_output_unittest_.cc:*\n'
99 u
'Expected equality of these values:\n'
100 u
' 1\n 2' + STACK_TRACE_TEMPLATE
,
108 u
'name': u
'DisabledTest',
116 u
'name': u
'DISABLED_test_not_run',
117 u
'status': u
'NOTRUN',
119 u
'classname': u
'DisabledTest'
124 u
'name': u
'MixedResultTest',
132 u
'name': u
'Succeeds',
135 u
'classname': u
'MixedResultTest'
141 u
'classname': u
'MixedResultTest',
145 u
'gtest_xml_output_unittest_.cc:*\n'
146 u
'Expected equality of these values:\n'
147 u
' 1\n 2' + STACK_TRACE_TEMPLATE
,
152 u
'gtest_xml_output_unittest_.cc:*\n'
153 u
'Expected equality of these values:\n'
154 u
' 2\n 3' + STACK_TRACE_TEMPLATE
,
160 u
'name': u
'DISABLED_test',
161 u
'status': u
'NOTRUN',
163 u
'classname': u
'MixedResultTest'
168 u
'name': u
'XmlQuotingTest',
176 u
'name': u
'OutputsCData',
179 u
'classname': u
'XmlQuotingTest',
183 u
'gtest_xml_output_unittest_.cc:*\n'
184 u
'Failed\nXML output: <?xml encoding="utf-8">'
185 u
'<top><![CDATA[cdata text]]></top>' +
186 STACK_TRACE_TEMPLATE
,
194 u
'name': u
'InvalidCharactersTest',
202 u
'name': u
'InvalidCharactersInMessage',
205 u
'classname': u
'InvalidCharactersTest',
209 u
'gtest_xml_output_unittest_.cc:*\n'
210 u
'Failed\nInvalid characters in brackets'
211 u
' [\x01\x02]' + STACK_TRACE_TEMPLATE
,
219 u
'name': u
'PropertyRecordingTest',
225 u
'SetUpTestCase': u
'yes',
226 u
'TearDownTestCase': u
'aye',
229 u
'name': u
'OneProperty',
232 u
'classname': u
'PropertyRecordingTest',
236 u
'name': u
'IntValuedProperty',
239 u
'classname': u
'PropertyRecordingTest',
243 u
'name': u
'ThreeProperties',
246 u
'classname': u
'PropertyRecordingTest',
252 u
'name': u
'TwoValuesForOneKeyUsesLastValue',
255 u
'classname': u
'PropertyRecordingTest',
261 u
'name': u
'NoFixtureTest',
269 u
'name': u
'RecordProperty',
272 u
'classname': u
'NoFixtureTest',
276 u
'name': u
'ExternalUtilityThatCallsRecordIntValuedProperty',
279 u
'classname': u
'NoFixtureTest',
280 u
'key_for_utility_int': u
'1'
284 u
'ExternalUtilityThatCallsRecordStringValuedProperty',
287 u
'classname': u
'NoFixtureTest',
288 u
'key_for_utility_string': u
'1'
293 u
'name': u
'TypedTest/0',
301 u
'name': u
'HasTypeParamAttribute',
302 u
'type_param': u
'int',
305 u
'classname': u
'TypedTest/0'
310 u
'name': u
'TypedTest/1',
318 u
'name': u
'HasTypeParamAttribute',
319 u
'type_param': u
'long',
322 u
'classname': u
'TypedTest/1'
327 u
'name': u
'Single/TypeParameterizedTestCase/0',
335 u
'name': u
'HasTypeParamAttribute',
336 u
'type_param': u
'int',
339 u
'classname': u
'Single/TypeParameterizedTestCase/0'
344 u
'name': u
'Single/TypeParameterizedTestCase/1',
352 u
'name': u
'HasTypeParamAttribute',
353 u
'type_param': u
'long',
356 u
'classname': u
'Single/TypeParameterizedTestCase/1'
361 u
'name': u
'Single/ValueParamTest',
369 u
'name': u
'HasValueParamAttribute/0',
370 u
'value_param': u
'33',
373 u
'classname': u
'Single/ValueParamTest'
376 u
'name': u
'HasValueParamAttribute/1',
377 u
'value_param': u
'42',
380 u
'classname': u
'Single/ValueParamTest'
383 u
'name': u
'AnotherTestThatHasValueParamAttribute/0',
384 u
'value_param': u
'33',
387 u
'classname': u
'Single/ValueParamTest'
390 u
'name': u
'AnotherTestThatHasValueParamAttribute/1',
391 u
'value_param': u
'42',
394 u
'classname': u
'Single/ValueParamTest'
401 EXPECTED_FILTERED
= {
408 u
'name': u
'AllTests',
409 u
'ad_hoc_property': u
'42',
411 u
'name': u
'SuccessfulTest',
418 u
'name': u
'Succeeds',
421 u
'classname': u
'SuccessfulTest',
433 u
'name': u
'AllTests',
437 GTEST_PROGRAM_PATH
= gtest_test_utils
.GetTestExecutablePath(GTEST_PROGRAM_NAME
)
439 SUPPORTS_TYPED_TESTS
= 'TypedTest' in gtest_test_utils
.Subprocess(
440 [GTEST_PROGRAM_PATH
, GTEST_LIST_TESTS_FLAG
], capture_stderr
=False).output
443 class GTestJsonOutputUnitTest(gtest_test_utils
.TestCase
):
444 """Unit test for Google Test's JSON output functionality.
447 # This test currently breaks on platforms that do not support typed and
448 # type-parameterized tests, so we don't run it under them.
449 if SUPPORTS_TYPED_TESTS
:
451 def testNonEmptyJsonOutput(self
):
452 """Verifies JSON output for a Google Test binary with non-empty output.
454 Runs a test program that generates a non-empty JSON output, and
455 tests that the JSON output is expected.
457 self
._TestJsonOutput
(GTEST_PROGRAM_NAME
, EXPECTED_NON_EMPTY
, 1)
459 def testEmptyJsonOutput(self
):
460 """Verifies JSON output for a Google Test binary without actual tests.
462 Runs a test program that generates an empty JSON output, and
463 tests that the JSON output is expected.
466 self
._TestJsonOutput
('gtest_no_test_unittest', EXPECTED_EMPTY
, 0)
468 def testTimestampValue(self
):
469 """Checks whether the timestamp attribute in the JSON output is valid.
471 Runs a test program that generates an empty JSON output, and checks if
472 the timestamp attribute in the testsuites tag is valid.
474 actual
= self
._GetJsonOutput
('gtest_no_test_unittest', [], 0)
475 date_time_str
= actual
['timestamp']
476 # datetime.strptime() is only available in Python 2.5+ so we have to
477 # parse the expected datetime manually.
478 match
= re
.match(r
'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str
)
481 'JSON datettime string %s has incorrect format' % date_time_str
)
482 date_time_from_json
= datetime
.datetime(
483 year
=int(match
.group(1)), month
=int(match
.group(2)),
484 day
=int(match
.group(3)), hour
=int(match
.group(4)),
485 minute
=int(match
.group(5)), second
=int(match
.group(6)))
487 time_delta
= abs(datetime
.datetime
.now() - date_time_from_json
)
488 # timestamp value should be near the current local time
489 self
.assertTrue(time_delta
< datetime
.timedelta(seconds
=600),
490 'time_delta is %s' % time_delta
)
492 def testDefaultOutputFile(self
):
493 """Verifies the default output file name.
495 Confirms that Google Test produces an JSON output file with the expected
496 default name if no name is explicitly specified.
498 output_file
= os
.path
.join(gtest_test_utils
.GetTempDir(),
499 GTEST_DEFAULT_OUTPUT_FILE
)
500 gtest_prog_path
= gtest_test_utils
.GetTestExecutablePath(
501 'gtest_no_test_unittest')
503 os
.remove(output_file
)
505 e
= sys
.exc_info()[1]
506 if e
.errno
!= errno
.ENOENT
:
509 p
= gtest_test_utils
.Subprocess(
510 [gtest_prog_path
, '%s=json' % GTEST_OUTPUT_FLAG
],
511 working_dir
=gtest_test_utils
.GetTempDir())
512 self
.assert_(p
.exited
)
513 self
.assertEquals(0, p
.exit_code
)
514 self
.assert_(os
.path
.isfile(output_file
))
516 def testSuppressedJsonOutput(self
):
517 """Verifies that no JSON output is generated.
519 Tests that no JSON file is generated if the default JSON listener is
520 shut down before RUN_ALL_TESTS is invoked.
523 json_path
= os
.path
.join(gtest_test_utils
.GetTempDir(),
524 GTEST_PROGRAM_NAME
+ 'out.json')
525 if os
.path
.isfile(json_path
):
528 command
= [GTEST_PROGRAM_PATH
,
529 '%s=json:%s' % (GTEST_OUTPUT_FLAG
, json_path
),
531 p
= gtest_test_utils
.Subprocess(command
)
532 if p
.terminated_by_signal
:
533 # p.signal is available only if p.terminated_by_signal is True.
535 p
.terminated_by_signal
,
536 '%s was killed by signal %d' % (GTEST_PROGRAM_NAME
, p
.signal
))
538 self
.assert_(p
.exited
)
539 self
.assertEquals(1, p
.exit_code
,
540 "'%s' exited with code %s, which doesn't match "
541 'the expected exit code %s.'
542 % (command
, p
.exit_code
, 1))
544 self
.assert_(not os
.path
.isfile(json_path
))
546 def testFilteredTestJsonOutput(self
):
547 """Verifies JSON output when a filter is applied.
549 Runs a test program that executes only some tests and verifies that
550 non-selected tests do not show up in the JSON output.
553 self
._TestJsonOutput
(GTEST_PROGRAM_NAME
, EXPECTED_FILTERED
, 0,
554 extra_args
=['%s=SuccessfulTest.*' % GTEST_FILTER_FLAG
])
556 def _GetJsonOutput(self
, gtest_prog_name
, extra_args
, expected_exit_code
):
557 """Returns the JSON output generated by running the program gtest_prog_name.
559 Furthermore, the program's exit code must be expected_exit_code.
562 gtest_prog_name: Google Test binary name.
563 extra_args: extra arguments to binary invocation.
564 expected_exit_code: program's exit code.
566 json_path
= os
.path
.join(gtest_test_utils
.GetTempDir(),
567 gtest_prog_name
+ 'out.json')
568 gtest_prog_path
= gtest_test_utils
.GetTestExecutablePath(gtest_prog_name
)
571 [gtest_prog_path
, '%s=json:%s' % (GTEST_OUTPUT_FLAG
, json_path
)] +
574 p
= gtest_test_utils
.Subprocess(command
)
575 if p
.terminated_by_signal
:
577 '%s was killed by signal %d' % (gtest_prog_name
, p
.signal
))
579 self
.assert_(p
.exited
)
580 self
.assertEquals(expected_exit_code
, p
.exit_code
,
581 "'%s' exited with code %s, which doesn't match "
582 'the expected exit code %s.'
583 % (command
, p
.exit_code
, expected_exit_code
))
584 with
open(json_path
) as f
:
585 actual
= json
.load(f
)
588 def _TestJsonOutput(self
, gtest_prog_name
, expected
,
589 expected_exit_code
, extra_args
=None):
590 """Checks the JSON output generated by the Google Test binary.
592 Asserts that the JSON document generated by running the program
593 gtest_prog_name matches expected_json, a string containing another
594 JSON document. Furthermore, the program's exit code must be
598 gtest_prog_name: Google Test binary name.
599 expected: expected output.
600 expected_exit_code: program's exit code.
601 extra_args: extra arguments to binary invocation.
604 actual
= self
._GetJsonOutput
(gtest_prog_name
, extra_args
or [],
606 self
.assertEqual(expected
, gtest_json_test_utils
.normalize(actual
))
609 if __name__
== '__main__':
610 os
.environ
['GTEST_STACK_TRACE_DEPTH'] = '1'
611 gtest_test_utils
.Main()