tesseract v5.3.3.20231005
googletest-filter-unittest.GTestFilterUnitTest Class Reference
Inheritance diagram for googletest-filter-unittest.GTestFilterUnitTest:

Public Member Functions

def AssertSetEqual (self, lhs, rhs)
 
def AssertPartitionIsValid (self, set_var, list_of_sets)
 
def AdjustForParameterizedTests (self, tests_to_run)
 
def RunAndVerify (self, gtest_filter, tests_to_run)
 
def RunAndVerifyWithSharding (self, gtest_filter, total_shards, tests_to_run, args=None, check_exit_0=False)
 
def RunAndVerifyAllowingDisabled (self, gtest_filter, tests_to_run)
 
def setUp (self)
 
def testDefaultBehavior (self)
 
def testDefaultBehaviorWithShards (self)
 
def testEmptyFilter (self)
 
def testBadFilter (self)
 
def testFullName (self)
 
def testUniversalFilters (self)
 
def testFilterByTestCase (self)
 
def testFilterByTest (self)
 
def testFilterDisabledTests (self)
 
def testWildcardInTestCaseName (self)
 
def testWildcardInTestName (self)
 
def testFilterWithoutDot (self)
 
def testTwoPatterns (self)
 
def testThreePatterns (self)
 
def testNegativeFilters (self)
 
def testFlagOverridesEnvVar (self)
 
def testShardStatusFileIsCreated (self)
 
def testShardStatusFileIsCreatedWithListTests (self)
 
def testShardingWorksWithDeathTests (self)
 

Detailed Description

Tests the env variable or the command line flag to filter tests.

Definition at line 230 of file googletest-filter-unittest.py.

Member Function Documentation

◆ AdjustForParameterizedTests()

def googletest-filter-unittest.GTestFilterUnitTest.AdjustForParameterizedTests (   self,
  tests_to_run 
)
Adjust tests_to_run in case value parameterized tests are disabled.

Definition at line 253 of file googletest-filter-unittest.py.

253 def AdjustForParameterizedTests(self, tests_to_run):
254 """Adjust tests_to_run in case value parameterized tests are disabled."""
255
256 global param_tests_present
257 if not param_tests_present:
258 return list(set(tests_to_run) - set(PARAM_TESTS))
259 else:
260 return tests_to_run
261

◆ AssertPartitionIsValid()

def googletest-filter-unittest.GTestFilterUnitTest.AssertPartitionIsValid (   self,
  set_var,
  list_of_sets 
)
Asserts that list_of_sets is a valid partition of set_var.

Definition at line 244 of file googletest-filter-unittest.py.

244 def AssertPartitionIsValid(self, set_var, list_of_sets):
245 """Asserts that list_of_sets is a valid partition of set_var."""
246
247 full_partition = []
248 for slice_var in list_of_sets:
249 full_partition.extend(slice_var)
250 self.assertEqual(len(set_var), len(full_partition))
251 self.assertEqual(set(set_var), set(full_partition))
252

◆ AssertSetEqual()

def googletest-filter-unittest.GTestFilterUnitTest.AssertSetEqual (   self,
  lhs,
  rhs 
)
Asserts that two sets are equal.

Definition at line 235 of file googletest-filter-unittest.py.

235 def AssertSetEqual(self, lhs, rhs):
236 """Asserts that two sets are equal."""
237
238 for elem in lhs:
239 self.assert_(elem in rhs, '%s in %s' % (elem, rhs))
240
241 for elem in rhs:
242 self.assert_(elem in lhs, '%s in %s' % (elem, lhs))
243

◆ RunAndVerify()

def googletest-filter-unittest.GTestFilterUnitTest.RunAndVerify (   self,
  gtest_filter,
  tests_to_run 
)
Checks that the binary runs correct set of tests for a given filter.

Definition at line 262 of file googletest-filter-unittest.py.

