mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	Fix breakage in TestSuite.countTestCases() introduced by issue #11798.
This commit is contained in:
		
							parent
							
								
									156b3610b8
								
							
						
					
					
						commit
						b5c66f8645
					
				
					 3 changed files with 45 additions and 4 deletions
				
			
		| 
						 | 
					@ -20,6 +20,7 @@ class BaseTestSuite(object):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, tests=()):
 | 
					    def __init__(self, tests=()):
 | 
				
			||||||
        self._tests = []
 | 
					        self._tests = []
 | 
				
			||||||
 | 
					        self._removed_tests = 0
 | 
				
			||||||
        self.addTests(tests)
 | 
					        self.addTests(tests)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __repr__(self):
 | 
					    def __repr__(self):
 | 
				
			||||||
| 
						 | 
					@ -37,8 +38,9 @@ def __iter__(self):
 | 
				
			||||||
        return iter(self._tests)
 | 
					        return iter(self._tests)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def countTestCases(self):
 | 
					    def countTestCases(self):
 | 
				
			||||||
        cases = 0
 | 
					        cases = self._removed_tests
 | 
				
			||||||
        for test in self:
 | 
					        for test in self:
 | 
				
			||||||
 | 
					            if test:
 | 
				
			||||||
                cases += test.countTestCases()
 | 
					                cases += test.countTestCases()
 | 
				
			||||||
        return cases
 | 
					        return cases
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -70,10 +72,16 @@ def run(self, result):
 | 
				
			||||||
    def _removeTestAtIndex(self, index):
 | 
					    def _removeTestAtIndex(self, index):
 | 
				
			||||||
        """Stop holding a reference to the TestCase at index."""
 | 
					        """Stop holding a reference to the TestCase at index."""
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            self._tests[index] = None
 | 
					            test = self._tests[index]
 | 
				
			||||||
        except TypeError:
 | 
					        except TypeError:
 | 
				
			||||||
            # support for suite implementations that have overriden self._test
 | 
					            # support for suite implementations that have overriden self._tests
 | 
				
			||||||
            pass
 | 
					            pass
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            # Some unittest tests add non TestCase/TestSuite objects to
 | 
				
			||||||
 | 
					            # the suite.
 | 
				
			||||||
 | 
					            if hasattr(test, 'countTestCases'):
 | 
				
			||||||
 | 
					                self._removed_tests += test.countTestCases()
 | 
				
			||||||
 | 
					            self._tests[index] = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __call__(self, *args, **kwds):
 | 
					    def __call__(self, *args, **kwds):
 | 
				
			||||||
        return self.run(*args, **kwds)
 | 
					        return self.run(*args, **kwds)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -51,6 +51,9 @@ def test_init__tests_optional(self):
 | 
				
			||||||
        suite = unittest.TestSuite()
 | 
					        suite = unittest.TestSuite()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.assertEqual(suite.countTestCases(), 0)
 | 
					        self.assertEqual(suite.countTestCases(), 0)
 | 
				
			||||||
 | 
					        # countTestCases() still works after tests are run
 | 
				
			||||||
 | 
					        suite.run(unittest.TestResult())
 | 
				
			||||||
 | 
					        self.assertEqual(suite.countTestCases(), 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # "class TestSuite([tests])"
 | 
					    # "class TestSuite([tests])"
 | 
				
			||||||
    # ...
 | 
					    # ...
 | 
				
			||||||
| 
						 | 
					@ -63,6 +66,9 @@ def test_init__empty_tests(self):
 | 
				
			||||||
        suite = unittest.TestSuite([])
 | 
					        suite = unittest.TestSuite([])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.assertEqual(suite.countTestCases(), 0)
 | 
					        self.assertEqual(suite.countTestCases(), 0)
 | 
				
			||||||
 | 
					        # countTestCases() still works after tests are run
 | 
				
			||||||
 | 
					        suite.run(unittest.TestResult())
 | 
				
			||||||
 | 
					        self.assertEqual(suite.countTestCases(), 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # "class TestSuite([tests])"
 | 
					    # "class TestSuite([tests])"
 | 
				
			||||||
    # ...
 | 
					    # ...
 | 
				
			||||||
| 
						 | 
					@ -84,6 +90,14 @@ def tests():
 | 
				
			||||||
        suite_3 = unittest.TestSuite(set(suite_1))
 | 
					        suite_3 = unittest.TestSuite(set(suite_1))
 | 
				
			||||||
        self.assertEqual(suite_3.countTestCases(), 2)
 | 
					        self.assertEqual(suite_3.countTestCases(), 2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # countTestCases() still works after tests are run
 | 
				
			||||||
 | 
					        suite_1.run(unittest.TestResult())
 | 
				
			||||||
 | 
					        self.assertEqual(suite_1.countTestCases(), 2)
 | 
				
			||||||
 | 
					        suite_2.run(unittest.TestResult())
 | 
				
			||||||
 | 
					        self.assertEqual(suite_2.countTestCases(), 2)
 | 
				
			||||||
 | 
					        suite_3.run(unittest.TestResult())
 | 
				
			||||||
 | 
					        self.assertEqual(suite_3.countTestCases(), 2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # "class TestSuite([tests])"
 | 
					    # "class TestSuite([tests])"
 | 
				
			||||||
    # ...
 | 
					    # ...
 | 
				
			||||||
    # "If tests is given, it must be an iterable of individual test cases
 | 
					    # "If tests is given, it must be an iterable of individual test cases
 | 
				
			||||||
| 
						 | 
					@ -99,6 +113,9 @@ def tests():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        suite = unittest.TestSuite(tests())
 | 
					        suite = unittest.TestSuite(tests())
 | 
				
			||||||
        self.assertEqual(suite.countTestCases(), 2)
 | 
					        self.assertEqual(suite.countTestCases(), 2)
 | 
				
			||||||
 | 
					        # countTestCases() still works after tests are run
 | 
				
			||||||
 | 
					        suite.run(unittest.TestResult())
 | 
				
			||||||
 | 
					        self.assertEqual(suite.countTestCases(), 2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ################################################################
 | 
					    ################################################################
 | 
				
			||||||
    ### /Tests for TestSuite.__init__
 | 
					    ### /Tests for TestSuite.__init__
 | 
				
			||||||
| 
						 | 
					@ -145,6 +162,9 @@ def test_countTestCases_simple(self):
 | 
				
			||||||
        suite = unittest.TestSuite((test1, test2))
 | 
					        suite = unittest.TestSuite((test1, test2))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.assertEqual(suite.countTestCases(), 2)
 | 
					        self.assertEqual(suite.countTestCases(), 2)
 | 
				
			||||||
 | 
					        # countTestCases() still works after tests are run
 | 
				
			||||||
 | 
					        suite.run(unittest.TestResult())
 | 
				
			||||||
 | 
					        self.assertEqual(suite.countTestCases(), 2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # "Return the number of tests represented by the this test object.
 | 
					    # "Return the number of tests represented by the this test object.
 | 
				
			||||||
    # ...this method is also implemented by the TestSuite class, which can
 | 
					    # ...this method is also implemented by the TestSuite class, which can
 | 
				
			||||||
| 
						 | 
					@ -162,6 +182,10 @@ def test2(self): pass
 | 
				
			||||||
        parent = unittest.TestSuite((test3, child, Test1('test1')))
 | 
					        parent = unittest.TestSuite((test3, child, Test1('test1')))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.assertEqual(parent.countTestCases(), 4)
 | 
					        self.assertEqual(parent.countTestCases(), 4)
 | 
				
			||||||
 | 
					        # countTestCases() still works after tests are run
 | 
				
			||||||
 | 
					        parent.run(unittest.TestResult())
 | 
				
			||||||
 | 
					        self.assertEqual(parent.countTestCases(), 4)
 | 
				
			||||||
 | 
					        self.assertEqual(child.countTestCases(), 2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # "Run the tests associated with this suite, collecting the result into
 | 
					    # "Run the tests associated with this suite, collecting the result into
 | 
				
			||||||
    # the test result object passed as result."
 | 
					    # the test result object passed as result."
 | 
				
			||||||
| 
						 | 
					@ -220,6 +244,9 @@ def test(self): pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.assertEqual(suite.countTestCases(), 1)
 | 
					        self.assertEqual(suite.countTestCases(), 1)
 | 
				
			||||||
        self.assertEqual(list(suite), [test])
 | 
					        self.assertEqual(list(suite), [test])
 | 
				
			||||||
 | 
					        # countTestCases() still works after tests are run
 | 
				
			||||||
 | 
					        suite.run(unittest.TestResult())
 | 
				
			||||||
 | 
					        self.assertEqual(suite.countTestCases(), 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # "Add a ... TestSuite to the suite"
 | 
					    # "Add a ... TestSuite to the suite"
 | 
				
			||||||
    def test_addTest__TestSuite(self):
 | 
					    def test_addTest__TestSuite(self):
 | 
				
			||||||
| 
						 | 
					@ -233,6 +260,9 @@ def test(self): pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.assertEqual(suite.countTestCases(), 1)
 | 
					        self.assertEqual(suite.countTestCases(), 1)
 | 
				
			||||||
        self.assertEqual(list(suite), [suite_2])
 | 
					        self.assertEqual(list(suite), [suite_2])
 | 
				
			||||||
 | 
					        # countTestCases() still works after tests are run
 | 
				
			||||||
 | 
					        suite.run(unittest.TestResult())
 | 
				
			||||||
 | 
					        self.assertEqual(suite.countTestCases(), 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # "Add all the tests from an iterable of TestCase and TestSuite
 | 
					    # "Add all the tests from an iterable of TestCase and TestSuite
 | 
				
			||||||
    # instances to this test suite."
 | 
					    # instances to this test suite."
 | 
				
			||||||
| 
						 | 
					@ -392,6 +422,7 @@ def tearDownModule():
 | 
				
			||||||
        self.assertEqual(len(result.errors), 1)
 | 
					        self.assertEqual(len(result.errors), 1)
 | 
				
			||||||
        self.assertEqual(len(result.failures), 0)
 | 
					        self.assertEqual(len(result.failures), 0)
 | 
				
			||||||
        self.assertEqual(result.testsRun, 2)
 | 
					        self.assertEqual(result.testsRun, 2)
 | 
				
			||||||
 | 
					        self.assertEqual(suite.countTestCases(), 2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_overriding_call(self):
 | 
					    def test_overriding_call(self):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -44,6 +44,8 @@ Core and Builtins
 | 
				
			||||||
Library
 | 
					Library
 | 
				
			||||||
-------
 | 
					-------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- Fix breakage in TestSuite.countTestCases() introduced by issue #11798.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- Issue #19918: Fix PurePath.relative_to() under Windows.
 | 
					- Issue #19918: Fix PurePath.relative_to() under Windows.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- Issue #19422: Explicitly disallow non-SOCK_STREAM sockets in the ssl
 | 
					- Issue #19422: Explicitly disallow non-SOCK_STREAM sockets in the ssl
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue