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

michael.foord python-checkins at python.org
Sun Mar 21 01:53:39 CET 2010


Author: michael.foord
Date: Sun Mar 21 01:53:39 2010
New Revision: 79163

Log:
A faulty load_tests in a test module no longer halts test discovery. A placeholder test, that reports the failure, is created instead.

Modified:
   python/trunk/Lib/test/test_unittest.py
   python/trunk/Lib/unittest/loader.py

Modified: python/trunk/Lib/test/test_unittest.py
==============================================================================
--- python/trunk/Lib/test/test_unittest.py	(original)
+++ python/trunk/Lib/test/test_unittest.py	Sun Mar 21 01:53:39 2010
@@ -289,6 +289,21 @@
         suite = loader.loadTestsFromModule(m, use_load_tests=False)
         self.assertEquals(load_tests_args, [])
 
+    def test_loadTestsFromModule__faulty_load_tests(self):
+        m = types.ModuleType('m')
+
+        def load_tests(loader, tests, pattern):
+            raise TypeError('some failure')
+        m.load_tests = load_tests
+
+        loader = unittest.TestLoader()
+        suite = loader.loadTestsFromModule(m)
+        self.assertIsInstance(suite, unittest.TestSuite)
+        self.assertEqual(suite.countTestCases(), 1)
+        test = list(suite)[0]
+
+        self.assertRaisesRegexp(TypeError, "some failure", test.m)
+
     ################################################################
     ### /Tests for TestLoader.loadTestsFromModule()
 

Modified: python/trunk/Lib/unittest/loader.py
==============================================================================
--- python/trunk/Lib/unittest/loader.py	(original)
+++ python/trunk/Lib/unittest/loader.py	Sun Mar 21 01:53:39 2010
@@ -33,12 +33,18 @@
         # Python 2.3 compatibility
         # format_exc returns two frames of discover.py as well
         message += '\n%s' % traceback.format_exc()
+    return _make_failed_test('ModuleImportFailure', name, suiteClass,
+                             ImportError(message))
 
-    def testImportFailure(self):
-        raise ImportError(message)
-    attrs = {name: testImportFailure}
-    ModuleImportFailure = type('ModuleImportFailure', (case.TestCase,), attrs)
-    return suiteClass((ModuleImportFailure(name),))
+def _make_failed_load_tests(name, exception, suiteClass):
+    return _make_failed_test('LoadTestsFailure', name, suiteClass, exception)
+
+def _make_failed_test(classname, methodname, suiteClass, exception):
+    def testFailure(self):
+        raise exception
+    attrs = {methodname: testFailure}
+    TestClass = type(classname, (case.TestCase,), attrs)
+    return suiteClass((TestClass(methodname),))
 
 
 class TestLoader(object):
@@ -73,7 +79,11 @@
         load_tests = getattr(module, 'load_tests', None)
         tests = self.suiteClass(tests)
         if use_load_tests and load_tests is not None:
-            return load_tests(self, tests, None)
+            try:
+                return load_tests(self, tests, None)
+            except Exception, e:
+                return _make_failed_load_tests(module.__name__, e,
+                                               self.suiteClass)
         return tests
 
     def loadTestsFromName(self, name, module=None):
@@ -239,7 +249,11 @@
                     for test in self._find_tests(full_path, pattern):
                         yield test
                 else:
-                    yield load_tests(self, tests, pattern)
+                    try:
+                        yield load_tests(self, tests, pattern)
+                    except Exception, e:
+                        yield _make_failed_load_tests(package.__name__, e,
+                                                      self.suiteClass)
 
 defaultTestLoader = TestLoader()
 


More information about the Python-checkins mailing list