protobuf/third_party/googletest/googletest/test/googletest-json-output-unittest.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright 2018, Google Inc.
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
8 #
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
14 # distribution.
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.
18 #
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.
30 
31 """Unit test for the gtest_json_output module."""
32 
33 import datetime
34 import errno
35 import json
36 import os
37 import re
38 import sys
39 
40 import gtest_json_test_utils
41 import gtest_test_utils
42 
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_'
48 
49 # The flag indicating stacktraces are not supported
50 NO_STACKTRACE_SUPPORT_FLAG = '--no_stacktrace_support'
51 
52 SUPPORTS_STACK_TRACES = NO_STACKTRACE_SUPPORT_FLAG not in sys.argv
53 
54 if SUPPORTS_STACK_TRACES:
55  STACK_TRACE_TEMPLATE = '\nStack trace:\n*'
56 else:
57  STACK_TRACE_TEMPLATE = ''
58 
59 EXPECTED_NON_EMPTY = {
60  u'tests': 24,
61  u'failures': 4,
62  u'disabled': 2,
63  u'errors': 0,
64  u'timestamp': u'*',
65  u'time': u'*',
66  u'ad_hoc_property': u'42',
67  u'name': u'AllTests',
68  u'testsuites': [
69  {
70  u'name': u'SuccessfulTest',
71  u'tests': 1,
72  u'failures': 0,
73  u'disabled': 0,
74  u'errors': 0,
75  u'time': u'*',
76  u'testsuite': [
77  {
78  u'name': u'Succeeds',
79  u'status': u'RUN',
80  u'time': u'*',
81  u'classname': u'SuccessfulTest'
82  }
83  ]
84  },
85  {
86  u'name': u'FailedTest',
87  u'tests': 1,
88  u'failures': 1,
89  u'disabled': 0,
90  u'errors': 0,
91  u'time': u'*',
92  u'testsuite': [
93  {
94  u'name': u'Fails',
95  u'status': u'RUN',
96  u'time': u'*',
97  u'classname': u'FailedTest',
98  u'failures': [
99  {
100  u'failure':
101  u'gtest_xml_output_unittest_.cc:*\n'
102  u'Expected equality of these values:\n'
103  u' 1\n 2' + STACK_TRACE_TEMPLATE,
104  u'type': u''
105  }
106  ]
107  }
108  ]
109  },
110  {
111  u'name': u'DisabledTest',
112  u'tests': 1,
113  u'failures': 0,
114  u'disabled': 1,
115  u'errors': 0,
116  u'time': u'*',
117  u'testsuite': [
118  {
119  u'name': u'DISABLED_test_not_run',
120  u'status': u'NOTRUN',
121  u'time': u'*',
122  u'classname': u'DisabledTest'
123  }
124  ]
125  },
126  {
127  u'name': u'SkippedTest',
128  u'tests': 1,
129  u'failures': 0,
130  u'disabled': 0,
131  u'errors': 0,
132  u'time': u'*',
133  u'testsuite': [
134  {
135  u'name': u'Skipped',
136  u'status': u'SKIPPED',
137  u'time': u'*',
138  u'classname': u'SkippedTest'
139  }
140  ]
141  },
142  {
143  u'name': u'MixedResultTest',
144  u'tests': 3,
145  u'failures': 1,
146  u'disabled': 1,
147  u'errors': 0,
148  u'time': u'*',
149  u'testsuite': [
150  {
151  u'name': u'Succeeds',
152  u'status': u'RUN',
153  u'time': u'*',
154  u'classname': u'MixedResultTest'
155  },
156  {
157  u'name': u'Fails',
158  u'status': u'RUN',
159  u'time': u'*',
160  u'classname': u'MixedResultTest',
161  u'failures': [
162  {
163  u'failure':
164  u'gtest_xml_output_unittest_.cc:*\n'
165  u'Expected equality of these values:\n'
166  u' 1\n 2' + STACK_TRACE_TEMPLATE,
167  u'type': u''
168  },
169  {
170  u'failure':
171  u'gtest_xml_output_unittest_.cc:*\n'
172  u'Expected equality of these values:\n'
173  u' 2\n 3' + STACK_TRACE_TEMPLATE,
174  u'type': u''
175  }
176  ]
177  },
178  {
179  u'name': u'DISABLED_test',
180  u'status': u'NOTRUN',
181  u'time': u'*',
182  u'classname': u'MixedResultTest'
183  }
184  ]
185  },
186  {
187  u'name': u'XmlQuotingTest',
188  u'tests': 1,
189  u'failures': 1,
190  u'disabled': 0,
191  u'errors': 0,
192  u'time': u'*',
193  u'testsuite': [
194  {
195  u'name': u'OutputsCData',
196  u'status': u'RUN',
197  u'time': u'*',
198  u'classname': u'XmlQuotingTest',
199  u'failures': [
200  {
201  u'failure':
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,
206  u'type': u''
207  }
208  ]
209  }
210  ]
211  },
212  {
213  u'name': u'InvalidCharactersTest',
214  u'tests': 1,
215  u'failures': 1,
216  u'disabled': 0,
217  u'errors': 0,
218  u'time': u'*',
219  u'testsuite': [
220  {
221  u'name': u'InvalidCharactersInMessage',
222  u'status': u'RUN',
223  u'time': u'*',
224  u'classname': u'InvalidCharactersTest',
225  u'failures': [
226  {
227  u'failure':
228  u'gtest_xml_output_unittest_.cc:*\n'
229  u'Failed\nInvalid characters in brackets'
230  u' [\x01\x02]' + STACK_TRACE_TEMPLATE,
231  u'type': u''
232  }
233  ]
234  }
235  ]
236  },
237  {
238  u'name': u'PropertyRecordingTest',
239  u'tests': 4,
240  u'failures': 0,
241  u'disabled': 0,
242  u'errors': 0,
243  u'time': u'*',
244  u'SetUpTestSuite': u'yes',
245  u'TearDownTestSuite': u'aye',
246  u'testsuite': [
247  {
248  u'name': u'OneProperty',
249  u'status': u'RUN',
250  u'time': u'*',
251  u'classname': u'PropertyRecordingTest',
252  u'key_1': u'1'
253  },
254  {
255  u'name': u'IntValuedProperty',
256  u'status': u'RUN',
257  u'time': u'*',
258  u'classname': u'PropertyRecordingTest',
259  u'key_int': u'1'
260  },
261  {
262  u'name': u'ThreeProperties',
263  u'status': u'RUN',
264  u'time': u'*',
265  u'classname': u'PropertyRecordingTest',
266  u'key_1': u'1',
267  u'key_2': u'2',
268  u'key_3': u'3'
269  },
270  {
271  u'name': u'TwoValuesForOneKeyUsesLastValue',
272  u'status': u'RUN',
273  u'time': u'*',
274  u'classname': u'PropertyRecordingTest',
275  u'key_1': u'2'
276  }
277  ]
278  },
279  {
280  u'name': u'NoFixtureTest',
281  u'tests': 3,
282  u'failures': 0,
283  u'disabled': 0,
284  u'errors': 0,
285  u'time': u'*',
286  u'testsuite': [
287  {
288  u'name': u'RecordProperty',
289  u'status': u'RUN',
290  u'time': u'*',
291  u'classname': u'NoFixtureTest',
292  u'key': u'1'
293  },
294  {
295  u'name': u'ExternalUtilityThatCallsRecordIntValuedProperty',
296  u'status': u'RUN',
297  u'time': u'*',
298  u'classname': u'NoFixtureTest',
299  u'key_for_utility_int': u'1'
300  },
301  {
302  u'name':
303  u'ExternalUtilityThatCallsRecordStringValuedProperty',
304  u'status': u'RUN',
305  u'time': u'*',
306  u'classname': u'NoFixtureTest',
307  u'key_for_utility_string': u'1'
308  }
309  ]
310  },
311  {
312  u'name': u'TypedTest/0',
313  u'tests': 1,
314  u'failures': 0,
315  u'disabled': 0,
316  u'errors': 0,
317  u'time': u'*',
318  u'testsuite': [
319  {
320  u'name': u'HasTypeParamAttribute',
321  u'type_param': u'int',
322  u'status': u'RUN',
323  u'time': u'*',
324  u'classname': u'TypedTest/0'
325  }
326  ]
327  },
328  {
329  u'name': u'TypedTest/1',
330  u'tests': 1,
331  u'failures': 0,
332  u'disabled': 0,
333  u'errors': 0,
334  u'time': u'*',
335  u'testsuite': [
336  {
337  u'name': u'HasTypeParamAttribute',
338  u'type_param': u'long',
339  u'status': u'RUN',
340  u'time': u'*',
341  u'classname': u'TypedTest/1'
342  }
343  ]
344  },
345  {
346  u'name': u'Single/TypeParameterizedTestSuite/0',
347  u'tests': 1,
348  u'failures': 0,
349  u'disabled': 0,
350  u'errors': 0,
351  u'time': u'*',
352  u'testsuite': [
353  {
354  u'name': u'HasTypeParamAttribute',
355  u'type_param': u'int',
356  u'status': u'RUN',
357  u'time': u'*',
358  u'classname': u'Single/TypeParameterizedTestSuite/0'
359  }
360  ]
361  },
362  {
363  u'name': u'Single/TypeParameterizedTestSuite/1',
364  u'tests': 1,
365  u'failures': 0,
366  u'disabled': 0,
367  u'errors': 0,
368  u'time': u'*',
369  u'testsuite': [
370  {
371  u'name': u'HasTypeParamAttribute',
372  u'type_param': u'long',
373  u'status': u'RUN',
374  u'time': u'*',
375  u'classname': u'Single/TypeParameterizedTestSuite/1'
376  }
377  ]
378  },
379  {
380  u'name': u'Single/ValueParamTest',
381  u'tests': 4,
382  u'failures': 0,
383  u'disabled': 0,
384  u'errors': 0,
385  u'time': u'*',
386  u'testsuite': [
387  {
388  u'name': u'HasValueParamAttribute/0',
389  u'value_param': u'33',
390  u'status': u'RUN',
391  u'time': u'*',
392  u'classname': u'Single/ValueParamTest'
393  },
394  {
395  u'name': u'HasValueParamAttribute/1',
396  u'value_param': u'42',
397  u'status': u'RUN',
398  u'time': u'*',
399  u'classname': u'Single/ValueParamTest'
400  },
401  {
402  u'name': u'AnotherTestThatHasValueParamAttribute/0',
403  u'value_param': u'33',
404  u'status': u'RUN',
405  u'time': u'*',
406  u'classname': u'Single/ValueParamTest'
407  },
408  {
409  u'name': u'AnotherTestThatHasValueParamAttribute/1',
410  u'value_param': u'42',
411  u'status': u'RUN',
412  u'time': u'*',
413  u'classname': u'Single/ValueParamTest'
414  }
415  ]
416  }
417  ]
418 }
419 
420 EXPECTED_FILTERED = {
421  u'tests': 1,
422  u'failures': 0,
423  u'disabled': 0,
424  u'errors': 0,
425  u'time': u'*',
426  u'timestamp': u'*',
427  u'name': u'AllTests',
428  u'ad_hoc_property': u'42',
429  u'testsuites': [{
430  u'name': u'SuccessfulTest',
431  u'tests': 1,
432  u'failures': 0,
433  u'disabled': 0,
434  u'errors': 0,
435  u'time': u'*',
436  u'testsuite': [{
437  u'name': u'Succeeds',
438  u'status': u'RUN',
439  u'time': u'*',
440  u'classname': u'SuccessfulTest',
441  }]
442  }],
443 }
444 
445 EXPECTED_EMPTY = {
446  u'tests': 0,
447  u'failures': 0,
448  u'disabled': 0,
449  u'errors': 0,
450  u'time': u'*',
451  u'timestamp': u'*',
452  u'name': u'AllTests',
453  u'testsuites': [],
454 }
455 
456 GTEST_PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
457 
458 SUPPORTS_TYPED_TESTS = 'TypedTest' in gtest_test_utils.Subprocess(
459  [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=False).output
460 
461 
462 class GTestJsonOutputUnitTest(gtest_test_utils.TestCase):
463  """Unit test for Google Test's JSON output functionality.
464  """
465 
466  # This test currently breaks on platforms that do not support typed and
467  # type-parameterized tests, so we don't run it under them.
468  if SUPPORTS_TYPED_TESTS:
469 
471  """Verifies JSON output for a Google Test binary with non-empty output.
472 
473  Runs a test program that generates a non-empty JSON output, and
474  tests that the JSON output is expected.
475  """
476  self._TestJsonOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY, 1)
477 
479  """Verifies JSON output for a Google Test binary without actual tests.
480 
481  Runs a test program that generates an empty JSON output, and
482  tests that the JSON output is expected.
483  """
484 
485  self._TestJsonOutput('gtest_no_test_unittest', EXPECTED_EMPTY, 0)
486 
488  """Checks whether the timestamp attribute in the JSON output is valid.
489 
490  Runs a test program that generates an empty JSON output, and checks if
491  the timestamp attribute in the testsuites tag is valid.
492  """
493  actual = self._GetJsonOutput('gtest_no_test_unittest', [], 0)
494  date_time_str = actual['timestamp']
495  # datetime.strptime() is only available in Python 2.5+ so we have to
496  # parse the expected datetime manually.
497  match = re.match(r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
498  self.assertTrue(
499  re.match,
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)))
505 
506  time_delta = abs(datetime.datetime.now() - date_time_from_json)
507  # timestamp value should be near the current local time
508  self.assertTrue(time_delta < datetime.timedelta(seconds=600),
509  'time_delta is %s' % time_delta)
510 
512  """Verifies the default output file name.
513 
514  Confirms that Google Test produces an JSON output file with the expected
515  default name if no name is explicitly specified.
516  """
517  output_file = os.path.join(gtest_test_utils.GetTempDir(),
518  GTEST_DEFAULT_OUTPUT_FILE)
519  gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
520  'gtest_no_test_unittest')
521  try:
522  os.remove(output_file)
523  except OSError:
524  e = sys.exc_info()[1]
525  if e.errno != errno.ENOENT:
526  raise
527 
529  [gtest_prog_path, '%s=json' % GTEST_OUTPUT_FLAG],
530  working_dir=gtest_test_utils.GetTempDir())
531  self.assert_(p.exited)
532  self.assertEquals(0, p.exit_code)
533  self.assert_(os.path.isfile(output_file))
534 
536  """Verifies that no JSON output is generated.
537 
538  Tests that no JSON file is generated if the default JSON listener is
539  shut down before RUN_ALL_TESTS is invoked.
540  """
541 
542  json_path = os.path.join(gtest_test_utils.GetTempDir(),
543  GTEST_PROGRAM_NAME + 'out.json')
544  if os.path.isfile(json_path):
545  os.remove(json_path)
546 
547  command = [GTEST_PROGRAM_PATH,
548  '%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path),
549  '--shut_down_xml']
550  p = gtest_test_utils.Subprocess(command)
551  if p.terminated_by_signal:
552  # p.signal is available only if p.terminated_by_signal is True.
553  self.assertFalse(
554  p.terminated_by_signal,
555  '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal))
556  else:
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))
562 
563  self.assert_(not os.path.isfile(json_path))
564 
566  """Verifies JSON output when a filter is applied.
567 
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.
570  """
571 
572  self._TestJsonOutput(GTEST_PROGRAM_NAME, EXPECTED_FILTERED, 0,
573  extra_args=['%s=SuccessfulTest.*' % GTEST_FILTER_FLAG])
574 
575  def _GetJsonOutput(self, gtest_prog_name, extra_args, expected_exit_code):
576  """Returns the JSON output generated by running the program gtest_prog_name.
577 
578  Furthermore, the program's exit code must be expected_exit_code.
579 
580  Args:
581  gtest_prog_name: Google Test binary name.
582  extra_args: extra arguments to binary invocation.
583  expected_exit_code: program's exit code.
584  """
585  json_path = os.path.join(gtest_test_utils.GetTempDir(),
586  gtest_prog_name + 'out.json')
587  gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
588 
589  command = (
590  [gtest_prog_path, '%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path)] +
591  extra_args
592  )
593  p = gtest_test_utils.Subprocess(command)
594  if p.terminated_by_signal:
595  self.assert_(False,
596  '%s was killed by signal %d' % (gtest_prog_name, p.signal))
597  else:
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)
605  return actual
606 
607  def _TestJsonOutput(self, gtest_prog_name, expected,
608  expected_exit_code, extra_args=None):
609  """Checks the JSON output generated by the Google Test binary.
610 
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
614  expected_exit_code.
615 
616  Args:
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.
621  """
622 
623  actual = self._GetJsonOutput(gtest_prog_name, extra_args or [],
624  expected_exit_code)
625  self.assertEqual(expected, gtest_json_test_utils.normalize(actual))
626 
627 
628 if __name__ == '__main__':
629  if NO_STACKTRACE_SUPPORT_FLAG in sys.argv:
630  # unittest.main() can't handle unknown flags
631  sys.argv.remove(NO_STACKTRACE_SUPPORT_FLAG)
632 
633  os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
googletest-json-output-unittest.GTestJsonOutputUnitTest._TestJsonOutput
def _TestJsonOutput(self, gtest_prog_name, expected, expected_exit_code, extra_args=None)
Definition: bloaty/third_party/googletest/googletest/test/googletest-json-output-unittest.py:751
googletest-json-output-unittest.GTestJsonOutputUnitTest.testFilteredTestJsonOutput
def testFilteredTestJsonOutput(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-json-output-unittest.py:709
gtest_test_utils.Subprocess
Definition: bloaty/third_party/googletest/googletest/test/gtest_test_utils.py:202
googletest-json-output-unittest.GTestJsonOutputUnitTest.testSuppressedJsonOutput
def testSuppressedJsonOutput(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-json-output-unittest.py:679
googletest-json-output-unittest.GTestJsonOutputUnitTest._GetJsonOutput
def _GetJsonOutput(self, gtest_prog_name, extra_args, expected_exit_code)
Definition: bloaty/third_party/googletest/googletest/test/googletest-json-output-unittest.py:719
xds_interop_client.int
int
Definition: xds_interop_client.py:113
gtest_test_utils.GetTempDir
def GetTempDir()
Definition: bloaty/third_party/googletest/googletest/test/gtest_test_utils.py:144
gtest_test_utils.GetTestExecutablePath
def GetTestExecutablePath(executable_name, build_dir=None)
Definition: bloaty/third_party/googletest/googletest/test/gtest_test_utils.py:151
gtest_json_test_utils.normalize
def normalize(obj)
Definition: bloaty/third_party/googletest/googletest/test/gtest_json_test_utils.py:35
googletest-json-output-unittest.GTestJsonOutputUnitTest.testDefaultOutputFile
def testDefaultOutputFile(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-json-output-unittest.py:655
googletest-json-output-unittest.GTestJsonOutputUnitTest.testNonEmptyJsonOutput
def testNonEmptyJsonOutput(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-json-output-unittest.py:614
googletest-json-output-unittest.GTestJsonOutputUnitTest.testEmptyJsonOutput
def testEmptyJsonOutput(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-json-output-unittest.py:622
googletest-json-output-unittest.GTestJsonOutputUnitTest.testTimestampValue
def testTimestampValue(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-json-output-unittest.py:631
open
#define open
Definition: test-fs.c:46
gtest_test_utils.Main
def Main()
Definition: bloaty/third_party/googletest/googletest/test/gtest_test_utils.py:301
gtest_test_utils.TestCase
TestCase
Definition: bloaty/third_party/googletest/googletest/test/gtest_test_utils.py:74


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:40