summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/test
diff options
context:
space:
mode:
authorRobert Collins <rbtcollins@hp.com>2014-11-04 14:09:01 (GMT)
committerRobert Collins <rbtcollins@hp.com>2014-11-04 14:09:01 (GMT)
commitbf2bda3c9704181cebb6163f5eacd5ad4e1c15f4 (patch)
tree1f2e15056fc7e7bd965dd5b043183bc575d28f62 /Lib/unittest/test
parentd39e199a0d49504583c5672252f653fc01837e32 (diff)
downloadcpython-bf2bda3c9704181cebb6163f5eacd5ad4e1c15f4.zip
cpython-bf2bda3c9704181cebb6163f5eacd5ad4e1c15f4.tar.gz
cpython-bf2bda3c9704181cebb6163f5eacd5ad4e1c15f4.tar.bz2
Close #22457: Honour load_tests in the start_dir of discovery.
We were not honouring load_tests in a package/__init__.py when that was the start_dir parameter, though we do when it is a child package. The fix required a little care since it introduces the possibility of infinite recursion.
Diffstat (limited to 'Lib/unittest/test')
-rw-r--r--Lib/unittest/test/test_discovery.py45
-rw-r--r--Lib/unittest/test/test_loader.py2
2 files changed, 46 insertions, 1 deletions
diff --git a/Lib/unittest/test/test_discovery.py b/Lib/unittest/test/test_discovery.py
index 92b983a..4f61314 100644
--- a/Lib/unittest/test/test_discovery.py
+++ b/Lib/unittest/test/test_discovery.py
@@ -368,6 +368,51 @@ class TestDiscovery(unittest.TestCase):
self.assertEqual(_find_tests_args, [(start_dir, 'pattern')])
self.assertIn(top_level_dir, sys.path)
+ def test_discover_start_dir_is_package_calls_package_load_tests(self):
+ # This test verifies that the package load_tests in a package is indeed
+ # invoked when the start_dir is a package (and not the top level).
+ # http://bugs.python.org/issue22457
+
+ # Test data: we expect the following:
+ # an isfile to verify the package, then importing and scanning
+ # as per _find_tests' normal behaviour.
+ # We expect to see our load_tests hook called once.
+ vfs = {abspath('/toplevel'): ['startdir'],
+ abspath('/toplevel/startdir'): ['__init__.py']}
+ def list_dir(path):
+ return list(vfs[path])
+ self.addCleanup(setattr, os, 'listdir', os.listdir)
+ os.listdir = list_dir
+ self.addCleanup(setattr, os.path, 'isfile', os.path.isfile)
+ os.path.isfile = lambda path: path.endswith('.py')
+ self.addCleanup(setattr, os.path, 'isdir', os.path.isdir)
+ os.path.isdir = lambda path: not path.endswith('.py')
+ self.addCleanup(sys.path.remove, abspath('/toplevel'))
+
+ class Module(object):
+ paths = []
+ load_tests_args = []
+
+ def __init__(self, path):
+ self.path = path
+
+ def load_tests(self, loader, tests, pattern):
+ return ['load_tests called ' + self.path]
+
+ def __eq__(self, other):
+ return self.path == other.path
+
+ loader = unittest.TestLoader()
+ loader._get_module_from_name = lambda name: Module(name)
+ loader.suiteClass = lambda thing: thing
+
+ suite = loader.discover('/toplevel/startdir', top_level_dir='/toplevel')
+
+ # We should have loaded tests from the package __init__.
+ # (normally this would be nested TestSuites.)
+ self.assertEqual(suite,
+ [['load_tests called startdir']])
+
def setup_import_issue_tests(self, fakefile):
listdir = os.listdir
os.listdir = lambda _: [fakefile]
diff --git a/Lib/unittest/test/test_loader.py b/Lib/unittest/test/test_loader.py
index c489730..68f1036 100644
--- a/Lib/unittest/test/test_loader.py
+++ b/Lib/unittest/test/test_loader.py
@@ -841,7 +841,7 @@ class Test_TestLoader(unittest.TestCase):
loader = unittest.TestLoader()
suite = loader.loadTestsFromNames(
- ['unittest.loader.sdasfasfasdf', 'unittest'])
+ ['unittest.loader.sdasfasfasdf', 'unittest.test.dummy'])
error, test = self.check_deferred_error(loader, list(suite)[0])
expected = "module 'unittest.loader' has no attribute 'sdasfasfasdf'"
self.assertIn(