262 def RunAndVerify(self, gtest_filter, tests_to_run):
263 """Checks that the binary runs correct set of tests for a given filter."""
264
265 tests_to_run = self.AdjustForParameterizedTests(tests_to_run)
266
267 # First, tests using the environment variable.
268
269 # Windows removes empty variables from the environment when passing it
270 # to a new process. This means it is impossible to pass an empty filter
271 # into a process using the environment variable. However, we can still
272 # test the case when the variable is not supplied (i.e., gtest_filter is
273 # None).
274 # pylint: disable-msg=C6403
275 if CAN_TEST_EMPTY_FILTER or gtest_filter != '':
276 SetEnvVar(FILTER_ENV_VAR, gtest_filter)
277 tests_run = RunAndExtractTestList()[0]
278 SetEnvVar(FILTER_ENV_VAR, None)
279 self.AssertSetEqual(tests_run, tests_to_run)
280 # pylint: enable-msg=C6403
281
282 # Next, tests using the command line flag.
283
284 if gtest_filter is None:
285 args = []
286 else:
287 args = ['--%s=%s' % (FILTER_FLAG, gtest_filter)]
288
289 tests_run = RunAndExtractTestList(args)[0]
290 self.AssertSetEqual(tests_run, tests_to_run)
291

◆ RunAndVerifyAllowingDisabled()

def googletest-filter-unittest.GTestFilterUnitTest.RunAndVerifyAllowingDisabled (   self,
  gtest_filter,
  tests_to_run 
)
Checks that the binary runs correct set of tests for the given filter.

Runs googletest-filter-unittest_ with the given filter, and enables
disabled tests. Verifies that the right set of tests were run.

Args:
  gtest_filter: A filter to apply to the tests.
  tests_to_run: A set of tests expected to run.

Definition at line 331 of file googletest-filter-unittest.py.

331 def RunAndVerifyAllowingDisabled(self, gtest_filter, tests_to_run):
332 """Checks that the binary runs correct set of tests for the given filter.
333
334 Runs googletest-filter-unittest_ with the given filter, and enables
335 disabled tests. Verifies that the right set of tests were run.
336
337 Args:
338 gtest_filter: A filter to apply to the tests.
339 tests_to_run: A set of tests expected to run.
340 """
341
342 tests_to_run = self.AdjustForParameterizedTests(tests_to_run)
343
344 # Construct the command line.
345 args = ['--%s' % ALSO_RUN_DISABLED_TESTS_FLAG]
346 if gtest_filter is not None:
347 args.append('--%s=%s' % (FILTER_FLAG, gtest_filter))
348
349 tests_run = RunAndExtractTestList(args)[0]
350 self.AssertSetEqual(tests_run, tests_to_run)
351

◆ RunAndVerifyWithSharding()

def googletest-filter-unittest.GTestFilterUnitTest.RunAndVerifyWithSharding (   self,
  gtest_filter,
  total_shards,
  tests_to_run,
  args = None,
  check_exit_0 = False 
)
Checks that binary runs correct tests for the given filter and shard.

Runs all shards of googletest-filter-unittest_ with the given filter, and
verifies that the right set of tests were run. The union of tests run
on each shard should be identical to tests_to_run, without duplicates.
If check_exit_0, .

Args:
  gtest_filter: A filter to apply to the tests.
  total_shards: A total number of shards to split test run into.
  tests_to_run: A set of tests expected to run.
  args   :      Arguments to pass to the to the test binary.
  check_exit_0: When set to a true value, make sure that all shards
                return 0.

Definition at line 292 of file googletest-filter-unittest.py.

293 args=None, check_exit_0=False):
294 """Checks that binary runs correct tests for the given filter and shard.
295
296 Runs all shards of googletest-filter-unittest_ with the given filter, and
297 verifies that the right set of tests were run. The union of tests run
298 on each shard should be identical to tests_to_run, without duplicates.
299 If check_exit_0, .
300
301 Args:
302 gtest_filter: A filter to apply to the tests.
303 total_shards: A total number of shards to split test run into.
304 tests_to_run: A set of tests expected to run.
305 args : Arguments to pass to the to the test binary.
306 check_exit_0: When set to a true value, make sure that all shards
307 return 0.
308 """
309
310 tests_to_run = self.AdjustForParameterizedTests(tests_to_run)
311
312 # Windows removes empty variables from the environment when passing it
313 # to a new process. This means it is impossible to pass an empty filter
314 # into a process using the environment variable. However, we can still
315 # test the case when the variable is not supplied (i.e., gtest_filter is
316 # None).
317 # pylint: disable-msg=C6403
318 if CAN_TEST_EMPTY_FILTER or gtest_filter != '':
319 SetEnvVar(FILTER_ENV_VAR, gtest_filter)
320 partition = []
321 for i in range(0, total_shards):
322 (tests_run, exit_code) = RunWithSharding(total_shards, i, args)
323 if check_exit_0:
324 self.assertEqual(0, exit_code)
325 partition.append(tests_run)
326
327 self.AssertPartitionIsValid(tests_to_run, partition)
328 SetEnvVar(FILTER_ENV_VAR, None)
329 # pylint: enable-msg=C6403
330
def RunWithSharding(total_shards, shard_index, command)

◆ setUp()

def googletest-filter-unittest.GTestFilterUnitTest.setUp (   self)
Sets up test case.

Determines whether value-parameterized tests are enabled in the binary and
sets the flags accordingly.

Definition at line 352 of file googletest-filter-unittest.py.

352 def setUp(self):
353 """Sets up test case.
354
355 Determines whether value-parameterized tests are enabled in the binary and
356 sets the flags accordingly.
357 """
358
359 global param_tests_present
360 if param_tests_present is None:
361 param_tests_present = PARAM_TEST_REGEX.search(
362 RunAndReturnOutput()) is not None
363

◆ testBadFilter()

def googletest-filter-unittest.GTestFilterUnitTest.testBadFilter (   self)
Tests a filter that matches nothing.

Definition at line 385 of file googletest-filter-unittest.py.

385 def testBadFilter(self):
386 """Tests a filter that matches nothing."""
387
388 self.RunAndVerify('BadFilter', [])
389 self.RunAndVerifyAllowingDisabled('BadFilter', [])
390

◆ testDefaultBehavior()

def googletest-filter-unittest.GTestFilterUnitTest.testDefaultBehavior (   self)
Tests the behavior of not specifying the filter.

Definition at line 364 of file googletest-filter-unittest.py.

364 def testDefaultBehavior(self):
365 """Tests the behavior of not specifying the filter."""
366
367 self.RunAndVerify(None, ACTIVE_TESTS)
368

◆ testDefaultBehaviorWithShards()

def googletest-filter-unittest.GTestFilterUnitTest.testDefaultBehaviorWithShards (   self)
Tests the behavior without the filter, with sharding enabled.

Definition at line 369 of file googletest-filter-unittest.py.

369 def testDefaultBehaviorWithShards(self):
370 """Tests the behavior without the filter, with sharding enabled."""
371
372 self.RunAndVerifyWithSharding(None, 1, ACTIVE_TESTS)
373 self.RunAndVerifyWithSharding(None, 2, ACTIVE_TESTS)
374 self.RunAndVerifyWithSharding(None, len(ACTIVE_TESTS) - 1, ACTIVE_TESTS)
375 self.RunAndVerifyWithSharding(None, len(ACTIVE_TESTS), ACTIVE_TESTS)
376 self.RunAndVerifyWithSharding(None, len(ACTIVE_TESTS) + 1, ACTIVE_TESTS)
377

◆ testEmptyFilter()

def googletest-filter-unittest.GTestFilterUnitTest.testEmptyFilter (   self)
Tests an empty filter.

Definition at line 378 of file googletest-filter-unittest.py.

378 def testEmptyFilter(self):
379 """Tests an empty filter."""
380
381 self.RunAndVerify('', [])
382 self.RunAndVerifyWithSharding('', 1, [])
383 self.RunAndVerifyWithSharding('', 2, [])
384

◆ testFilterByTest()

def googletest-filter-unittest.GTestFilterUnitTest.testFilterByTest (   self)
Tests filtering by test name.

Definition at line 417 of file googletest-filter-unittest.py.

417 def testFilterByTest(self):
418 """Tests filtering by test name."""
419
420 self.RunAndVerify('*.TestOne', ['BarTest.TestOne', 'BazTest.TestOne'])
421

◆ testFilterByTestCase()

def googletest-filter-unittest.GTestFilterUnitTest.testFilterByTestCase (   self)
Tests filtering by test case name.

Definition at line 407 of file googletest-filter-unittest.py.

407 def testFilterByTestCase(self):
408 """Tests filtering by test case name."""
409
410 self.RunAndVerify('FooTest.*', ['FooTest.Abc', 'FooTest.Xyz'])
411
412 BAZ_TESTS = ['BazTest.TestOne', 'BazTest.TestA', 'BazTest.TestB']
413 self.RunAndVerify('BazTest.*', BAZ_TESTS)
414 self.RunAndVerifyAllowingDisabled('BazTest.*',
415 BAZ_TESTS + ['BazTest.DISABLED_TestC'])
416

◆ testFilterDisabledTests()

def googletest-filter-unittest.GTestFilterUnitTest.testFilterDisabledTests (   self)
Select only the disabled tests to run.

Definition at line 422 of file googletest-filter-unittest.py.

422 def testFilterDisabledTests(self):
423 """Select only the disabled tests to run."""
424
425 self.RunAndVerify('DISABLED_FoobarTest.Test1', [])
426 self.RunAndVerifyAllowingDisabled('DISABLED_FoobarTest.Test1',
427 ['DISABLED_FoobarTest.Test1'])
428
429 self.RunAndVerify('*DISABLED_*', [])
430 self.RunAndVerifyAllowingDisabled('*DISABLED_*', DISABLED_TESTS)
431
432 self.RunAndVerify('*.DISABLED_*', [])
433 self.RunAndVerifyAllowingDisabled('*.DISABLED_*', [
434 'BarTest.DISABLED_TestFour',
435 'BarTest.DISABLED_TestFive',
436 'BazTest.DISABLED_TestC',
437 'DISABLED_FoobarTest.DISABLED_Test2',
438 ])
439
440 self.RunAndVerify('DISABLED_*', [])
441 self.RunAndVerifyAllowingDisabled('DISABLED_*', [
442 'DISABLED_FoobarTest.Test1',
443 'DISABLED_FoobarTest.DISABLED_Test2',
444 'DISABLED_FoobarbazTest.TestA',
445 ])
446

◆ testFilterWithoutDot()

def googletest-filter-unittest.GTestFilterUnitTest.testFilterWithoutDot (   self)
Tests a filter that has no '.' in it.

Definition at line 464 of file googletest-filter-unittest.py.

464 def testFilterWithoutDot(self):
465 """Tests a filter that has no '.' in it."""
466
467 self.RunAndVerify('*z*', [
468 'FooTest.Xyz',
469
470 'BazTest.TestOne',
471 'BazTest.TestA',
472 'BazTest.TestB',
473 ])
474

◆ testFlagOverridesEnvVar()

def googletest-filter-unittest.GTestFilterUnitTest.testFlagOverridesEnvVar (   self)
Tests that the filter flag overrides the filtering env. variable.

Definition at line 569 of file googletest-filter-unittest.py.

569 def testFlagOverridesEnvVar(self):
570 """Tests that the filter flag overrides the filtering env. variable."""
571
572 SetEnvVar(FILTER_ENV_VAR, 'Foo*')
573 args = ['--%s=%s' % (FILTER_FLAG, '*One')]
574 tests_run = RunAndExtractTestList(args)[0]
575 SetEnvVar(FILTER_ENV_VAR, None)
576
577 self.AssertSetEqual(tests_run, ['BarTest.TestOne', 'BazTest.TestOne'])
578

◆ testFullName()

def googletest-filter-unittest.GTestFilterUnitTest.testFullName (   self)
Tests filtering by full name.

Definition at line 391 of file googletest-filter-unittest.py.

391 def testFullName(self):
392 """Tests filtering by full name."""
393
394 self.RunAndVerify('FooTest.Xyz', ['FooTest.Xyz'])
395 self.RunAndVerifyAllowingDisabled('FooTest.Xyz', ['FooTest.Xyz'])
396 self.RunAndVerifyWithSharding('FooTest.Xyz', 5, ['FooTest.Xyz'])
397

◆ testNegativeFilters()

def googletest-filter-unittest.GTestFilterUnitTest.testNegativeFilters (   self)

Definition at line 517 of file googletest-filter-unittest.py.

