31 """Unit test for Google Test fail_fast.
33 A user can specify if a Google Test program should continue test execution
34 after a test failure via the GTEST_FAIL_FAST environment variable or the
35 --gtest_fail_fast flag. The default value of the flag can also be changed
36 by Bazel fail fast environment variable TESTBRIDGE_TEST_RUNNER_FAIL_FAST.
38 This script tests such functionality by invoking googletest-failfast-unittest_
39 (a program written with Google Test) with different environments and command
44 from googletest.test
import gtest_test_utils
49 BAZEL_FAIL_FAST_ENV_VAR =
'TESTBRIDGE_TEST_RUNNER_FAIL_FAST'
52 FAIL_FAST_ENV_VAR =
'GTEST_FAIL_FAST'
55 FAIL_FAST_FLAG =
'gtest_fail_fast'
58 RUN_DISABLED_FLAG =
'gtest_also_run_disabled_tests'
61 FILTER_FLAG =
'gtest_filter'
65 'googletest-failfast-unittest_')
69 LIST_TESTS_FLAG =
'--gtest_list_tests'
73 [COMMAND, LIST_TESTS_FLAG]).output
77 environ = os.environ.copy()
81 """Sets the env variable to 'value'; unsets it when 'value' is None."""
84 environ[env_var] = value
85 elif env_var
in environ:
90 """Runs the test program and returns its output."""
94 '.GTestFailFastUnitTest.xml')
95 args += [
'--gtest_output=xml:' + xml_path]
96 if fail_fast
is not None:
97 if isinstance(fail_fast, str):
98 args += [
'--%s=%s' % (FAIL_FAST_FLAG, fail_fast)]
100 args += [
'--%s' % FAIL_FAST_FLAG]
102 args += [
'--no%s' % FAIL_FAST_FLAG]
104 args += [
'--%s=%s.*' % (FILTER_FLAG, test_suite)]
106 args += [
'--%s' % RUN_DISABLED_FLAG]
108 with open(xml_path)
as xml_file:
109 return txt_out, xml_file.read()
114 """Tests the env variable or the command line flag for fail_fast."""
117 """Tests the behavior of not specifying the fail_fast."""
120 self.assertIn(
'22 FAILED TEST', txt)
124 self.assertIn(
'1 FAILED TEST', txt)
125 self.assertIn(
'[ SKIPPED ] 3 tests', txt)
128 self.assertIn(
'4 FAILED TEST', txt)
129 self.assertNotIn(
'[ SKIPPED ]', txt)
132 """Tests the behavior of specifying fail_fast via Googletest env var."""
137 self.assertIn(
'1 FAILED TEST', txt)
138 self.assertIn(
'[ SKIPPED ] 3 tests', txt)
142 self.assertIn(
'4 FAILED TEST', txt)
143 self.assertNotIn(
'[ SKIPPED ]', txt)
148 """Tests the behavior of specifying fail_fast via Bazel testbridge."""
153 self.assertIn(
'1 FAILED TEST', txt)
154 self.assertIn(
'[ SKIPPED ] 3 tests', txt)
158 self.assertIn(
'4 FAILED TEST', txt)
159 self.assertNotIn(
'[ SKIPPED ]', txt)
164 """Tests precedence of flag over env var."""
169 self.assertIn(
'1 FAILED TEST', txt)
170 self.assertIn(
'[ SKIPPED ] 3 tests', txt)
175 """Tests that the Googletest native env var over Bazel testbridge."""
181 self.assertIn(
'1 FAILED TEST', txt)
182 self.assertIn(
'[ SKIPPED ] 3 tests', txt)
189 self.assertIn(
'1 FAILED TEST', txt)
190 self.assertIn(
'[ SKIPPED ] 3 tests', txt)
191 for expected_count, callback
in [(1,
'OnTestSuiteStart'),
194 (5,
'OnTestPartResult'),
195 (1,
'OnTestSuiteEnd')]:
197 expected_count, txt.count(callback),
198 'Expected %d calls to callback %s match count on output: %s ' %
199 (expected_count, callback, txt))
202 self.assertIn(
'3 FAILED TEST', txt)
203 self.assertIn(
'[ SKIPPED ] 1 test', txt)
204 for expected_count, callback
in [(1,
'OnTestSuiteStart'),
207 (5,
'OnTestPartResult'),
208 (1,
'OnTestSuiteEnd')]:
210 expected_count, txt.count(callback),
211 'Expected %d calls to callback %s match count on output: %s ' %
212 (expected_count, callback, txt))
216 count, xml.count(
'result="%s"' % result),
217 'Expected \'result="%s"\' match count of %s: %s ' %
218 (result, count, xml))
222 count, xml.count(
'status="%s"' % status),
223 'Expected \'status="%s"\' match count of %s: %s ' %
224 (status, count, xml))
234 """Assert XML and text output of a test execution."""
237 if failure_count > 0:
238 self.assertIn(
'%s FAILED TEST' % failure_count, txt)
239 if suppressed_count > 0:
240 self.assertIn(
'%s DISABLED TEST' % suppressed_count, txt)
241 if skipped_count > 0:
242 self.assertIn(
'[ SKIPPED ] %s tests' % skipped_count, txt)
244 passed_count + failure_count + skipped_count, xml)
257 """Assert --fail_fast via flag."""
259 for fail_fast
in (
'true',
'1',
't',
True):
261 failure_count, skipped_count,
262 suppressed_count, run_disabled)
271 """Assert --nofail_fast via flag."""
273 for fail_fast
in (
'false',
'0',
'f',
False):
275 failure_count, skipped_count,
276 suppressed_count, run_disabled)
279 """Tests the behavior of fail_fast and TEST_F."""
281 test_suite=
'HasFixtureTest',
287 test_suite=
'HasFixtureTest',
294 """Tests the behavior of fail_fast and TEST."""
296 test_suite=
'HasSimpleTest',
302 test_suite=
'HasSimpleTest',
309 """Tests the behavior of fail_fast and TEST_P."""
311 test_suite=
'HasParametersSuite/HasParametersTest',
317 test_suite=
'HasParametersSuite/HasParametersTest',
324 """Tests the behavior of fail_fast and Disabled test cases."""
326 test_suite=
'HasDisabledTest',
333 test_suite=
'HasDisabledTest',
341 """Tests the behavior of fail_fast and Disabled test cases enabled."""
343 test_suite=
'HasDisabledTest',
350 test_suite=
'HasDisabledTest',
358 """Tests the behavior of fail_fast and Disabled test suites."""
360 test_suite=
'DISABLED_HasDisabledSuite',
367 test_suite=
'DISABLED_HasDisabledSuite',
375 """Tests the behavior of fail_fast and Disabled test suites enabled."""
377 test_suite=
'DISABLED_HasDisabledSuite',
384 test_suite=
'DISABLED_HasDisabledSuite',
391 if SUPPORTS_DEATH_TESTS:
394 """Tests the behavior of fail_fast and death tests."""
396 test_suite=
'HasDeathTest',
402 test_suite=
'HasDeathTest',
409 if __name__ ==
'__main__':