32 """Tests the text output of Google C++ Testing Framework.
35 gtest_output_test.py --build_dir=BUILD/DIR --gengolden
36 # where BUILD/DIR contains the built gtest_output_test_ file.
37 gtest_output_test.py --gengolden
41 __author__ =
'wan@google.com (Zhanyong Wan)'
47 import gtest_test_utils
51 GENGOLDEN_FLAG =
'--gengolden'
52 CATCH_EXCEPTIONS_ENV_VAR_NAME =
'GTEST_CATCH_EXCEPTIONS'
54 IS_WINDOWS = os.name ==
'nt'
57 GOLDEN_NAME =
'gtest_output_test_golden_lin.txt'
63 COMMAND_LIST_TESTS = ({}, [PROGRAM_PATH,
'--gtest_list_tests'])
64 COMMAND_WITH_COLOR = ({}, [PROGRAM_PATH,
'--gtest_color=yes'])
65 COMMAND_WITH_TIME = ({}, [PROGRAM_PATH,
67 'internal_skip_environment_and_ad_hoc_tests',
68 '--gtest_filter=FatalFailureTest.*:LoggingTest.*'])
69 COMMAND_WITH_DISABLED = (
71 '--gtest_also_run_disabled_tests',
72 'internal_skip_environment_and_ad_hoc_tests',
73 '--gtest_filter=*DISABLED_*'])
74 COMMAND_WITH_SHARDING = (
75 {
'GTEST_SHARD_INDEX':
'1',
'GTEST_TOTAL_SHARDS':
'2'},
77 'internal_skip_environment_and_ad_hoc_tests',
78 '--gtest_filter=PassingTest.*'])
84 """Changes all Windows/Mac line endings in s to UNIX line endings."""
86 return s.replace(
'\r\n',
'\n').replace(
'\r',
'\n')
90 """Removes all file location info from a Google Test program's output.
93 test_output: the output of a Google Test program.
96 output with all file location info (in the form of
97 'DIRECTORY/FILE_NAME:LINE_NUMBER: 'or
98 'DIRECTORY\\FILE_NAME(LINE_NUMBER): ') replaced by
102 return re.sub(
r'.*[/\\](.+)(\:\d+|\(\d+\))\: ',
r'\1:#: ', test_output)
106 """Removes all stack traces from a Google Test program's output."""
109 return re.sub(
r'Stack trace:(.|\n)*?\n\n',
110 'Stack trace: (omitted)\n\n', output)
114 """Removes all traces of stack traces from a Google Test program's output."""
117 return re.sub(
r'Stack trace:(.|\n)*?\n\n',
'', output)
121 """Removes all time information from a Google Test program's output."""
123 return re.sub(
r'\(\d+ ms',
'(? ms', output)
127 """Removes compiler-specific type info from Google Test program's output.
130 test_output: the output of a Google Test program.
133 output with type information normalized to canonical form.
137 return re.sub(
r'unsigned int',
'unsigned', test_output)
141 """Normalizes platform specific output details for easier comparison."""
145 test_output = re.sub(
'\x1b\\[(0;3\d)?m',
'', test_output)
147 test_output = re.sub(
r': Failure\n',
r': error: ', test_output)
149 test_output = re.sub(
r'((\w|\.)+)\((\d+)\):',
r'\1:\3:', test_output)
155 """Removes test counts from a Google Test program's output."""
157 output = re.sub(
r'\d+ tests?, listed below',
158 '? tests, listed below', output)
159 output = re.sub(
r'\d+ FAILED TESTS',
160 '? FAILED TESTS', output)
161 output = re.sub(
r'\d+ tests? from \d+ test cases?',
162 '? tests from ? test cases', output)
163 output = re.sub(
r'\d+ tests? from ([a-zA-Z_])',
164 r'? tests from \1', output)
165 return re.sub(
r'\d+ tests?\.',
'? tests.', output)
169 """Removes output of specified tests from a Google Test program's output.
171 This function strips not only the beginning and the end of a test but also
172 all output in between.
175 test_output: A string containing the test output.
176 pattern: A regex string that matches names of test cases or
180 Contents of test_output with tests whose names match pattern removed.
183 test_output = re.sub(
184 r'.*\[ RUN \] .*%s(.|\n)*?\[( FAILED | OK )\] .*%s.*\n' % (
188 return re.sub(
r'.*%s.*\n' % pattern,
'', test_output)
192 """Normalizes output (the output of gtest_output_test_.exe)."""
202 """Runs a command in a sub-process, and returns its output in a string.
205 env_cmd: The shell command. A 2-tuple where element 0 is a dict of extra
206 environment variables to set, and element 1 is a string with
207 the command and any flags.
210 A string with the command's combined standard and diagnostic output.
215 environ = os.environ.copy()
216 environ.update(env_cmd[0])
223 """Runs a command and returns its output with all file location
227 env_cmd: The shell command. A 2-tuple where element 0 is a dict of extra
228 environment variables to set, and element 1 is a string with
229 the command and any flags.
233 environ, cmdline = env_cmd
234 environ = dict(environ)
235 environ[CATCH_EXCEPTIONS_ENV_VAR_NAME] =
'1'
240 """Returns concatenated output from several representative commands."""
249 SUPPORTS_DEATH_TESTS =
'DeathTest' in test_list
250 SUPPORTS_TYPED_TESTS =
'TypedTest' in test_list
251 SUPPORTS_THREADS =
'ExpectFailureWithThreadsTest' in test_list
252 SUPPORTS_STACK_TRACES =
False
254 CAN_GENERATE_GOLDEN_FILE = (SUPPORTS_DEATH_TESTS
and
255 SUPPORTS_TYPED_TESTS
and
261 if not SUPPORTS_DEATH_TESTS:
263 if not SUPPORTS_TYPED_TESTS:
267 if not SUPPORTS_THREADS:
269 'ExpectFailureWithThreadsTest')
271 'ScopedFakeTestPartResultReporterTest')
274 if not SUPPORTS_STACK_TRACES:
282 golden_file =
open(GOLDEN_PATH,
'r')
297 if CAN_GENERATE_GOLDEN_FILE:
298 self.assertEqual(normalized_golden, normalized_actual,
299 '\n'.
join(difflib.unified_diff(
300 normalized_golden.split(
'\n'),
301 normalized_actual.split(
'\n'),
302 'golden',
'actual')))
310 if os.getenv(
'DEBUG_GTEST_OUTPUT_TEST'):
313 '_gtest_output_test_normalized_actual.txt'),
'wb').
write(
317 '_gtest_output_test_normalized_golden.txt'),
'wb').
write(
320 self.assertEqual(normalized_golden, normalized_actual)
323 if __name__ ==
'__main__':
324 if sys.argv[1:] == [GENGOLDEN_FLAG]:
325 if CAN_GENERATE_GOLDEN_FILE:
327 golden_file =
open(GOLDEN_PATH,
'wb')
328 golden_file.write(output)
332 """Unable to write a golden file when compiled in an environment
333 that does not support all the required features (death tests, typed tests,
334 and multiple threads). Please generate the golden file using a binary built
335 with those features enabled.""")
337 sys.stderr.write(message)