diff options
author | Georg Brandl <georg@python.org> | 2007-03-07 11:54:49 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2007-03-07 11:54:49 (GMT) |
commit | d9e50261e8c108b0ab5b336cea75c0b9fd03a81e (patch) | |
tree | d9995f4a52b17d1b45d5e504d831c7e715b71034 | |
parent | 9decc0dc8b42c713e030dafc766af7ddfc6caf89 (diff) | |
download | cpython-d9e50261e8c108b0ab5b336cea75c0b9fd03a81e.zip cpython-d9e50261e8c108b0ab5b336cea75c0b9fd03a81e.tar.gz cpython-d9e50261e8c108b0ab5b336cea75c0b9fd03a81e.tar.bz2 |
Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Fixes #878275.
-rw-r--r-- | Lib/test/test_unittest.py | 13 | ||||
-rw-r--r-- | Lib/unittest.py | 9 |
2 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py index 0812184..ac52e72 100644 --- a/Lib/test/test_unittest.py +++ b/Lib/test/test_unittest.py @@ -1580,6 +1580,19 @@ class Test_TestSuite(TestCase, TestEquality): pass else: 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): diff --git a/Lib/unittest.py b/Lib/unittest.py index 0d69f52..483f006 100644 --- a/Lib/unittest.py +++ b/Lib/unittest.py @@ -428,9 +428,18 @@ class TestSuite: return cases 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) def addTests(self, tests): + if isinstance(tests, basestring): + raise TypeError("tests must be an iterable of tests, not a string") for test in tests: self.addTest(test) |