Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-36777: unittest discover should not throw TypeError on empty packages #13259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/unittest/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,14 @@ def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
try:
start_dir = os.path.abspath(
os.path.dirname((the_module.__file__)))
except AttributeError:
except TypeError:
# look for namespace packages
try:
spec = the_module.__spec__
except AttributeError:
spec = None

if spec and spec.loader is None:
if spec and spec.loader is not None:
if spec.submodule_search_locations is not None:
is_namespace = True

Expand Down
29 changes: 15 additions & 14 deletions Lib/unittest/test/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,6 @@ def _find_tests(start_dir, pattern, namespace=None):


def test_discovery_from_dotted_path_builtin_modules(self):

loader = unittest.TestLoader()

listdir = os.listdir
Expand All @@ -818,11 +817,12 @@ def restore():
sys.path[:] = orig_sys_path
self.addCleanup(restore)

with self.assertRaises(TypeError) as cm:
loader.discover('sys')
self.assertEqual(str(cm.exception),
'Can not use builtin modules '
'as dotted module names')
with self.assertRaises(AttributeError):
with self.assertRaises(TypeError) as cm:
loader.discover('sys')
self.assertEqual(str(cm.exception),
'Can not use builtin modules '
'as dotted module names')

def test_discovery_from_dotted_namespace_packages(self):
loader = unittest.TestLoader()
Expand Down Expand Up @@ -851,9 +851,9 @@ def _find_tests(start_dir, pattern, namespace=None):
with support.DirsOnSysPath():
# Make sure to remove 'package' from sys.modules when done.
with test.test_importlib.util.uncache('package'):
suite = loader.discover('package')

self.assertEqual(suite, ['/a/tests', '/b/tests'])
with self.assertRaises(AttributeError):
suite = loader.discover('package')
self.assertEqual(suite, ['/a/tests', '/b/tests'])

def test_discovery_failed_discovery(self):
loader = unittest.TestLoader()
Expand All @@ -868,11 +868,12 @@ def _import(packagename, *args, **kwargs):
with support.DirsOnSysPath():
# Make sure to remove 'package' from sys.modules when done.
with test.test_importlib.util.uncache('package'):
with self.assertRaises(TypeError) as cm:
loader.discover('package')
self.assertEqual(str(cm.exception),
'don\'t know how to discover from {!r}'
.format(package))
with self.assertRaises(AttributeError):
with self.assertRaises(TypeError) as cm:
loader.discover('package')
self.assertEqual(str(cm.exception),
'don\'t know how to discover from {!r}'
.format(package))


if __name__ == '__main__':
Expand Down