-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unittest discovery doesn't detect namespace packages when given no parameters #68070
Comments
Unittest discovery does not seem to work if the tests package is a namespace package. The attached file should have all details to reproduce, as soon as I readd an __init__.py everything works fine. Test was done using python3.4.2 and 3.4.3 |
Spent some time looking into this one. Looks like the problem is in TestLoader.discover() method. There are couple of issues I found in it, all caused by same assumption. Documentation [1] states that all test modules found by discover() method should be importable from top_level_dir. Whenever this method finds a subdirectory of start_dir it checks for __init__.py file. If there's no __init__.py then finder assumes that files within this package is not importable and stops recursion. This kind of 'importablity' check is not valid since we have namespace packages. I'm not sure what should be done to fix this issue. We can change documentation to state that only regular packages with tests will be discovered. Or we can fix 'importability' checks, which will mean that all tests in all subdirectories will be discovered. [1] https://docs.python.org/3.4/library/unittest.html#unittest.TestLoader.discover |
Is there any reason for unittest to not use pkgutil.iter_modules or pkgutil.walk_packages? Either should work. |
Isn't this already supported by the patch added in bpo-17457? |
Not fully. Patch for bpo-17457 fixed discovery when you explicitly specified namespace package as a target for discovery. I.e. when you run python -m unittest namespace_pkg But it didn't recurse into any namespace packages inside namespace_pkg. So when you run python -m unittest it doesn't go under all namespace packages (basically folders) in cwd. |
Hi. I'd be happy enough to use pkgutil helpers if they (like walkdirs) allowed trimming the output: part of the definition of discovery is that one can control it, to stop it importing or processing part of the tree that one doesn't want processed. Alex: can you create a test case please - just a small script, python or shell or whatever, that will setup a demonstration of the bug? Thanks. |
This script creates following directory structure issue_23882\ If you run from issue_23882 directory python -m unittest it doesn't find any tests. If there is __init__.py inside tests folder, same command finds test_fail.py and runs it. I'm not sure yet about using pkgutil cause looks like iter_modules and walk_packages do not find namespace packages as well. |
Ok, so here's whats happening: Running with tests as the first parameter works because it doesn't require the directory being directly scanned to be a package. Equally, if the namespace isn't mapped to a local directory, 'python -m unittest tests' will load tests from the tests namespace. So - this seems to be acting as designed. Namespace packages working fine. However, I think what you're asking for is that namespace packages in your cwd are picked up and tested, without you having to supply the namespace name? I think what we'd need for that to work sanely is a patch to teach loader.discover to determine the local namespaces which are present under start_dir. Right now this code: gets in your way: we assume that either we're processing a namespace, or there are no namespaces at all. I think some more detangling of the generator could address this fairly cleanly. |
Thanks. I understand the code pretty well and I saw bpo-17457 that fixes discovery for explicitly specified namespace package. What I need is to know how discovery has to work. Do we need to discover namespace packages inside discovery path? And should we do that recursively? I.e. should we pick namespace packages inside namespace packages? This will lead to recursive scan of all directories under discovery path. |
I'm still not sure which solution is the best. So I attach two simple patches. First one enables full recursive scan of start_dir for namespace packages. Second one loads tests only from top level namespace packages. |
Ah the user model? I think the following: If I run 'python -m unittest' in a directory, then I expect to run all of the tests contained within that directory tree, and no others. Does that definition help? |
Yes. That is how issue23882_find_all.patch works. I just removed hte condition
This makes namespace parameter redundant. Can I remove it? |
Please, review the patch. |
reviewed in rietvald, but here too just in case. The hunk that saves/restores _top_level_dir feels wrong to me - and not part of this bug, please remove it. The rest of the patch is fine today. But it also needs to add two specifically namespace tests: concretely we need the following cases covered: a) namespace subdir/namespace subdir/test_foo.py gets loaded |
(for the trivial case of CLI discover without a parameter - so translate that to the lower level API and then test that) |
I had rejected this idea in bpo-29642. This is a copy of my comments in the issue. --- I'm afraid this change makes testloader searches unrelated directory contains massive files (like node_modules). I don't think loading all tests from whole namespace package is not usual use case. When using import, (namespace) package name is explicitly specified. In test loader's case, there are no such limit. |
I think people misunderstanding and misusing PEP-420, withouth knowing what is namespace package for. I had wrote an article to describe namespace package is not a regular package. |
Searching into directory without __init__.py recursively is not only inefficient, but also dangerous. project/
What happens if My conclution is: |
But namespace packages are still useful for what PEP-420 envisages and they should be able to have runnable tests. For instance projectX/
Here interfaces is a namespace package and projectX and projectY are kept separate, perhaps to reduce dependency and/or for separation of concerns. I don't think this is insurmountable - even taking into account Inada's very good point about dangerous_scripts. A full tests/ packages in on both projectX and projectY would allow their test to run. However, in some environments, it is normal to put the test alongside the code as shown here. But some better documentation on this issue would advisable, and some guidance on the best practice. Python has a complex ecosystem, and so an individual developer might hit this problem but be using a third party |
In such case, both directories must be specified explicitly. Test discovery doesn't know that a directory is namespace package or Note that PEP-420 searches namespace package only when it is specified. |
From the merge: +++ b/Doc/library/unittest.rst
@@ -330,7 +330,9 @@ Test modules and packages can customize test loading and discovery by through
the `load_tests protocol`_.
.. versionchanged:: 3.4
- Test discovery supports :term:`namespace packages <namespace package>`.
+ Test discovery supports :term:`namespace packages <namespace package>`
+ for start directory. Note that you need to the top level directory too.
+ (e.g. ``python -m unittest discover -s root/namespace -t root``).
The last sentence is missing a verb after 'you need to' saying what to do about "the top level directory. "be in"? "go to"? "later destroy"? (just kidding ;-) |
diff -r 293d9964cf6e Lib/unittest/loader.py
--- a/Lib/unittest/loader.py Tue Apr 28 00:04:53 2015 -0400
+++ b/Lib/unittest/loader.py Tue Apr 28 10:12:07 2015 +0300
@@ -338,7 +338,7 @@
raise ImportError('Start directory is not importable: %r' % start_dir)
if not is_namespace:
- tests = list(self._find_tests(start_dir, pattern))
+ tests = list(self._find_tests(start_dir, pattern, namespace=True))
return self.suiteClass(tests)
def _get_directory_containing_module(self, module_name):
@@ -403,7 +403,7 @@
name = self._get_name_from_path(full_path)
self._loading_packages.add(name)
try:
- yield from self._find_tests(full_path, pattern, namespace)
+ yield from self._find_tests(full_path, pattern, False)
finally:
self._loading_packages.discard(name)
diff -r 293d9964cf6e Lib/unittest/test/test_program.py
--- a/Lib/unittest/test/test_program.py Tue Apr 28 00:04:53 2015 -0400
+++ b/Lib/unittest/test/test_program.py Tue Apr 28 10:12:07 2015 +0300
@@ -16,7 +16,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):
self.wasRun = True
self.assertEqual(start_dir, expectedPath)
return tests |
I noticed that namespace package support has been broken since this commit. Now namespace pacakge has __file__ attribute which is None. But...
The commit is backported to 3.7 branch. So namespace package support has been broken since Python 3.7. Shouldn't we drop namespace package support? |
As the author of PEP-420, I think having test discovery support namespace packages is a mistake, at least in the sense of pretending every directory is a namespace package. |
I just reported https://bugs.python.org/issue45864 , and closed as duplicate of this. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: