diff options
Diffstat (limited to 'Lib/unittest/loader.py')
-rw-r--r-- | Lib/unittest/loader.py | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py index bfee3dc..4e9e152 100644 --- a/Lib/unittest/loader.py +++ b/Lib/unittest/loader.py @@ -23,12 +23,18 @@ def _make_failed_import_test(name, suiteClass): # 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, ImportError(message), + suiteClass) - 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, exception, suiteClass) + +def _make_failed_test(classname, methodname, exception, suiteClass): + def testFailure(self): + raise exception + attrs = {methodname: testFailure} + TestClass = type(classname, (case.TestCase,), attrs) + return suiteClass((TestClass(methodname),)) class TestLoader(object): @@ -63,7 +69,11 @@ class TestLoader(object): 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 as e: + return _make_failed_load_tests(module.__name__, e, + self.suiteClass) return tests def loadTestsFromName(self, name, module=None): @@ -234,7 +244,11 @@ class TestLoader(object): 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 as e: + yield _make_failed_load_tests(package.__name__, e, + self.suiteClass) defaultTestLoader = TestLoader() |