mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
unittest TestLoader test discovery filename matching done in a method. This makes it easier to override the matching strategy in subclasses. No behaviour change in actual implementation.
This commit is contained in:
parent
d5adb5d73d
commit
9ef5d33084
1 changed files with 24 additions and 20 deletions
|
@ -230,6 +230,10 @@ class TestLoader(object):
|
||||||
__import__(name)
|
__import__(name)
|
||||||
return sys.modules[name]
|
return sys.modules[name]
|
||||||
|
|
||||||
|
def _match_path(self, path, full_path, pattern):
|
||||||
|
# override this method to use alternative matching strategy
|
||||||
|
return fnmatch(path, pattern)
|
||||||
|
|
||||||
def _find_tests(self, start_dir, pattern):
|
def _find_tests(self, start_dir, pattern):
|
||||||
"""Used by discovery. Yields test suites it loads."""
|
"""Used by discovery. Yields test suites it loads."""
|
||||||
paths = os.listdir(start_dir)
|
paths = os.listdir(start_dir)
|
||||||
|
@ -240,26 +244,26 @@ class TestLoader(object):
|
||||||
if not VALID_MODULE_NAME.match(path):
|
if not VALID_MODULE_NAME.match(path):
|
||||||
# valid Python identifiers only
|
# valid Python identifiers only
|
||||||
continue
|
continue
|
||||||
|
if not self._match_path(path, full_path, pattern):
|
||||||
if fnmatch(path, pattern):
|
continue
|
||||||
# if the test file matches, load it
|
# if the test file matches, load it
|
||||||
name = self._get_name_from_path(full_path)
|
name = self._get_name_from_path(full_path)
|
||||||
try:
|
try:
|
||||||
module = self._get_module_from_name(name)
|
module = self._get_module_from_name(name)
|
||||||
except:
|
except:
|
||||||
yield _make_failed_import_test(name, self.suiteClass)
|
yield _make_failed_import_test(name, self.suiteClass)
|
||||||
else:
|
else:
|
||||||
mod_file = os.path.abspath(getattr(module, '__file__', full_path))
|
mod_file = os.path.abspath(getattr(module, '__file__', full_path))
|
||||||
realpath = os.path.splitext(mod_file)[0]
|
realpath = os.path.splitext(mod_file)[0]
|
||||||
fullpath_noext = os.path.splitext(full_path)[0]
|
fullpath_noext = os.path.splitext(full_path)[0]
|
||||||
if realpath.lower() != fullpath_noext.lower():
|
if realpath.lower() != fullpath_noext.lower():
|
||||||
module_dir = os.path.dirname(realpath)
|
module_dir = os.path.dirname(realpath)
|
||||||
mod_name = os.path.splitext(os.path.basename(full_path))[0]
|
mod_name = os.path.splitext(os.path.basename(full_path))[0]
|
||||||
expected_dir = os.path.dirname(full_path)
|
expected_dir = os.path.dirname(full_path)
|
||||||
msg = ("%r module incorrectly imported from %r. Expected %r. "
|
msg = ("%r module incorrectly imported from %r. Expected %r. "
|
||||||
"Is this module globally installed?")
|
"Is this module globally installed?")
|
||||||
raise ImportError(msg % (mod_name, module_dir, expected_dir))
|
raise ImportError(msg % (mod_name, module_dir, expected_dir))
|
||||||
yield self.loadTestsFromModule(module)
|
yield self.loadTestsFromModule(module)
|
||||||
elif os.path.isdir(full_path):
|
elif os.path.isdir(full_path):
|
||||||
if not os.path.isfile(os.path.join(full_path, '__init__.py')):
|
if not os.path.isfile(os.path.join(full_path, '__init__.py')):
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue