[Python-checkins] r54207 - in python/trunk/Lib: test/test_unittest.py unittest.py

georg.brandl python-checkins at python.org
Wed Mar 7 12:54:54 CET 2007


Author: georg.brandl
Date: Wed Mar  7 12:54:49 2007
New Revision: 54207

Modified:
   python/trunk/Lib/test/test_unittest.py
   python/trunk/Lib/unittest.py
Log:
Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Fixes #878275.



Modified: python/trunk/Lib/test/test_unittest.py
==============================================================================
--- python/trunk/Lib/test/test_unittest.py	(original)
+++ python/trunk/Lib/test/test_unittest.py	Wed Mar  7 12:54:49 2007
@@ -1580,6 +1580,19 @@
             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):

Modified: python/trunk/Lib/unittest.py
==============================================================================
--- python/trunk/Lib/unittest.py	(original)
+++ python/trunk/Lib/unittest.py	Wed Mar  7 12:54:49 2007
@@ -428,9 +428,18 @@
         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)
 


More information about the Python-checkins mailing list