mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Support dotted module names for test discovery paths in unittest. Issue 7780.
This commit is contained in:
parent
2b79fdfc69
commit
931190b4f5
2 changed files with 32 additions and 6 deletions
|
@ -171,22 +171,41 @@ class TestLoader(object):
|
|||
packages can continue discovery themselves. top_level_dir is stored so
|
||||
load_tests does not need to pass this argument in to loader.discover().
|
||||
"""
|
||||
set_implicit_top = False
|
||||
if top_level_dir is None and self._top_level_dir is not None:
|
||||
# make top_level_dir optional if called from load_tests in a package
|
||||
top_level_dir = self._top_level_dir
|
||||
elif top_level_dir is None:
|
||||
set_implicit_top = True
|
||||
top_level_dir = start_dir
|
||||
|
||||
top_level_dir = os.path.abspath(os.path.normpath(top_level_dir))
|
||||
start_dir = os.path.abspath(os.path.normpath(start_dir))
|
||||
top_level_dir = os.path.abspath(top_level_dir)
|
||||
|
||||
if not top_level_dir in sys.path:
|
||||
# all test modules must be importable from the top level directory
|
||||
sys.path.append(top_level_dir)
|
||||
self._top_level_dir = top_level_dir
|
||||
|
||||
if start_dir != top_level_dir and not os.path.isfile(os.path.join(start_dir, '__init__.py')):
|
||||
# what about __init__.pyc or pyo (etc)
|
||||
is_not_importable = False
|
||||
if os.path.isdir(os.path.abspath(start_dir)):
|
||||
start_dir = os.path.abspath(start_dir)
|
||||
if start_dir != top_level_dir:
|
||||
is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py'))
|
||||
else:
|
||||
# support for discovery from dotted module names
|
||||
try:
|
||||
__import__(start_dir)
|
||||
except ImportError:
|
||||
is_not_importable = True
|
||||
else:
|
||||
the_module = sys.modules[start_dir]
|
||||
top_part = start_dir.split('.')[0]
|
||||
start_dir = os.path.abspath(os.path.dirname((the_module.__file__)))
|
||||
if set_implicit_top:
|
||||
self._top_level_dir = os.path.abspath(os.path.dirname(os.path.dirname(sys.modules[top_part].__file__)))
|
||||
sys.path.remove(top_level_dir)
|
||||
|
||||
if is_not_importable:
|
||||
raise ImportError('Start directory is not importable: %r' % start_dir)
|
||||
|
||||
tests = list(self._find_tests(start_dir, pattern))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue