Fixes gtest_filter_unittest and gtest_help_test on systems without death tests.
This commit is contained in:
parent
d21c142eb8
commit
eddd9e85a8
|
@ -108,6 +108,14 @@ TEST_CASE_REGEX = re.compile(r'^\[\-+\] \d+ tests? from (\w+(/\w+)?)')
|
||||||
# Regex for parsing test names from Google Test's output.
|
# Regex for parsing test names from Google Test's output.
|
||||||
TEST_REGEX = re.compile(r'^\[\s*RUN\s*\].*\.(\w+(/\w+)?)')
|
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_.
|
# Full names of all tests in gtest_filter_unittests_.
|
||||||
PARAM_TESTS = [
|
PARAM_TESTS = [
|
||||||
'SeqP/ParamTest.TestX/0',
|
'SeqP/ParamTest.TestX/0',
|
||||||
|
@ -129,6 +137,14 @@ DISABLED_TESTS = [
|
||||||
'DISABLED_FoobarbazTest.TestA',
|
'DISABLED_FoobarbazTest.TestA',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
if SUPPORTS_DEATH_TESTS:
|
||||||
|
DEATH_TESTS = [
|
||||||
|
'HasDeathTest.Test1',
|
||||||
|
'HasDeathTest.Test2',
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
DEATH_TESTS = []
|
||||||
|
|
||||||
# All the non-disabled tests.
|
# All the non-disabled tests.
|
||||||
ACTIVE_TESTS = [
|
ACTIVE_TESTS = [
|
||||||
'FooTest.Abc',
|
'FooTest.Abc',
|
||||||
|
@ -141,10 +157,7 @@ ACTIVE_TESTS = [
|
||||||
'BazTest.TestOne',
|
'BazTest.TestOne',
|
||||||
'BazTest.TestA',
|
'BazTest.TestA',
|
||||||
'BazTest.TestB',
|
'BazTest.TestB',
|
||||||
|
] + DEATH_TESTS + PARAM_TESTS
|
||||||
'HasDeathTest.Test1',
|
|
||||||
'HasDeathTest.Test2',
|
|
||||||
] + PARAM_TESTS
|
|
||||||
|
|
||||||
param_tests_present = None
|
param_tests_present = None
|
||||||
|
|
||||||
|
@ -210,7 +223,7 @@ def RunWithSharding(total_shards, shard_index, command):
|
||||||
|
|
||||||
|
|
||||||
class GTestFilterUnitTest(gtest_test_utils.TestCase):
|
class GTestFilterUnitTest(gtest_test_utils.TestCase):
|
||||||
"""Tests GTEST_FILTER env variable or --gtest_filter flag to filter tests."""
|
"""Tests the env variable or the command line flag to filter tests."""
|
||||||
|
|
||||||
# Utilities.
|
# Utilities.
|
||||||
|
|
||||||
|
@ -242,17 +255,17 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase):
|
||||||
return tests_to_run
|
return tests_to_run
|
||||||
|
|
||||||
def RunAndVerify(self, gtest_filter, tests_to_run):
|
def RunAndVerify(self, gtest_filter, tests_to_run):
|
||||||
"""Checks that the binary runs correct set of tests for the given filter."""
|
"""Checks that the binary runs correct set of tests for a given filter."""
|
||||||
|
|
||||||
tests_to_run = self.AdjustForParameterizedTests(tests_to_run)
|
tests_to_run = self.AdjustForParameterizedTests(tests_to_run)
|
||||||
|
|
||||||
# First, tests using GTEST_FILTER.
|
# First, tests using the environment variable.
|
||||||
|
|
||||||
# Windows removes empty variables from the environment when passing it
|
# Windows removes empty variables from the environment when passing it
|
||||||
# to a new process. This means it is impossible to pass an empty filter
|
# to a new process. This means it is impossible to pass an empty filter
|
||||||
# into a process using the GTEST_FILTER environment variable. However,
|
# into a process using the environment variable. However, we can still
|
||||||
# we can still test the case when the variable is not supplied (i.e.,
|
# test the case when the variable is not supplied (i.e., gtest_filter is
|
||||||
# gtest_filter is None).
|
# None).
|
||||||
# pylint: disable-msg=C6403
|
# pylint: disable-msg=C6403
|
||||||
if CAN_TEST_EMPTY_FILTER or gtest_filter != '':
|
if CAN_TEST_EMPTY_FILTER or gtest_filter != '':
|
||||||
SetEnvVar(FILTER_ENV_VAR, gtest_filter)
|
SetEnvVar(FILTER_ENV_VAR, gtest_filter)
|
||||||
|
@ -261,7 +274,7 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase):
|
||||||
self.AssertSetEqual(tests_run, tests_to_run)
|
self.AssertSetEqual(tests_run, tests_to_run)
|
||||||
# pylint: enable-msg=C6403
|
# pylint: enable-msg=C6403
|
||||||
|
|
||||||
# Next, tests using --gtest_filter.
|
# Next, tests using the command line flag.
|
||||||
|
|
||||||
if gtest_filter is None:
|
if gtest_filter is None:
|
||||||
args = []
|
args = []
|
||||||
|
@ -291,10 +304,10 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase):
|
||||||
tests_to_run = self.AdjustForParameterizedTests(tests_to_run)
|
tests_to_run = self.AdjustForParameterizedTests(tests_to_run)
|
||||||
|
|
||||||
# Windows removes empty variables from the environment when passing it
|
# Windows removes empty variables from the environment when passing it
|
||||||
# to a new process. This means it is impossible to pass an empty filter
|
# to a new process. This means it is impossible to pass an empty filter
|
||||||
# into a process using the GTEST_FILTER environment variable. However,
|
# into a process using the environment variable. However, we can still
|
||||||
# we can still test the case when the variable is not supplied (i.e.,
|
# test the case when the variable is not supplied (i.e., gtest_filter is
|
||||||
# gtest_filter is None).
|
# None).
|
||||||
# pylint: disable-msg=C6403
|
# pylint: disable-msg=C6403
|
||||||
if CAN_TEST_EMPTY_FILTER or gtest_filter != '':
|
if CAN_TEST_EMPTY_FILTER or gtest_filter != '':
|
||||||
SetEnvVar(FILTER_ENV_VAR, gtest_filter)
|
SetEnvVar(FILTER_ENV_VAR, gtest_filter)
|
||||||
|
@ -435,10 +448,7 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase):
|
||||||
|
|
||||||
'BazTest.TestOne',
|
'BazTest.TestOne',
|
||||||
'BazTest.TestA',
|
'BazTest.TestA',
|
||||||
'BazTest.TestB',
|
'BazTest.TestB', ] + DEATH_TESTS + PARAM_TESTS)
|
||||||
|
|
||||||
'HasDeathTest.Test1',
|
|
||||||
'HasDeathTest.Test2', ] + PARAM_TESTS)
|
|
||||||
|
|
||||||
def testWildcardInTestName(self):
|
def testWildcardInTestName(self):
|
||||||
"""Tests using wildcard in the test name."""
|
"""Tests using wildcard in the test name."""
|
||||||
|
@ -499,7 +509,7 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase):
|
||||||
])
|
])
|
||||||
|
|
||||||
def testNegativeFilters(self):
|
def testNegativeFilters(self):
|
||||||
self.RunAndVerify('*-HasDeathTest.Test1', [
|
self.RunAndVerify('*-BazTest.TestOne', [
|
||||||
'FooTest.Abc',
|
'FooTest.Abc',
|
||||||
'FooTest.Xyz',
|
'FooTest.Xyz',
|
||||||
|
|
||||||
|
@ -507,24 +517,17 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase):
|
||||||
'BarTest.TestTwo',
|
'BarTest.TestTwo',
|
||||||
'BarTest.TestThree',
|
'BarTest.TestThree',
|
||||||
|
|
||||||
'BazTest.TestOne',
|
|
||||||
'BazTest.TestA',
|
'BazTest.TestA',
|
||||||
'BazTest.TestB',
|
'BazTest.TestB',
|
||||||
|
] + DEATH_TESTS + PARAM_TESTS)
|
||||||
|
|
||||||
'HasDeathTest.Test2',
|
self.RunAndVerify('*-FooTest.Abc:BazTest.*', [
|
||||||
] + PARAM_TESTS)
|
|
||||||
|
|
||||||
self.RunAndVerify('*-FooTest.Abc:HasDeathTest.*', [
|
|
||||||
'FooTest.Xyz',
|
'FooTest.Xyz',
|
||||||
|
|
||||||
'BarTest.TestOne',
|
'BarTest.TestOne',
|
||||||
'BarTest.TestTwo',
|
'BarTest.TestTwo',
|
||||||
'BarTest.TestThree',
|
'BarTest.TestThree',
|
||||||
|
] + DEATH_TESTS + PARAM_TESTS)
|
||||||
'BazTest.TestOne',
|
|
||||||
'BazTest.TestA',
|
|
||||||
'BazTest.TestB',
|
|
||||||
] + PARAM_TESTS)
|
|
||||||
|
|
||||||
self.RunAndVerify('BarTest.*-BarTest.TestOne', [
|
self.RunAndVerify('BarTest.*-BarTest.TestOne', [
|
||||||
'BarTest.TestTwo',
|
'BarTest.TestTwo',
|
||||||
|
@ -532,15 +535,11 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase):
|
||||||
])
|
])
|
||||||
|
|
||||||
# Tests without leading '*'.
|
# Tests without leading '*'.
|
||||||
self.RunAndVerify('-FooTest.Abc:FooTest.Xyz:HasDeathTest.*', [
|
self.RunAndVerify('-FooTest.Abc:FooTest.Xyz:BazTest.*', [
|
||||||
'BarTest.TestOne',
|
'BarTest.TestOne',
|
||||||
'BarTest.TestTwo',
|
'BarTest.TestTwo',
|
||||||
'BarTest.TestThree',
|
'BarTest.TestThree',
|
||||||
|
] + DEATH_TESTS + PARAM_TESTS)
|
||||||
'BazTest.TestOne',
|
|
||||||
'BazTest.TestA',
|
|
||||||
'BazTest.TestB',
|
|
||||||
] + PARAM_TESTS)
|
|
||||||
|
|
||||||
# Value parameterized tests.
|
# Value parameterized tests.
|
||||||
self.RunAndVerify('*/*', PARAM_TESTS)
|
self.RunAndVerify('*/*', PARAM_TESTS)
|
||||||
|
@ -586,7 +585,7 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase):
|
||||||
os.remove(shard_status_file)
|
os.remove(shard_status_file)
|
||||||
|
|
||||||
def testShardStatusFileIsCreatedWithListTests(self):
|
def testShardStatusFileIsCreatedWithListTests(self):
|
||||||
"""Tests that the shard file is created with --gtest_list_tests."""
|
"""Tests that the shard file is created with the "list_tests" flag."""
|
||||||
|
|
||||||
shard_status_file = os.path.join(gtest_test_utils.GetTempDir(),
|
shard_status_file = os.path.join(gtest_test_utils.GetTempDir(),
|
||||||
'shard_status_file2')
|
'shard_status_file2')
|
||||||
|
@ -594,32 +593,41 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase):
|
||||||
|
|
||||||
extra_env = {SHARD_STATUS_FILE_ENV_VAR: shard_status_file}
|
extra_env = {SHARD_STATUS_FILE_ENV_VAR: shard_status_file}
|
||||||
try:
|
try:
|
||||||
InvokeWithModifiedEnv(extra_env,
|
output = InvokeWithModifiedEnv(extra_env,
|
||||||
RunAndReturnOutput,
|
RunAndReturnOutput,
|
||||||
['--gtest_list_tests'])
|
[LIST_TESTS_FLAG])
|
||||||
finally:
|
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))
|
self.assert_(os.path.exists(shard_status_file))
|
||||||
os.remove(shard_status_file)
|
os.remove(shard_status_file)
|
||||||
|
|
||||||
def testShardingWorksWithDeathTests(self):
|
if SUPPORTS_DEATH_TESTS:
|
||||||
"""Tests integration with death tests and sharding."""
|
def testShardingWorksWithDeathTests(self):
|
||||||
gtest_filter = 'HasDeathTest.*:SeqP/*'
|
"""Tests integration with death tests and sharding."""
|
||||||
expected_tests = [
|
|
||||||
'HasDeathTest.Test1',
|
|
||||||
'HasDeathTest.Test2',
|
|
||||||
|
|
||||||
'SeqP/ParamTest.TestX/0',
|
gtest_filter = 'HasDeathTest.*:SeqP/*'
|
||||||
'SeqP/ParamTest.TestX/1',
|
expected_tests = [
|
||||||
'SeqP/ParamTest.TestY/0',
|
'HasDeathTest.Test1',
|
||||||
'SeqP/ParamTest.TestY/1',
|
'HasDeathTest.Test2',
|
||||||
]
|
|
||||||
|
|
||||||
for flag in ['--gtest_death_test_style=threadsafe',
|
'SeqP/ParamTest.TestX/0',
|
||||||
'--gtest_death_test_style=fast']:
|
'SeqP/ParamTest.TestX/1',
|
||||||
self.RunAndVerifyWithSharding(gtest_filter, 3, expected_tests,
|
'SeqP/ParamTest.TestY/0',
|
||||||
check_exit_0=True, args=[flag])
|
'SeqP/ParamTest.TestY/1',
|
||||||
self.RunAndVerifyWithSharding(gtest_filter, 5, expected_tests,
|
]
|
||||||
check_exit_0=True, args=[flag])
|
|
||||||
|
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__':
|
if __name__ == '__main__':
|
||||||
gtest_test_utils.Main()
|
gtest_test_utils.Main()
|
||||||
|
|
|
@ -51,11 +51,15 @@ FLAG_PREFIX = '--gtest_'
|
||||||
CATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions'
|
CATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions'
|
||||||
DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style'
|
DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style'
|
||||||
UNKNOWN_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing'
|
UNKNOWN_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing'
|
||||||
INCORRECT_FLAG_VARIANTS = [re.sub('^--', '-', DEATH_TEST_STYLE_FLAG),
|
LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests'
|
||||||
re.sub('^--', '/', DEATH_TEST_STYLE_FLAG),
|
INCORRECT_FLAG_VARIANTS = [re.sub('^--', '-', LIST_TESTS_FLAG),
|
||||||
re.sub('_', '-', DEATH_TEST_STYLE_FLAG)]
|
re.sub('^--', '/', LIST_TESTS_FLAG),
|
||||||
|
re.sub('_', '-', LIST_TESTS_FLAG)]
|
||||||
INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing'
|
INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing'
|
||||||
|
|
||||||
|
SUPPORTS_DEATH_TESTS = "DeathTest" in gtest_test_utils.Subprocess(
|
||||||
|
[PROGRAM_PATH, LIST_TESTS_FLAG]).output
|
||||||
|
|
||||||
# The help message must match this regex.
|
# The help message must match this regex.
|
||||||
HELP_REGEX = re.compile(
|
HELP_REGEX = re.compile(
|
||||||
FLAG_PREFIX + r'list_tests.*' +
|
FLAG_PREFIX + r'list_tests.*' +
|
||||||
|
@ -107,10 +111,13 @@ class GTestHelpTest(gtest_test_utils.TestCase):
|
||||||
self.assert_(HELP_REGEX.search(output), output)
|
self.assert_(HELP_REGEX.search(output), output)
|
||||||
if IS_WINDOWS:
|
if IS_WINDOWS:
|
||||||
self.assert_(CATCH_EXCEPTIONS_FLAG in output, output)
|
self.assert_(CATCH_EXCEPTIONS_FLAG in output, output)
|
||||||
self.assert_(DEATH_TEST_STYLE_FLAG not in output, output)
|
|
||||||
else:
|
else:
|
||||||
self.assert_(CATCH_EXCEPTIONS_FLAG not in output, output)
|
self.assert_(CATCH_EXCEPTIONS_FLAG not in output, output)
|
||||||
|
|
||||||
|
if SUPPORTS_DEATH_TESTS and not IS_WINDOWS:
|
||||||
self.assert_(DEATH_TEST_STYLE_FLAG in output, output)
|
self.assert_(DEATH_TEST_STYLE_FLAG in output, output)
|
||||||
|
else:
|
||||||
|
self.assert_(DEATH_TEST_STYLE_FLAG not in output, output)
|
||||||
|
|
||||||
def TestNonHelpFlag(self, flag):
|
def TestNonHelpFlag(self, flag):
|
||||||
"""Verifies correct behavior when no help flag is specified.
|
"""Verifies correct behavior when no help flag is specified.
|
||||||
|
|
|
@ -40,3 +40,7 @@
|
||||||
TEST(HelpFlagTest, ShouldNotBeRun) {
|
TEST(HelpFlagTest, ShouldNotBeRun) {
|
||||||
ASSERT_TRUE(false) << "Tests shouldn't be run when --help is specified.";
|
ASSERT_TRUE(false) << "Tests shouldn't be run when --help is specified.";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if GTEST_HAS_DEATH_TEST
|
||||||
|
TEST(DeathTest, UsedByPythonScriptToDetectSupportForDeathTestsInThisBinary) {}
|
||||||
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user