Skip to content

Commit 7207d6d

Browse files
author
Michael Foord
committed
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.
1 parent bdd7646 commit 7207d6d

1 file changed

Lines changed: 24 additions & 20 deletions

File tree

Lib/unittest/loader.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ def _get_module_from_name(self, name):
230230
__import__(name)
231231
return sys.modules[name]
232232

233+
def _match_path(self, path, full_path, pattern):
234+
# override this method to use alternative matching strategy
235+
return fnmatch(path, pattern)
236+
233237
def _find_tests(self, start_dir, pattern):
234238
"""Used by discovery. Yields test suites it loads."""
235239
paths = os.listdir(start_dir)
@@ -240,26 +244,26 @@ def _find_tests(self, start_dir, pattern):
240244
if not VALID_MODULE_NAME.match(path):
241245
# valid Python identifiers only
242246
continue
243-
244-
if fnmatch(path, pattern):
245-
# if the test file matches, load it
246-
name = self._get_name_from_path(full_path)
247-
try:
248-
module = self._get_module_from_name(name)
249-
except:
250-
yield _make_failed_import_test(name, self.suiteClass)
251-
else:
252-
mod_file = os.path.abspath(getattr(module, '__file__', full_path))
253-
realpath = os.path.splitext(mod_file)[0]
254-
fullpath_noext = os.path.splitext(full_path)[0]
255-
if realpath.lower() != fullpath_noext.lower():
256-
module_dir = os.path.dirname(realpath)
257-
mod_name = os.path.splitext(os.path.basename(full_path))[0]
258-
expected_dir = os.path.dirname(full_path)
259-
msg = ("%r module incorrectly imported from %r. Expected %r. "
260-
"Is this module globally installed?")
261-
raise ImportError(msg % (mod_name, module_dir, expected_dir))
262-
yield self.loadTestsFromModule(module)
247+
if not self._match_path(path, full_path, pattern):
248+
continue
249+
# if the test file matches, load it
250+
name = self._get_name_from_path(full_path)
251+
try:
252+
module = self._get_module_from_name(name)
253+
except:
254+
yield _make_failed_import_test(name, self.suiteClass)
255+
else:
256+
mod_file = os.path.abspath(getattr(module, '__file__', full_path))
257+
realpath = os.path.splitext(mod_file)[0]
258+
fullpath_noext = os.path.splitext(full_path)[0]
259+
if realpath.lower() != fullpath_noext.lower():
260+
module_dir = os.path.dirname(realpath)
261+
mod_name = os.path.splitext(os.path.basename(full_path))[0]
262+
expected_dir = os.path.dirname(full_path)
263+
msg = ("%r module incorrectly imported from %r. Expected %r. "
264+
"Is this module globally installed?")
265+
raise ImportError(msg % (mod_name, module_dir, expected_dir))
266+
yield self.loadTestsFromModule(module)
263267
elif os.path.isdir(full_path):
264268
if not os.path.isfile(os.path.join(full_path, '__init__.py')):
265269
continue

0 commit comments

Comments
 (0)