517 def testNegativeFilters(self):
518 self.RunAndVerify('*-BazTest.TestOne', [
519 'FooTest.Abc',
520 'FooTest.Xyz',
521
522 'BarTest.TestOne',
523 'BarTest.TestTwo',
524 'BarTest.TestThree',
525
526 'BazTest.TestA',
527 'BazTest.TestB',
528 ] + DEATH_TESTS + PARAM_TESTS)
529
530 self.RunAndVerify('*-FooTest.Abc:BazTest.*', [
531 'FooTest.Xyz',
532
533 'BarTest.TestOne',
534 'BarTest.TestTwo',
535 'BarTest.TestThree',
536 ] + DEATH_TESTS + PARAM_TESTS)
537
538 self.RunAndVerify('BarTest.*-BarTest.TestOne', [
539 'BarTest.TestTwo',
540 'BarTest.TestThree',
541 ])
542
543 # Tests without leading '*'.
544 self.RunAndVerify('-FooTest.Abc:FooTest.Xyz:BazTest.*', [
545 'BarTest.TestOne',
546 'BarTest.TestTwo',
547 'BarTest.TestThree',
548 ] + DEATH_TESTS + PARAM_TESTS)
549
550 # Value parameterized tests.
551 self.RunAndVerify('*/*', PARAM_TESTS)
552
553 # Value parameterized tests filtering by the sequence name.
554 self.RunAndVerify('SeqP/*', [
555 'SeqP/ParamTest.TestX/0',
556 'SeqP/ParamTest.TestX/1',
557 'SeqP/ParamTest.TestY/0',
558 'SeqP/ParamTest.TestY/1',
559 ])
560
561 # Value parameterized tests filtering by the test name.
562 self.RunAndVerify('*/0', [
563 'SeqP/ParamTest.TestX/0',
564 'SeqP/ParamTest.TestY/0',
565 'SeqQ/ParamTest.TestX/0',
566 'SeqQ/ParamTest.TestY/0',
567 ])
568

◆ testShardingWorksWithDeathTests()

def googletest-filter-unittest.GTestFilterUnitTest.testShardingWorksWithDeathTests (   self)
Tests integration with death tests and sharding.

Definition at line 617 of file googletest-filter-unittest.py.

617 def testShardingWorksWithDeathTests(self):
618 """Tests integration with death tests and sharding."""
619
620 gtest_filter = 'HasDeathTest.*:SeqP/*'
621 expected_tests = [
622 'HasDeathTest.Test1',
623 'HasDeathTest.Test2',
624
625 'SeqP/ParamTest.TestX/0',
626 'SeqP/ParamTest.TestX/1',
627 'SeqP/ParamTest.TestY/0',
628 'SeqP/ParamTest.TestY/1',
629 ]
630
631 for flag in ['--gtest_death_test_style=threadsafe',
632 '--gtest_death_test_style=fast']:
633 self.RunAndVerifyWithSharding(gtest_filter, 3, expected_tests,
634 check_exit_0=True, args=[flag])
635 self.RunAndVerifyWithSharding(gtest_filter, 5, expected_tests,
636 check_exit_0=True, args=[flag])
637

◆ testShardStatusFileIsCreated()

def googletest-filter-unittest.GTestFilterUnitTest.testShardStatusFileIsCreated (   self)
Tests that the shard file is created if specified in the environment.

Definition at line 579 of file googletest-filter-unittest.py.

579 def testShardStatusFileIsCreated(self):
580 """Tests that the shard file is created if specified in the environment."""
581
582 shard_status_file = os.path.join(gtest_test_utils.GetTempDir(),
583 'shard_status_file')
584 self.assert_(not os.path.exists(shard_status_file))
585
586 extra_env = {SHARD_STATUS_FILE_ENV_VAR: shard_status_file}
587 try:
588 InvokeWithModifiedEnv(extra_env, RunAndReturnOutput)
589 finally:
590 self.assert_(os.path.exists(shard_status_file))
591 os.remove(shard_status_file)
592
def InvokeWithModifiedEnv(extra_env, function, *args, **kwargs)

◆ testShardStatusFileIsCreatedWithListTests()

def googletest-filter-unittest.GTestFilterUnitTest.testShardStatusFileIsCreatedWithListTests (   self)
Tests that the shard file is created with the "list_tests" flag.

Definition at line 593 of file googletest-filter-unittest.py.

