diff options
Diffstat (limited to 'gtest/test/gtest_filter_unittest.py')
-rwxr-xr-x | gtest/test/gtest_filter_unittest.py | 189 |
1 files changed, 75 insertions, 114 deletions
diff --git a/gtest/test/gtest_filter_unittest.py b/gtest/test/gtest_filter_unittest.py index 0d1a770..a94a521 100755 --- a/gtest/test/gtest_filter_unittest.py +++ b/gtest/test/gtest_filter_unittest.py @@ -45,42 +45,11 @@ __author__ = 'wan@google.com (Zhanyong Wan)' import os import re import sets -import sys - import gtest_test_utils # Constants. -# Checks if this platform can pass empty environment variables to child -# processes. We set an env variable to an empty string and invoke a python -# script in a subprocess to print whether the variable is STILL in -# os.environ. We then use 'eval' to parse the child's output so that an -# exception is thrown if the input is anything other than 'True' nor 'False'. -os.environ['EMPTY_VAR'] = '' -child = gtest_test_utils.Subprocess( - [sys.executable, '-c', 'import os; print \'EMPTY_VAR\' in os.environ']) -CAN_PASS_EMPTY_ENV = eval(child.output) - - -# Check if this platform can unset environment variables in child processes. -# We set an env variable to a non-empty string, unset it, and invoke -# a python script in a subprocess to print whether the variable -# is NO LONGER in os.environ. -# We use 'eval' to parse the child's output so that an exception -# is thrown if the input is neither 'True' nor 'False'. -os.environ['UNSET_VAR'] = 'X' -del os.environ['UNSET_VAR'] -child = gtest_test_utils.Subprocess( - [sys.executable, '-c', 'import os; print \'UNSET_VAR\' not in os.environ']) -CAN_UNSET_ENV = eval(child.output) - - -# Checks if we should test with an empty filter. This doesn't -# make sense on platforms that cannot pass empty env variables (Win32) -# and on platforms that cannot unset variables (since we cannot tell -# the difference between "" and NULL -- Borland and Solaris < 5.10) -CAN_TEST_EMPTY_FILTER = (CAN_PASS_EMPTY_ENV and CAN_UNSET_ENV) - +IS_WINDOWS = os.name == 'nt' # The environment variable for specifying the test filters. FILTER_ENV_VAR = 'GTEST_FILTER' @@ -108,14 +77,6 @@ TEST_CASE_REGEX = re.compile(r'^\[\-+\] \d+ tests? from (\w+(/\w+)?)') # Regex for parsing test names from Google Test's output. TEST_REGEX = re.compile(r'^\[\s*RUN\s*\].*\.(\w+(/\w+)?)') -# The command line flag to tell Google Test to output the list of tests it -# will run. -LIST_TESTS_FLAG = '--gtest_list_tests' - -# Indicates whether Google Test supports death tests. -SUPPORTS_DEATH_TESTS = 'HasDeathTest' in gtest_test_utils.Subprocess( - [COMMAND, LIST_TESTS_FLAG]).output - # Full names of all tests in gtest_filter_unittests_. PARAM_TESTS = [ 'SeqP/ParamTest.TestX/0', @@ -137,14 +98,6 @@ DISABLED_TESTS = [ 'DISABLED_FoobarbazTest.TestA', ] -if SUPPORTS_DEATH_TESTS: - DEATH_TESTS = [ - 'HasDeathTest.Test1', - 'HasDeathTest.Test2', - ] -else: - DEATH_TESTS = [] - # All the non-disabled tests. ACTIVE_TESTS = [ 'FooTest.Abc', @@ -157,35 +110,35 @@ ACTIVE_TESTS = [ 'BazTest.TestOne', 'BazTest.TestA', 'BazTest.TestB', - ] + DEATH_TESTS + PARAM_TESTS + + 'HasDeathTest.Test1', + 'HasDeathTest.Test2', + ] + PARAM_TESTS param_tests_present = None # Utilities. -environ = os.environ.copy() - def SetEnvVar(env_var, value): """Sets the env variable to 'value'; unsets it when 'value' is None.""" if value is not None: - environ[env_var] = value - elif env_var in environ: - del environ[env_var] + os.environ[env_var] = value + elif env_var in os.environ: + del os.environ[env_var] def RunAndReturnOutput(args = None): """Runs the test program and returns its output.""" - return gtest_test_utils.Subprocess([COMMAND] + (args or []), - env=environ).output + return gtest_test_utils.Subprocess([COMMAND] + (args or [])).output def RunAndExtractTestList(args = None): """Runs the test program and returns its exit code and a list of tests run.""" - p = gtest_test_utils.Subprocess([COMMAND] + (args or []), env=environ) + p = gtest_test_utils.Subprocess([COMMAND] + (args or [])) tests_run = [] test_case = '' test = '' @@ -204,12 +157,15 @@ def RunAndExtractTestList(args = None): def InvokeWithModifiedEnv(extra_env, function, *args, **kwargs): """Runs the given function and arguments in a modified environment.""" try: - original_env = environ.copy() - environ.update(extra_env) + original_env = os.environ.copy() + os.environ.update(extra_env) return function(*args, **kwargs) finally: - environ.clear() - environ.update(original_env) + for key in extra_env.iterkeys(): + if key in original_env: + os.environ[key] = original_env[key] + else: + del os.environ[key] def RunWithSharding(total_shards, shard_index, command): @@ -223,7 +179,7 @@ def RunWithSharding(total_shards, shard_index, command): class GTestFilterUnitTest(gtest_test_utils.TestCase): - """Tests the env variable or the command line flag to filter tests.""" + """Tests GTEST_FILTER env variable or --gtest_filter flag to filter tests.""" # Utilities. @@ -255,26 +211,26 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase): return tests_to_run def RunAndVerify(self, gtest_filter, tests_to_run): - """Checks that the binary runs correct set of tests for a given filter.""" + """Checks that the binary runs correct set of tests for the given filter.""" tests_to_run = self.AdjustForParameterizedTests(tests_to_run) - # First, tests using the environment variable. + # First, tests using GTEST_FILTER. # Windows removes empty variables from the environment when passing it - # to a new process. This means it is impossible to pass an empty filter - # into a process using the environment variable. However, we can still - # test the case when the variable is not supplied (i.e., gtest_filter is - # None). + # to a new process. This means it is impossible to pass an empty filter + # into a process using the GTEST_FILTER environment variable. However, + # we can still test the case when the variable is not supplied (i.e., + # gtest_filter is None). # pylint: disable-msg=C6403 - if CAN_TEST_EMPTY_FILTER or gtest_filter != '': + if not IS_WINDOWS or gtest_filter != '': SetEnvVar(FILTER_ENV_VAR, gtest_filter) tests_run = RunAndExtractTestList()[0] SetEnvVar(FILTER_ENV_VAR, None) self.AssertSetEqual(tests_run, tests_to_run) # pylint: enable-msg=C6403 - # Next, tests using the command line flag. + # Next, tests using --gtest_filter. if gtest_filter is None: args = [] @@ -304,12 +260,12 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase): tests_to_run = self.AdjustForParameterizedTests(tests_to_run) # Windows removes empty variables from the environment when passing it - # to a new process. This means it is impossible to pass an empty filter - # into a process using the environment variable. However, we can still - # test the case when the variable is not supplied (i.e., gtest_filter is - # None). + # to a new process. This means it is impossible to pass an empty filter + # into a process using the GTEST_FILTER environment variable. However, + # we can still test the case when the variable is not supplied (i.e., + # gtest_filter is None). # pylint: disable-msg=C6403 - if CAN_TEST_EMPTY_FILTER or gtest_filter != '': + if not IS_WINDOWS or gtest_filter != '': SetEnvVar(FILTER_ENV_VAR, gtest_filter) partition = [] for i in range(0, total_shards): @@ -448,7 +404,10 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase): 'BazTest.TestOne', 'BazTest.TestA', - 'BazTest.TestB', ] + DEATH_TESTS + PARAM_TESTS) + 'BazTest.TestB', + + 'HasDeathTest.Test1', + 'HasDeathTest.Test2', ] + PARAM_TESTS) def testWildcardInTestName(self): """Tests using wildcard in the test name.""" @@ -509,7 +468,7 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase): ]) def testNegativeFilters(self): - self.RunAndVerify('*-BazTest.TestOne', [ + self.RunAndVerify('*-HasDeathTest.Test1', [ 'FooTest.Abc', 'FooTest.Xyz', @@ -517,17 +476,24 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase): 'BarTest.TestTwo', 'BarTest.TestThree', + 'BazTest.TestOne', 'BazTest.TestA', 'BazTest.TestB', - ] + DEATH_TESTS + PARAM_TESTS) - self.RunAndVerify('*-FooTest.Abc:BazTest.*', [ + 'HasDeathTest.Test2', + ] + PARAM_TESTS) + + self.RunAndVerify('*-FooTest.Abc:HasDeathTest.*', [ 'FooTest.Xyz', 'BarTest.TestOne', 'BarTest.TestTwo', 'BarTest.TestThree', - ] + DEATH_TESTS + PARAM_TESTS) + + 'BazTest.TestOne', + 'BazTest.TestA', + 'BazTest.TestB', + ] + PARAM_TESTS) self.RunAndVerify('BarTest.*-BarTest.TestOne', [ 'BarTest.TestTwo', @@ -535,11 +501,15 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase): ]) # Tests without leading '*'. - self.RunAndVerify('-FooTest.Abc:FooTest.Xyz:BazTest.*', [ + self.RunAndVerify('-FooTest.Abc:FooTest.Xyz:HasDeathTest.*', [ 'BarTest.TestOne', 'BarTest.TestTwo', 'BarTest.TestThree', - ] + DEATH_TESTS + PARAM_TESTS) + + 'BazTest.TestOne', + 'BazTest.TestA', + 'BazTest.TestB', + ] + PARAM_TESTS) # Value parameterized tests. self.RunAndVerify('*/*', PARAM_TESTS) @@ -585,7 +555,7 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase): os.remove(shard_status_file) def testShardStatusFileIsCreatedWithListTests(self): - """Tests that the shard file is created with the "list_tests" flag.""" + """Tests that the shard file is created with --gtest_list_tests.""" shard_status_file = os.path.join(gtest_test_utils.GetTempDir(), 'shard_status_file2') @@ -593,41 +563,32 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase): extra_env = {SHARD_STATUS_FILE_ENV_VAR: shard_status_file} try: - output = InvokeWithModifiedEnv(extra_env, - RunAndReturnOutput, - [LIST_TESTS_FLAG]) + InvokeWithModifiedEnv(extra_env, + RunAndReturnOutput, + ['--gtest_list_tests']) finally: - # This assertion ensures that Google Test enumerated the tests as - # opposed to running them. - self.assert_('[==========]' not in output, - 'Unexpected output during test enumeration.\n' - 'Please ensure that LIST_TESTS_FLAG is assigned the\n' - 'correct flag value for listing Google Test tests.') - self.assert_(os.path.exists(shard_status_file)) os.remove(shard_status_file) - if SUPPORTS_DEATH_TESTS: - def testShardingWorksWithDeathTests(self): - """Tests integration with death tests and sharding.""" - - gtest_filter = 'HasDeathTest.*:SeqP/*' - expected_tests = [ - 'HasDeathTest.Test1', - 'HasDeathTest.Test2', - - 'SeqP/ParamTest.TestX/0', - 'SeqP/ParamTest.TestX/1', - 'SeqP/ParamTest.TestY/0', - 'SeqP/ParamTest.TestY/1', - ] - - for flag in ['--gtest_death_test_style=threadsafe', - '--gtest_death_test_style=fast']: - self.RunAndVerifyWithSharding(gtest_filter, 3, expected_tests, - check_exit_0=True, args=[flag]) - self.RunAndVerifyWithSharding(gtest_filter, 5, expected_tests, - check_exit_0=True, args=[flag]) + def testShardingWorksWithDeathTests(self): + """Tests integration with death tests and sharding.""" + gtest_filter = 'HasDeathTest.*:SeqP/*' + expected_tests = [ + 'HasDeathTest.Test1', + 'HasDeathTest.Test2', + + 'SeqP/ParamTest.TestX/0', + 'SeqP/ParamTest.TestX/1', + 'SeqP/ParamTest.TestY/0', + 'SeqP/ParamTest.TestY/1', + ] + + for flag in ['--gtest_death_test_style=threadsafe', + '--gtest_death_test_style=fast']: + self.RunAndVerifyWithSharding(gtest_filter, 3, expected_tests, + check_exit_0=True, args=[flag]) + self.RunAndVerifyWithSharding(gtest_filter, 5, expected_tests, + check_exit_0=True, args=[flag]) if __name__ == '__main__': gtest_test_utils.Main() |