Skip to content

Commit 659dd62

Browse files
committed
Close python#7559: ImportError when loading a test now shown as ImportError.
Previously the ImportError was only shown if the top level containing package failed to import, with other ImportErrors showing up as AttributeError - hiding the real cause. As part of this, `TestLoader.loadTestsFromNames` now captures errors to self.errors.
1 parent c134584 commit 659dd62

4 files changed

Lines changed: 191 additions & 105 deletions

File tree

‎Doc/library/unittest.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,12 @@ Loading and running tests
16291629

16301630
The method optionally resolves *name* relative to the given *module*.
16311631

1632+
.. versionchanged:: 3.5
1633+
If an :exc:`ImportError` or :exc:`AttributeError` occurs while traversing
1634+
*name* then a synthetic test that raises that error when run will be
1635+
returned. These errors are included in the errors accumulated by
1636+
self.errors.
1637+
16321638

16331639
.. method:: loadTestsFromNames(names, module=None)
16341640

‎Lib/unittest/loader.py‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,47 @@ def loadTestsFromName(self, name, module=None):
130130
The method optionally resolves the names relative to a given module.
131131
"""
132132
parts = name.split('.')
133+
error_case, error_message = None, None
133134
if module is None:
134135
parts_copy = parts[:]
135136
while parts_copy:
136137
try:
137-
module = __import__('.'.join(parts_copy))
138+
module_name = '.'.join(parts_copy)
139+
module = __import__(module_name)
138140
break
139141
except ImportError:
140-
del parts_copy[-1]
142+
next_attribute = parts_copy.pop()
143+
# Last error so we can give it to the user if needed.
144+
error_case, error_message = _make_failed_import_test(
145+
next_attribute, self.suiteClass)
141146
if not parts_copy:
142-
raise
147+
# Even the top level import failed: report that error.
148+
self.errors.append(error_message)
149+
return error_case
143150
parts = parts[1:]
144151
obj = module
145152
for part in parts:
146-
parent, obj = obj, getattr(obj, part)
153+
try:
154+
parent, obj = obj, getattr(obj, part)
155+
except AttributeError as e:
156+
# We can't traverse some part of the name.
157+
if (getattr(obj, '__path__', None) is not None
158+
and error_case is not None):
159+
# This is a package (no __path__ per importlib docs), and we
160+
# encountered an error importing something. We cannot tell
161+
# the difference between package.WrongNameTestClass and
162+
# package.wrong_module_name so we just report the
163+
# ImportError - it is more informative.
164+
self.errors.append(error_message)
165+
return error_case
166+
else:
167+
# Otherwise, we signal that an AttributeError has occurred.
168+
error_case, error_message = _make_failed_test(
169+
'AttributeError', part, e, self.suiteClass,
170+
'Failed to access attribute:\n%s' % (
171+
traceback.format_exc(),))
172+
self.errors.append(error_message)
173+
return error_case
147174

148175
if isinstance(obj, types.ModuleType):
149176
return self.loadTestsFromModule(obj)

0 commit comments

Comments
 (0)