mirror of
https://github.com/python/cpython.git
synced 2025-11-01 06:01:29 +00:00
Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Fixes #878275.
This commit is contained in:
parent
9decc0dc8b
commit
d9e50261e8
2 changed files with 22 additions and 0 deletions
|
|
@ -1580,6 +1580,19 @@ def test_addTest__noniterable(self):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
self.fail("Failed to raise TypeError")
|
self.fail("Failed to raise TypeError")
|
||||||
|
|
||||||
|
def test_addTest__noncallable(self):
|
||||||
|
suite = unittest.TestSuite()
|
||||||
|
self.assertRaises(TypeError, suite.addTest, 5)
|
||||||
|
|
||||||
|
def test_addTest__casesuiteclass(self):
|
||||||
|
suite = unittest.TestSuite()
|
||||||
|
self.assertRaises(TypeError, suite.addTest, Test_TestSuite)
|
||||||
|
self.assertRaises(TypeError, suite.addTest, unittest.TestSuite)
|
||||||
|
|
||||||
|
def test_addTests__string(self):
|
||||||
|
suite = unittest.TestSuite()
|
||||||
|
self.assertRaises(TypeError, suite.addTests, "foo")
|
||||||
|
|
||||||
|
|
||||||
class Test_FunctionTestCase(TestCase):
|
class Test_FunctionTestCase(TestCase):
|
||||||
|
|
|
||||||
|
|
@ -428,9 +428,18 @@ def countTestCases(self):
|
||||||
return cases
|
return cases
|
||||||
|
|
||||||
def addTest(self, test):
|
def addTest(self, test):
|
||||||
|
# sanity checks
|
||||||
|
if not callable(test):
|
||||||
|
raise TypeError("the test to add must be callable")
|
||||||
|
if (isinstance(test, (type, types.ClassType)) and
|
||||||
|
issubclass(test, (TestCase, TestSuite))):
|
||||||
|
raise TypeError("TestCases and TestSuites must be instantiated "
|
||||||
|
"before passing them to addTest()")
|
||||||
self._tests.append(test)
|
self._tests.append(test)
|
||||||
|
|
||||||
def addTests(self, tests):
|
def addTests(self, tests):
|
||||||
|
if isinstance(tests, basestring):
|
||||||
|
raise TypeError("tests must be an iterable of tests, not a string")
|
||||||
for test in tests:
|
for test in tests:
|
||||||
self.addTest(test)
|
self.addTest(test)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue