aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2015-03-18 23:56:46 +0100
committerAntoine Pitrou <solipsis@pitrou.net>2015-03-18 23:56:46 +0100
commitd833779ceaebeb29352488ffddabf5fc2f070364 (patch)
tree83ff79a3d53920897701d6180472e40ec4e72e93 /Lib/unittest/loader.py
parentIssue #23353: improve exceptions tests for generators (diff)
downloadcpython-d833779ceaebeb29352488ffddabf5fc2f070364.tar.gz
cpython-d833779ceaebeb29352488ffddabf5fc2f070364.tar.bz2
cpython-d833779ceaebeb29352488ffddabf5fc2f070364.zip
Issue #22903: The fake test case created by unittest.loader when it fails importing a test module is now picklable.
Diffstat (limited to 'Lib/unittest/loader.py')
-rw-r--r--Lib/unittest/loader.py29
1 files changed, 20 insertions, 9 deletions
diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py
index 808c50eb668..af39216d263 100644
--- a/Lib/unittest/loader.py
+++ b/Lib/unittest/loader.py
@@ -19,20 +19,31 @@ __unittest = True
VALID_MODULE_NAME = re.compile(r'[_a-z]\w*\.py$', re.IGNORECASE)
+class _FailedTest(case.TestCase):
+ _testMethodName = None
+
+ def __init__(self, method_name, exception):
+ self._exception = exception
+ super(_FailedTest, self).__init__(method_name)
+
+ def __getattr__(self, name):
+ if name != self._testMethodName:
+ return super(_FailedTest, self).__getattr__(name)
+ def testFailure():
+ raise self._exception
+ return testFailure
+
+
def _make_failed_import_test(name, suiteClass):
message = 'Failed to import test module: %s\n%s' % (name, traceback.format_exc())
- return _make_failed_test('ModuleImportFailure', name, ImportError(message),
- suiteClass)
+ return _make_failed_test(name, ImportError(message), suiteClass)
def _make_failed_load_tests(name, exception, suiteClass):
- return _make_failed_test('LoadTestsFailure', name, exception, suiteClass)
+ return _make_failed_test(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),))
+def _make_failed_test(methodname, exception, suiteClass):
+ test = _FailedTest(methodname, exception)
+ return suiteClass((test,))
def _make_skipped_test(methodname, exception, suiteClass):
@case.skip(str(exception))