]> granicus.if.org Git - python/commitdiff
Add some sanity checks to unittest.TestSuite's addTest(s) methods.
authorGeorg Brandl <georg@python.org>
Wed, 7 Mar 2007 11:54:49 +0000 (11:54 +0000)
committerGeorg Brandl <georg@python.org>
Wed, 7 Mar 2007 11:54:49 +0000 (11:54 +0000)
Fixes #878275.

Lib/test/test_unittest.py
Lib/unittest.py

index 0812184afeebc3b4a49cf3bb04477c773eba3061..ac52e720c025e5ccf488cba3385fc60da860209b 100644 (file)
@@ -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):
index 0d69f5268702f4546ba7a25a88b159c2da034345..483f00670b4769f7ddd3a29256a436c091251c30 100644 (file)
@@ -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)