diff -r 3031d69f94ef Lib/unittest/loader.py --- a/Lib/unittest/loader.py Sat Mar 23 12:00:00 2013 -0700 +++ b/Lib/unittest/loader.py Sun Mar 24 00:11:38 2013 +0200 @@ -200,6 +200,8 @@ self._top_level_dir = top_level_dir is_not_importable = False + is_namespace = False + tests = [] if os.path.isdir(os.path.abspath(start_dir)): start_dir = os.path.abspath(start_dir) if start_dir != top_level_dir: @@ -213,15 +215,33 @@ else: the_module = sys.modules[start_dir] top_part = start_dir.split('.')[0] - start_dir = os.path.abspath(os.path.dirname((the_module.__file__))) + try: + start_dir = os.path.abspath(os.path.dirname((the_module.__file__))) + except AttributeError: + # look for namespace packages + if hasattr(the_module.__loader__, '_path'): + is_namespace = True + for path in the_module.__loader__._path: + if not set_implicit_top and not path.startswith(top_level_dir): + continue + self._top_level_dir = path.split(the_module.__name__.replace(".", os.path.sep))[0] + tests.extend(self._find_tests(path, pattern, namespace=True)) + else: + # builtin module + raise TypeError('Can not use builtin modules as dotted module names') from None + if set_implicit_top: - self._top_level_dir = self._get_directory_containing_module(top_part) - sys.path.remove(top_level_dir) + if not is_namespace: + self._top_level_dir = self._get_directory_containing_module(top_part) + sys.path.remove(top_level_dir) + else: + 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)) + if not is_namespace: + tests = list(self._find_tests(start_dir, pattern)) return self.suiteClass(tests) def _get_directory_containing_module(self, module_name): @@ -254,7 +274,7 @@ # 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, namespace=False): """Used by discovery. Yields test suites it loads.""" paths = sorted(os.listdir(start_dir)) @@ -286,10 +306,10 @@ "Is this module globally installed?") raise ImportError(msg % (mod_name, module_dir, expected_dir)) yield self.loadTestsFromModule(module) - elif os.path.isdir(full_path): - if not os.path.isfile(os.path.join(full_path, '__init__.py')): - continue - + elif os.path.isdir(full_path): + if not namespace and not os.path.isfile(os.path.join(full_path, '__init__.py')): + continue + load_tests = None tests = None if fnmatch(path, pattern): @@ -304,7 +324,7 @@ # tests loaded from package file yield tests # recurse into the package - yield from self._find_tests(full_path, pattern) + yield from self._find_tests(full_path, pattern, namespace=namespace) else: try: yield load_tests(self, tests, pattern) diff -r 3031d69f94ef Lib/unittest/test/test_discovery.py --- a/Lib/unittest/test/test_discovery.py Sat Mar 23 12:00:00 2013 -0700 +++ b/Lib/unittest/test/test_discovery.py Sun Mar 24 00:11:38 2013 +0200 @@ -1,6 +1,8 @@ import os import re import sys +import types +import builtins import unittest @@ -172,7 +174,7 @@ self.addCleanup(restore_isdir) _find_tests_args = [] - def _find_tests(start_dir, pattern): + def _find_tests(start_dir, pattern, namespace=None): _find_tests_args.append((start_dir, pattern)) return ['tests'] loader._find_tests = _find_tests @@ -417,7 +419,7 @@ expectedPath = os.path.abspath(os.path.dirname(unittest.test.__file__)) self.wasRun = False - def _find_tests(start_dir, pattern): + def _find_tests(start_dir, pattern, namespace=None): self.wasRun = True self.assertEqual(start_dir, expectedPath) return tests @@ -427,5 +429,52 @@ self.assertEqual(suite._tests, tests) + def test_discovery_from_dotted_path_builtin_modules(self): + + loader = unittest.TestLoader() + + listdir = os.listdir + os.listdir = lambda _: ['test_this_does_not_exist.py'] + isfile = os.path.isfile + isdir = os.path.isdir + os.path.isdir = lambda _: False + orig_sys_path = sys.path[:] + def restore(): + os.path.isfile = isfile + os.path.isdir = isdir + os.listdir = listdir + sys.path[:] = orig_sys_path + self.addCleanup(restore) + + with self.assertRaises(TypeError): + loader.discover('sys') + + def test_discovery_from_dotted_namespace_packages(self): + loader = unittest.TestLoader() + + orig_import = __import__ + package = types.ModuleType('package') + package.__loader__ = types.SimpleNamespace( _path=['/a', '/b']) + + def _import(packagename, *args, **kwargs): + sys.modules[packagename] = package + return package + + def cleanup(): + builtins.__import__ = orig_import + self.addCleanup(cleanup) + builtins.__import__ = _import + + _find_tests_args = [] + def _find_tests(start_dir, pattern, namespace=None): + _find_tests_args.append((start_dir, pattern)) + return ['%s/tests' % start_dir] + + loader._find_tests = _find_tests + loader.suiteClass = list + suite = loader.discover('package') + self.assertEqual(suite, ['/a/tests', '/b/tests']) + + if __name__ == '__main__': unittest.main()