Issue 17457: extend test discovery to support namespace packages · python/cpython@e28bb15 · GitHub
Skip to content

Commit e28bb15

Browse files
committed
Issue 17457: extend test discovery to support namespace packages
1 parent 8933521 commit e28bb15

4 files changed

Lines changed: 150 additions & 11 deletions

File tree

Lib/unittest/loader.py

Lines changed: 51 additions & 9 deletions

Lib/unittest/test/test_discovery.py

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22
import re
33
import sys
4+
import types
5+
import builtins
46
from test import support
57

68
import unittest
@@ -173,7 +175,7 @@ def restore_isdir():
173175
self.addCleanup(restore_isdir)
174176

175177
_find_tests_args = []
176-
def _find_tests(start_dir, pattern):
178+
def _find_tests(start_dir, pattern, namespace=None):
177179
_find_tests_args.append((start_dir, pattern))
178180
return ['tests']
179181
loader._find_tests = _find_tests
@@ -436,7 +438,7 @@ def test_discovery_from_dotted_path(self):
436438
expectedPath = os.path.abspath(os.path.dirname(unittest.test.__file__))
437439

438440
self.wasRun = False
439-
def _find_tests(start_dir, pattern):
441+
def _find_tests(start_dir, pattern, namespace=None):
440442
self.wasRun = True
441443
self.assertEqual(start_dir, expectedPath)
442444
return tests
@@ -446,5 +448,79 @@ def _find_tests(start_dir, pattern):
446448
self.assertEqual(suite._tests, tests)
447449

448450

451+
def test_discovery_from_dotted_path_builtin_modules(self):
452+
453+
loader = unittest.TestLoader()
454+
455+
listdir = os.listdir
456+
os.listdir = lambda _: ['test_this_does_not_exist.py']
457+
isfile = os.path.isfile
458+
isdir = os.path.isdir
459+
os.path.isdir = lambda _: False
460+
orig_sys_path = sys.path[:]
461+
def restore():
462+
os.path.isfile = isfile
463+
os.path.isdir = isdir
464+
os.listdir = listdir
465+
sys.path[:] = orig_sys_path
466+
self.addCleanup(restore)
467+
468+
with self.assertRaises(TypeError) as cm:
469+
loader.discover('sys')
470+
self.assertEqual(str(cm.exception),
471+
'Can not use builtin modules '
472+
'as dotted module names')
473+
474+
def test_discovery_from_dotted_namespace_packages(self):
475+
loader = unittest.TestLoader()
476+
477+
orig_import = __import__
478+
package = types.ModuleType('package')
479+
package.__path__ = ['/a', '/b']
480+
package.__spec__ = types.SimpleNamespace(
481+
loader=None,
482+
submodule_search_locations=['/a', '/b']
483+
)
484+
485+
def _import(packagename, *args, **kwargs):
486+
sys.modules[packagename] = package
487+
return package
488+
489+
def cleanup():
490+
builtins.__import__ = orig_import
491+
self.addCleanup(cleanup)
492+
builtins.__import__ = _import
493+
494+
_find_tests_args = []
495+
def _find_tests(start_dir, pattern, namespace=None):
496+
_find_tests_args.append((start_dir, pattern))
497+
return ['%s/tests' % start_dir]
498+
499+
loader._find_tests = _find_tests
500+
loader.suiteClass = list
501+
suite = loader.discover('package')
502+
self.assertEqual(suite, ['/a/tests', '/b/tests'])
503+
504+
def test_discovery_failed_discovery(self):
505+
loader = unittest.TestLoader()
506+
package = types.ModuleType('package')
507+
orig_import = __import__
508+
509+
def _import(packagename, *args, **kwargs):
510+
sys.modules[packagename] = package
511+
return package
512+
513+
def cleanup():
514+
builtins.__import__ = orig_import
515+
self.addCleanup(cleanup)
516+
builtins.__import__ = _import
517+
518+
with self.assertRaises(TypeError) as cm:
519+
loader.discover('package')
520+
self.assertEqual(str(cm.exception),
521+
'don\'t know how to discover from {!r}'
522+
.format(package))
523+
524+
449525
if __name__ == '__main__':
450526
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ Core and Builtins
479479
Library
480480
-------
481481

482+
- Issue #17457: unittest test discovery now works with namespace packages.
483+
Patch by Claudiu Popa.
484+
482485
- Issue #18235: Fix the sysconfig variables LDSHARED and BLDSHARED under AIX.
483486
Patch by David Edelsohn.
484487

Misc/python-wing5.wpr

Lines changed: 18 additions & 0 deletions

0 commit comments

Comments
 (0)