summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/loader.py
diff options
context:
space:
mode:
authorRobert Collins <rbtcollins@hp.com>2014-10-20 00:24:05 (GMT)
committerRobert Collins <rbtcollins@hp.com>2014-10-20 00:24:05 (GMT)
commitf920c2122b86857f6c6b61ba857ba5f51dccf7d0 (patch)
tree2984e2d2ee149bbc36bd7f8be2881ca392e664d5 /Lib/unittest/loader.py
parent1ed2e69a4ac1a4ac5a83ce3ab332b3051eaf59f0 (diff)
downloadcpython-f920c2122b86857f6c6b61ba857ba5f51dccf7d0.zip
cpython-f920c2122b86857f6c6b61ba857ba5f51dccf7d0.tar.gz
cpython-f920c2122b86857f6c6b61ba857ba5f51dccf7d0.tar.bz2
Close #19746: expose unittest discovery errors on TestLoader.errors
This makes it possible to examine the errors from unittest discovery without executing the test suite - important when the test suite may be very large, or when enumerating the test ids from a test suite.
Diffstat (limited to 'Lib/unittest/loader.py')
-rw-r--r--Lib/unittest/loader.py33
1 files changed, 24 insertions, 9 deletions
diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py
index a8c6492..aaee52a 100644
--- a/Lib/unittest/loader.py
+++ b/Lib/unittest/loader.py
@@ -21,19 +21,22 @@ VALID_MODULE_NAME = re.compile(r'[_a-z]\w*\.py$', re.IGNORECASE)
def _make_failed_import_test(name, suiteClass):
- message = 'Failed to import test module: %s\n%s' % (name, traceback.format_exc())
+ message = 'Failed to import test module: %s\n%s' % (
+ name, traceback.format_exc())
return _make_failed_test('ModuleImportFailure', name, ImportError(message),
- suiteClass)
+ suiteClass, message)
def _make_failed_load_tests(name, exception, suiteClass):
- return _make_failed_test('LoadTestsFailure', name, exception, suiteClass)
+ message = 'Failed to call load_tests:\n%s' % (traceback.format_exc(),)
+ return _make_failed_test(
+ 'LoadTestsFailure', name, exception, suiteClass, message)
-def _make_failed_test(classname, methodname, exception, suiteClass):
+def _make_failed_test(classname, methodname, exception, suiteClass, message):
def testFailure(self):
raise exception
attrs = {methodname: testFailure}
TestClass = type(classname, (case.TestCase,), attrs)
- return suiteClass((TestClass(methodname),))
+ return suiteClass((TestClass(methodname),)), message
def _make_skipped_test(methodname, exception, suiteClass):
@case.skip(str(exception))
@@ -59,6 +62,10 @@ class TestLoader(object):
suiteClass = suite.TestSuite
_top_level_dir = None
+ def __init__(self):
+ super(TestLoader, self).__init__()
+ self.errors = []
+
def loadTestsFromTestCase(self, testCaseClass):
"""Return a suite of all tests cases contained in testCaseClass"""
if issubclass(testCaseClass, suite.TestSuite):
@@ -107,8 +114,10 @@ class TestLoader(object):
try:
return load_tests(self, tests, pattern)
except Exception as e:
- return _make_failed_load_tests(module.__name__, e,
- self.suiteClass)
+ error_case, error_message = _make_failed_load_tests(
+ module.__name__, e, self.suiteClass)
+ self.errors.append(error_message)
+ return error_case
return tests
def loadTestsFromName(self, name, module=None):
@@ -336,7 +345,10 @@ class TestLoader(object):
except case.SkipTest as e:
yield _make_skipped_test(name, e, self.suiteClass)
except:
- yield _make_failed_import_test(name, self.suiteClass)
+ error_case, error_message = \
+ _make_failed_import_test(name, self.suiteClass)
+ self.errors.append(error_message)
+ yield error_case
else:
mod_file = os.path.abspath(getattr(module, '__file__', full_path))
realpath = _jython_aware_splitext(os.path.realpath(mod_file))
@@ -362,7 +374,10 @@ class TestLoader(object):
except case.SkipTest as e:
yield _make_skipped_test(name, e, self.suiteClass)
except:
- yield _make_failed_import_test(name, self.suiteClass)
+ error_case, error_message = \
+ _make_failed_import_test(name, self.suiteClass)
+ self.errors.append(error_message)
+ yield error_case
else:
load_tests = getattr(package, 'load_tests', None)
tests = self.loadTestsFromModule(package, pattern=pattern)