593 def testShardStatusFileIsCreatedWithListTests(self):
594 """Tests that the shard file is created with the "list_tests" flag."""
595
596 shard_status_file = os.path.join(gtest_test_utils.GetTempDir(),
597 'shard_status_file2')
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 output = InvokeWithModifiedEnv(extra_env,
603 RunAndReturnOutput,
604 [LIST_TESTS_FLAG])
605 finally:
606 # This assertion ensures that Google Test enumerated the tests as
607 # opposed to running them.
608 self.assert_('[==========]' not in output,
609 'Unexpected output during test enumeration.\n'
610 'Please ensure that LIST_TESTS_FLAG is assigned the\n'
611 'correct flag value for listing Google Test tests.')
612
613 self.assert_(os.path.exists(shard_status_file))
614 os.remove(shard_status_file)
615

◆ testThreePatterns()

def googletest-filter-unittest.GTestFilterUnitTest.testThreePatterns (   self)
Tests filters that consist of three patterns.

Definition at line 488 of file googletest-filter-unittest.py.

488 def testThreePatterns(self):
489 """Tests filters that consist of three patterns."""
490
491 self.RunAndVerify('*oo*:*A*:*One', [
492 'FooTest.Abc',
493 'FooTest.Xyz',
494
495 'BarTest.TestOne',
496
497 'BazTest.TestOne',
498 'BazTest.TestA',
499 ])
500
501 # The 2nd pattern is empty.
502 self.RunAndVerify('*oo*::*One', [
503 'FooTest.Abc',
504 'FooTest.Xyz',
505
506 'BarTest.TestOne',
507
508 'BazTest.TestOne',
509 ])
510
511 # The last 2 patterns are empty.
512 self.RunAndVerify('*oo*::', [
513 'FooTest.Abc',
514 'FooTest.Xyz',
515 ])
516

◆ testTwoPatterns()

def googletest-filter-unittest.GTestFilterUnitTest.testTwoPatterns (   self)
Tests filters that consist of two patterns.

Definition at line 475 of file googletest-filter-unittest.py.

475 def testTwoPatterns(self):
476 """Tests filters that consist of two patterns."""
477
478 self.RunAndVerify('Foo*.*:*A*', [
479 'FooTest.Abc',
480 'FooTest.Xyz',
481
482 'BazTest.TestA',
483 ])
484
485 # An empty pattern + a non-empty one
486 self.RunAndVerify(':*A*', ['FooTest.Abc', 'BazTest.TestA'])
487

◆ testUniversalFilters()

def googletest-filter-unittest.GTestFilterUnitTest.testUniversalFilters (   self)
Tests filters that match everything.

Definition at line 398 of file googletest-filter-unittest.py.

398 def testUniversalFilters(self):
399 """Tests filters that match everything."""
400
401 self.RunAndVerify('*', ACTIVE_TESTS)
402 self.RunAndVerify('*.*', ACTIVE_TESTS)
403 self.RunAndVerifyWithSharding('*.*', len(ACTIVE_TESTS) - 3, ACTIVE_TESTS)
404 self.RunAndVerifyAllowingDisabled('*', ACTIVE_TESTS + DISABLED_TESTS)
405 self.RunAndVerifyAllowingDisabled('*.*', ACTIVE_TESTS + DISABLED_TESTS)
406

◆ testWildcardInTestCaseName()

def googletest-filter-unittest.GTestFilterUnitTest.testWildcardInTestCaseName (   self)
Tests using wildcard in the test case name.

Definition at line 447 of file googletest-filter-unittest.py.

447 def testWildcardInTestCaseName(self):
448 """Tests using wildcard in the test case name."""
449
450 self.RunAndVerify('*a*.*', [
451 'BarTest.TestOne',
452 'BarTest.TestTwo',
453 'BarTest.TestThree',
454
455 'BazTest.TestOne',
456 'BazTest.TestA',
457 'BazTest.TestB', ] + DEATH_TESTS + PARAM_TESTS)
458

◆ testWildcardInTestName()

def googletest-filter-unittest.GTestFilterUnitTest.testWildcardInTestName (   self)
Tests using wildcard in the test name.

Definition at line 459 of file googletest-filter-unittest.py.

459 def testWildcardInTestName(self):
460 """Tests using wildcard in the test name."""
461
462 self.RunAndVerify('*.*A*', ['FooTest.Abc', 'BazTest.TestA'])
463

The documentation for this class was generated from the following file: