1#!/usr/bin/env python 2# 3# Copyright 2005 Google Inc. 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 Google Test test filters. 32 33A user can specify which test(s) in a Google Test program to run via either 34the GTEST_FILTER environment variable or the --gtest_filter flag. 35This script tests such functionality by invoking 36googletest-filter-unittest_ (a program written with Google Test) with different 37environments and command line flags. 38 39Note that test sharding may also influence which tests are filtered. Therefore, 40we test that here also. 41""" 42 43import os 44import re 45try: 46 from sets import Set as set # For Python 2.3 compatibility 47except ImportError: 48 pass 49import sys 50from googletest.test import gtest_test_utils 51 52# Constants. 53 54# Checks if this platform can pass empty environment variables to child 55# processes. We set an env variable to an empty string and invoke a python 56# script in a subprocess to print whether the variable is STILL in 57# os.environ. We then use 'eval' to parse the child's output so that an 58# exception is thrown if the input is anything other than 'True' nor 'False'. 59CAN_PASS_EMPTY_ENV = False 60if sys.executable: 61 os.environ['EMPTY_VAR'] = '' 62 child = gtest_test_utils.Subprocess( 63 [sys.executable, '-c', 'import os; print(\'EMPTY_VAR\' in os.environ)']) 64 CAN_PASS_EMPTY_ENV = eval(child.output) 65 66 67# Check if this platform can unset environment variables in child processes. 68# We set an env variable to a non-empty string, unset it, and invoke 69# a python script in a subprocess to print whether the variable 70# is NO LONGER in os.environ. 71# We use 'eval' to parse the child's output so that an exception 72# is thrown if the input is neither 'True' nor 'False'. 73CAN_UNSET_ENV = False 74if sys.executable: 75 os.environ['UNSET_VAR'] = 'X' 76 del os.environ['UNSET_VAR'] 77 child = gtest_test_utils.Subprocess( 78 [sys.executable, '-c', 'import os; print(\'UNSET_VAR\' not in os.environ)' 79 ]) 80 CAN_UNSET_ENV = eval(child.output) 81 82 83# Checks if we should test with an empty filter. This doesn't 84# make sense on platforms that cannot pass empty env variables (Win32) 85# and on platforms that cannot unset variables (since we cannot tell 86# the difference between "" and NULL -- Borland and Solaris < 5.10) 87CAN_TEST_EMPTY_FILTER = (CAN_PASS_EMPTY_ENV and CAN_UNSET_ENV) 88 89 90# The environment variable for specifying the test filters. 91FILTER_ENV_VAR = 'GTEST_FILTER' 92 93# The environment variables for test sharding. 94TOTAL_SHARDS_ENV_VAR = 'GTEST_TOTAL_SHARDS' 95SHARD_INDEX_ENV_VAR = 'GTEST_SHARD_INDEX' 96SHARD_STATUS_FILE_ENV_VAR = 'GTEST_SHARD_STATUS_FILE' 97 98# The command line flag for specifying the test filters. 99FILTER_FLAG = 'gtest_filter' 100 101# The command line flag for including disabled tests. 102ALSO_RUN_DISABLED_TESTS_FLAG = 'gtest_also_run_disabled_tests' 103 104# Command to run the googletest-filter-unittest_ program. 105COMMAND = gtest_test_utils.GetTestExecutablePath('googletest-filter-unittest_') 106 107# Regex for determining whether parameterized tests are enabled in the binary. 108PARAM_TEST_REGEX = re.compile(r'/ParamTest') 109 110# Regex for parsing test case names from Google Test's output. 111TEST_CASE_REGEX = re.compile(r'^\[\-+\] \d+ tests? from (\w+(/\w+)?)') 112 113# Regex for parsing test names from Google Test's output. 114TEST_REGEX = re.compile(r'^\[\s*RUN\s*\].*\.(\w+(/\w+)?)') 115 116# Regex for parsing disabled banner from Google Test's output 117DISABLED_BANNER_REGEX = re.compile(r'^\[\s*DISABLED\s*\] (.*)') 118 119# The command line flag to tell Google Test to output the list of tests it 120# will run. 121LIST_TESTS_FLAG = '--gtest_list_tests' 122 123# Indicates whether Google Test supports death tests. 124SUPPORTS_DEATH_TESTS = 'HasDeathTest' in gtest_test_utils.Subprocess( 125 [COMMAND, LIST_TESTS_FLAG]).output 126 127# Full names of all tests in googletest-filter-unittests_. 128PARAM_TESTS = [ 129 'SeqP/ParamTest.TestX/0', 130 'SeqP/ParamTest.TestX/1', 131 'SeqP/ParamTest.TestY/0', 132 'SeqP/ParamTest.TestY/1', 133 'SeqQ/ParamTest.TestX/0', 134 'SeqQ/ParamTest.TestX/1', 135 'SeqQ/ParamTest.TestY/0', 136 'SeqQ/ParamTest.TestY/1', 137 ] 138 139DISABLED_TESTS = [ 140 'BarTest.DISABLED_TestFour', 141 'BarTest.DISABLED_TestFive', 142 'BazTest.DISABLED_TestC', 143 'DISABLED_FoobarTest.Test1', 144 'DISABLED_FoobarTest.DISABLED_Test2', 145 'DISABLED_FoobarbazTest.TestA', 146 ] 147 148if SUPPORTS_DEATH_TESTS: 149 DEATH_TESTS = [ 150 'HasDeathTest.Test1', 151 'HasDeathTest.Test2', 152 ] 153else: 154 DEATH_TESTS = [] 155 156# All the non-disabled tests. 157ACTIVE_TESTS = [ 158 'FooTest.Abc', 159 'FooTest.Xyz', 160 161 'BarTest.TestOne', 162 'BarTest.TestTwo', 163 'BarTest.TestThree', 164 165 'BazTest.TestOne', 166 'BazTest.TestA', 167 'BazTest.TestB', 168 ] + DEATH_TESTS + PARAM_TESTS 169 170param_tests_present = None 171 172# Utilities. 173 174environ = os.environ.copy() 175 176 177def SetEnvVar(env_var, value): 178 """Sets the env variable to 'value'; unsets it when 'value' is None.""" 179 180 if value is not None: 181 environ[env_var] = value 182 elif env_var in environ: 183 del environ[env_var] 184 185 186def RunAndReturnOutput(args = None): 187 """Runs the test program and returns its output.""" 188 189 return gtest_test_utils.Subprocess([COMMAND] + (args or []), 190 env=environ).output 191 192 193def RunAndExtractTestList(args = None): 194 """Runs the test program and returns its exit code and a list of tests run.""" 195 196 p = gtest_test_utils.Subprocess([COMMAND] + (args or []), env=environ) 197 tests_run = [] 198 test_case = '' 199 test = '' 200 for line in p.output.split('\n'): 201 match = TEST_CASE_REGEX.match(line) 202 if match is not None: 203 test_case = match.group(1) 204 else: 205 match = TEST_REGEX.match(line) 206 if match is not None: 207 test = match.group(1) 208 tests_run.append(test_case + '.' + test) 209 return (tests_run, p.exit_code) 210 211 212def RunAndExtractDisabledBannerList(args=None): 213 """Runs the test program and returns tests that printed a disabled banner.""" 214 p = gtest_test_utils.Subprocess([COMMAND] + (args or []), env=environ) 215 banners_printed = [] 216 for line in p.output.split('\n'): 217 match = DISABLED_BANNER_REGEX.match(line) 218 if match is not None: 219 banners_printed.append(match.group(1)) 220 return banners_printed 221 222 223def InvokeWithModifiedEnv(extra_env, function, *args, **kwargs): 224 """Runs the given function and arguments in a modified environment.""" 225 try: 226 original_env = environ.copy() 227 environ.update(extra_env) 228 return function(*args, **kwargs) 229 finally: 230 environ.clear() 231 environ.update(original_env) 232 233 234def RunWithSharding(total_shards, shard_index, command): 235 """Runs a test program shard and returns exit code and a list of tests run.""" 236 237 extra_env = {SHARD_INDEX_ENV_VAR: str(shard_index), 238 TOTAL_SHARDS_ENV_VAR: str(total_shards)} 239 return InvokeWithModifiedEnv(extra_env, RunAndExtractTestList, command) 240 241# The unit test. 242 243 244class GTestFilterUnitTest(gtest_test_utils.TestCase): 245 """Tests the env variable or the command line flag to filter tests.""" 246 247 # Utilities. 248 249 def AssertSetEqual(self, lhs, rhs): 250 """Asserts that two sets are equal.""" 251 252 for elem in lhs: 253 self.assert_(elem in rhs, '%s in %s' % (elem, rhs)) 254 255 for elem in rhs: 256 self.assert_(elem in lhs, '%s in %s' % (elem, lhs)) 257 258 def AssertPartitionIsValid(self, set_var, list_of_sets): 259 """Asserts that list_of_sets is a valid partition of set_var.""" 260 261 full_partition = [] 262 for slice_var in list_of_sets: 263 full_partition.extend(slice_var) 264 self.assertEqual(len(set_var), len(full_partition)) 265 self.assertEqual(set(set_var), set(full_partition)) 266 267 def AdjustForParameterizedTests(self, tests_to_run): 268 """Adjust tests_to_run in case value parameterized tests are disabled.""" 269 270 global param_tests_present 271 if not param_tests_present: 272 return list(set(tests_to_run) - set(PARAM_TESTS)) 273 else: 274 return tests_to_run 275 276 def RunAndVerify(self, gtest_filter, tests_to_run): 277 """Checks that the binary runs correct set of tests for a given filter.""" 278 279 tests_to_run = self.AdjustForParameterizedTests(tests_to_run) 280 281 # First, tests using the environment variable. 282 283 # Windows removes empty variables from the environment when passing it 284 # to a new process. This means it is impossible to pass an empty filter 285 # into a process using the environment variable. However, we can still 286 # test the case when the variable is not supplied (i.e., gtest_filter is 287 # None). 288 # pylint: disable-msg=C6403 289 if CAN_TEST_EMPTY_FILTER or gtest_filter != '': 290 SetEnvVar(FILTER_ENV_VAR, gtest_filter) 291 tests_run = RunAndExtractTestList()[0] 292 SetEnvVar(FILTER_ENV_VAR, None) 293 self.AssertSetEqual(tests_run, tests_to_run) 294 # pylint: enable-msg=C6403 295 296 # Next, tests using the command line flag. 297 298 if gtest_filter is None: 299 args = [] 300 else: 301 args = ['--%s=%s' % (FILTER_FLAG, gtest_filter)] 302 303 tests_run = RunAndExtractTestList(args)[0] 304 self.AssertSetEqual(tests_run, tests_to_run) 305 306 def RunAndVerifyWithSharding(self, gtest_filter, total_shards, tests_to_run, 307 args=None, check_exit_0=False): 308 """Checks that binary runs correct tests for the given filter and shard. 309 310 Runs all shards of googletest-filter-unittest_ with the given filter, and 311 verifies that the right set of tests were run. The union of tests run 312 on each shard should be identical to tests_to_run, without duplicates. 313 If check_exit_0, . 314 315 Args: 316 gtest_filter: A filter to apply to the tests. 317 total_shards: A total number of shards to split test run into. 318 tests_to_run: A set of tests expected to run. 319 args : Arguments to pass to the to the test binary. 320 check_exit_0: When set to a true value, make sure that all shards 321 return 0. 322 """ 323 324 tests_to_run = self.AdjustForParameterizedTests(tests_to_run) 325 326 # Windows removes empty variables from the environment when passing it 327 # to a new process. This means it is impossible to pass an empty filter 328 # into a process using the environment variable. However, we can still 329 # test the case when the variable is not supplied (i.e., gtest_filter is 330 # None). 331 # pylint: disable-msg=C6403 332 if CAN_TEST_EMPTY_FILTER or gtest_filter != '': 333 SetEnvVar(FILTER_ENV_VAR, gtest_filter) 334 partition = [] 335 for i in range(0, total_shards): 336 (tests_run, exit_code) = RunWithSharding(total_shards, i, args) 337 if check_exit_0: 338 self.assertEqual(0, exit_code) 339 partition.append(tests_run) 340 341 self.AssertPartitionIsValid(tests_to_run, partition) 342 SetEnvVar(FILTER_ENV_VAR, None) 343 # pylint: enable-msg=C6403 344 345 def RunAndVerifyAllowingDisabled(self, gtest_filter, tests_to_run): 346 """Checks that the binary runs correct set of tests for the given filter. 347 348 Runs googletest-filter-unittest_ with the given filter, and enables 349 disabled tests. Verifies that the right set of tests were run. 350 351 Args: 352 gtest_filter: A filter to apply to the tests. 353 tests_to_run: A set of tests expected to run. 354 """ 355 356 tests_to_run = self.AdjustForParameterizedTests(tests_to_run) 357 358 # Construct the command line. 359 args = ['--%s' % ALSO_RUN_DISABLED_TESTS_FLAG] 360 if gtest_filter is not None: 361 args.append('--%s=%s' % (FILTER_FLAG, gtest_filter)) 362 363 tests_run = RunAndExtractTestList(args)[0] 364 self.AssertSetEqual(tests_run, tests_to_run) 365 366 def setUp(self): 367 """Sets up test case. 368 369 Determines whether value-parameterized tests are enabled in the binary and 370 sets the flags accordingly. 371 """ 372 373 global param_tests_present 374 if param_tests_present is None: 375 param_tests_present = PARAM_TEST_REGEX.search( 376 RunAndReturnOutput()) is not None 377 378 def testDefaultBehavior(self): 379 """Tests the behavior of not specifying the filter.""" 380 381 self.RunAndVerify(None, ACTIVE_TESTS) 382 383 def testDefaultBehaviorWithShards(self): 384 """Tests the behavior without the filter, with sharding enabled.""" 385 386 self.RunAndVerifyWithSharding(None, 1, ACTIVE_TESTS) 387 self.RunAndVerifyWithSharding(None, 2, ACTIVE_TESTS) 388 self.RunAndVerifyWithSharding(None, len(ACTIVE_TESTS) - 1, ACTIVE_TESTS) 389 self.RunAndVerifyWithSharding(None, len(ACTIVE_TESTS), ACTIVE_TESTS) 390 self.RunAndVerifyWithSharding(None, len(ACTIVE_TESTS) + 1, ACTIVE_TESTS) 391 392 def testEmptyFilter(self): 393 """Tests an empty filter.""" 394 395 self.RunAndVerify('', []) 396 self.RunAndVerifyWithSharding('', 1, []) 397 self.RunAndVerifyWithSharding('', 2, []) 398 399 def testBadFilter(self): 400 """Tests a filter that matches nothing.""" 401 402 self.RunAndVerify('BadFilter', []) 403 self.RunAndVerifyAllowingDisabled('BadFilter', []) 404 405 def testFullName(self): 406 """Tests filtering by full name.""" 407 408 self.RunAndVerify('FooTest.Xyz', ['FooTest.Xyz']) 409 self.RunAndVerifyAllowingDisabled('FooTest.Xyz', ['FooTest.Xyz']) 410 self.RunAndVerifyWithSharding('FooTest.Xyz', 5, ['FooTest.Xyz']) 411 412 def testUniversalFilters(self): 413 """Tests filters that match everything.""" 414 415 self.RunAndVerify('*', ACTIVE_TESTS) 416 self.RunAndVerify('*.*', ACTIVE_TESTS) 417 self.RunAndVerifyWithSharding('*.*', len(ACTIVE_TESTS) - 3, ACTIVE_TESTS) 418 self.RunAndVerifyAllowingDisabled('*', ACTIVE_TESTS + DISABLED_TESTS) 419 self.RunAndVerifyAllowingDisabled('*.*', ACTIVE_TESTS + DISABLED_TESTS) 420 421 def testFilterByTestCase(self): 422 """Tests filtering by test case name.""" 423 424 self.RunAndVerify('FooTest.*', ['FooTest.Abc', 'FooTest.Xyz']) 425 426 BAZ_TESTS = ['BazTest.TestOne', 'BazTest.TestA', 'BazTest.TestB'] 427 self.RunAndVerify('BazTest.*', BAZ_TESTS) 428 self.RunAndVerifyAllowingDisabled('BazTest.*', 429 BAZ_TESTS + ['BazTest.DISABLED_TestC']) 430 431 def testFilterByTest(self): 432 """Tests filtering by test name.""" 433 434 self.RunAndVerify('*.TestOne', ['BarTest.TestOne', 'BazTest.TestOne']) 435 436 def testFilterDisabledTests(self): 437 """Select only the disabled tests to run.""" 438 439 self.RunAndVerify('DISABLED_FoobarTest.Test1', []) 440 self.RunAndVerifyAllowingDisabled('DISABLED_FoobarTest.Test1', 441 ['DISABLED_FoobarTest.Test1']) 442 443 self.RunAndVerify('*DISABLED_*', []) 444 self.RunAndVerifyAllowingDisabled('*DISABLED_*', DISABLED_TESTS) 445 446 self.RunAndVerify('*.DISABLED_*', []) 447 self.RunAndVerifyAllowingDisabled('*.DISABLED_*', [ 448 'BarTest.DISABLED_TestFour', 449 'BarTest.DISABLED_TestFive', 450 'BazTest.DISABLED_TestC', 451 'DISABLED_FoobarTest.DISABLED_Test2', 452 ]) 453 454 self.RunAndVerify('DISABLED_*', []) 455 self.RunAndVerifyAllowingDisabled('DISABLED_*', [ 456 'DISABLED_FoobarTest.Test1', 457 'DISABLED_FoobarTest.DISABLED_Test2', 458 'DISABLED_FoobarbazTest.TestA', 459 ]) 460 461 def testWildcardInTestCaseName(self): 462 """Tests using wildcard in the test case name.""" 463 464 self.RunAndVerify('*a*.*', [ 465 'BarTest.TestOne', 466 'BarTest.TestTwo', 467 'BarTest.TestThree', 468 469 'BazTest.TestOne', 470 'BazTest.TestA', 471 'BazTest.TestB', ] + DEATH_TESTS + PARAM_TESTS) 472 473 def testWildcardInTestName(self): 474 """Tests using wildcard in the test name.""" 475 476 self.RunAndVerify('*.*A*', ['FooTest.Abc', 'BazTest.TestA']) 477 478 def testFilterWithoutDot(self): 479 """Tests a filter that has no '.' in it.""" 480 481 self.RunAndVerify('*z*', [ 482 'FooTest.Xyz', 483 484 'BazTest.TestOne', 485 'BazTest.TestA', 486 'BazTest.TestB', 487 ]) 488 489 def testTwoPatterns(self): 490 """Tests filters that consist of two patterns.""" 491 492 self.RunAndVerify('Foo*.*:*A*', [ 493 'FooTest.Abc', 494 'FooTest.Xyz', 495 496 'BazTest.TestA', 497 ]) 498 499 # An empty pattern + a non-empty one 500 self.RunAndVerify(':*A*', ['FooTest.Abc', 'BazTest.TestA']) 501 502 def testThreePatterns(self): 503 """Tests filters that consist of three patterns.""" 504 505 self.RunAndVerify('*oo*:*A*:*One', [ 506 'FooTest.Abc', 507 'FooTest.Xyz', 508 509 'BarTest.TestOne', 510 511 'BazTest.TestOne', 512 'BazTest.TestA', 513 ]) 514 515 # The 2nd pattern is empty. 516 self.RunAndVerify('*oo*::*One', [ 517 'FooTest.Abc', 518 'FooTest.Xyz', 519 520 'BarTest.TestOne', 521 522 'BazTest.TestOne', 523 ]) 524 525 # The last 2 patterns are empty. 526 self.RunAndVerify('*oo*::', [ 527 'FooTest.Abc', 528 'FooTest.Xyz', 529 ]) 530 531 def testNegativeFilters(self): 532 self.RunAndVerify('*-BazTest.TestOne', [ 533 'FooTest.Abc', 534 'FooTest.Xyz', 535 536 'BarTest.TestOne', 537 'BarTest.TestTwo', 538 'BarTest.TestThree', 539 540 'BazTest.TestA', 541 'BazTest.TestB', 542 ] + DEATH_TESTS + PARAM_TESTS) 543 544 self.RunAndVerify('*-FooTest.Abc:BazTest.*', [ 545 'FooTest.Xyz', 546 547 'BarTest.TestOne', 548 'BarTest.TestTwo', 549 'BarTest.TestThree', 550 ] + DEATH_TESTS + PARAM_TESTS) 551 552 self.RunAndVerify('BarTest.*-BarTest.TestOne', [ 553 'BarTest.TestTwo', 554 'BarTest.TestThree', 555 ]) 556 557 # Tests without leading '*'. 558 self.RunAndVerify('-FooTest.Abc:FooTest.Xyz:BazTest.*', [ 559 'BarTest.TestOne', 560 'BarTest.TestTwo', 561 'BarTest.TestThree', 562 ] + DEATH_TESTS + PARAM_TESTS) 563 564 # Value parameterized tests. 565 self.RunAndVerify('*/*', PARAM_TESTS) 566 567 # Value parameterized tests filtering by the sequence name. 568 self.RunAndVerify('SeqP/*', [ 569 'SeqP/ParamTest.TestX/0', 570 'SeqP/ParamTest.TestX/1', 571 'SeqP/ParamTest.TestY/0', 572 'SeqP/ParamTest.TestY/1', 573 ]) 574 575 # Value parameterized tests filtering by the test name. 576 self.RunAndVerify('*/0', [ 577 'SeqP/ParamTest.TestX/0', 578 'SeqP/ParamTest.TestY/0', 579 'SeqQ/ParamTest.TestX/0', 580 'SeqQ/ParamTest.TestY/0', 581 ]) 582 583 def testFlagOverridesEnvVar(self): 584 """Tests that the filter flag overrides the filtering env. variable.""" 585 586 SetEnvVar(FILTER_ENV_VAR, 'Foo*') 587 args = ['--%s=%s' % (FILTER_FLAG, '*One')] 588 tests_run = RunAndExtractTestList(args)[0] 589 SetEnvVar(FILTER_ENV_VAR, None) 590 591 self.AssertSetEqual(tests_run, ['BarTest.TestOne', 'BazTest.TestOne']) 592 593 def testShardStatusFileIsCreated(self): 594 """Tests that the shard file is created if specified in the environment.""" 595 596 shard_status_file = os.path.join(gtest_test_utils.GetTempDir(), 597 'shard_status_file') 598 self.assert_(not os.path.exists(shard_status_file)) 599 600 extra_env = {SHARD_STATUS_FILE_ENV_VAR: shard_status_file} 601 try: 602 InvokeWithModifiedEnv(extra_env, RunAndReturnOutput) 603 finally: 604 self.assert_(os.path.exists(shard_status_file)) 605 os.remove(shard_status_file) 606 607 def testShardStatusFileIsCreatedWithListTests(self): 608 """Tests that the shard file is created with the "list_tests" flag.""" 609 610 shard_status_file = os.path.join(gtest_test_utils.GetTempDir(), 611 'shard_status_file2') 612 self.assert_(not os.path.exists(shard_status_file)) 613 614 extra_env = {SHARD_STATUS_FILE_ENV_VAR: shard_status_file} 615 try: 616 output = InvokeWithModifiedEnv(extra_env, 617 RunAndReturnOutput, 618 [LIST_TESTS_FLAG]) 619 finally: 620 # This assertion ensures that Google Test enumerated the tests as 621 # opposed to running them. 622 self.assert_('[==========]' not in output, 623 'Unexpected output during test enumeration.\n' 624 'Please ensure that LIST_TESTS_FLAG is assigned the\n' 625 'correct flag value for listing Google Test tests.') 626 627 self.assert_(os.path.exists(shard_status_file)) 628 os.remove(shard_status_file) 629 630 def testDisabledBanner(self): 631 """Tests that the disabled banner prints only tests that match filter.""" 632 make_filter = lambda s: ['--%s=%s' % (FILTER_FLAG, s)] 633 634 banners = RunAndExtractDisabledBannerList(make_filter('*')) 635 self.AssertSetEqual(banners, [ 636 'BarTest.DISABLED_TestFour', 'BarTest.DISABLED_TestFive', 637 'BazTest.DISABLED_TestC' 638 ]) 639 640 banners = RunAndExtractDisabledBannerList(make_filter('Bar*')) 641 self.AssertSetEqual( 642 banners, ['BarTest.DISABLED_TestFour', 'BarTest.DISABLED_TestFive']) 643 644 banners = RunAndExtractDisabledBannerList(make_filter('*-Bar*')) 645 self.AssertSetEqual(banners, ['BazTest.DISABLED_TestC']) 646 647 if SUPPORTS_DEATH_TESTS: 648 def testShardingWorksWithDeathTests(self): 649 """Tests integration with death tests and sharding.""" 650 651 gtest_filter = 'HasDeathTest.*:SeqP/*' 652 expected_tests = [ 653 'HasDeathTest.Test1', 654 'HasDeathTest.Test2', 655 656 'SeqP/ParamTest.TestX/0', 657 'SeqP/ParamTest.TestX/1', 658 'SeqP/ParamTest.TestY/0', 659 'SeqP/ParamTest.TestY/1', 660 ] 661 662 for flag in ['--gtest_death_test_style=threadsafe', 663 '--gtest_death_test_style=fast']: 664 self.RunAndVerifyWithSharding(gtest_filter, 3, expected_tests, 665 check_exit_0=True, args=[flag]) 666 self.RunAndVerifyWithSharding(gtest_filter, 5, expected_tests, 667 check_exit_0=True, args=[flag]) 668 669if __name__ == '__main__': 670 gtest_test_utils.Main() 671