-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Fix problems with running tests in package __init__
files (#4046)
#4285
Conversation
src/_pytest/main.py
Outdated
# actually yielded up to higher collection layers, so | ||
# remove it to allow collection by later package | ||
# traversal. | ||
pm._duplicatepaths.discard(pkginit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created a backport for master: #4286 |
Cool! |
Cherry-pick e041823, please. |
Alright, I've updated on top of e041823 |
Codecov Report
@@ Coverage Diff @@
## master #4285 +/- ##
==========================================
+ Coverage 95.65% 95.87% +0.21%
==========================================
Files 109 109
Lines 24630 24639 +9
Branches 2396 2395 -1
==========================================
+ Hits 23560 23622 +62
+ Misses 759 721 -38
+ Partials 311 296 -15
Continue to review full report at Codecov.
|
Great work, thanks! |
Great work guys, thanks! |
Awesome, thanks |
This fixes some behavior changes that were introduced in 3.7.0, detailed by #4046.
If you have a directory structure like
then prior to 3.7.0 you could run
pytest mypackage
from the project directory, and it would pick up all the tests in__init__.py
andmymodule.py
. After 3.7.0 the tests in__init__.py
started being ignored.There was a partial fix for this in #3872, but it only works if the project directory and not the package directory is passed as the argument (commit 8485081 adds an example failing test for this.)
This PR seems to fix that and the other issue in #4046. I'm pretty sure the changes here aren't ideal and should probably be cleaned up by someone more familiar with the codebase, but hopefully this can at least serve as a starting point for the fixes.
The issue with
__init__.py
being ignored seems to stem from it being added to the duplicate paths set when it's visited in the root traversal loop, then when it's visited by the directory collection loop it gets ignored as a duplicate even though it was never checked for tests. Removing it from the duplicate set as a fix causes an additional issue of a duplicate nested package being created, which I also added a special-case fix for.The issue with all package files being collected when only
__init__.py
was specified (test in 3f07f7e) appears to stem from__init__.py
being treated as aPackage
(that gets traversed) when it should be treated as aModule
.With these changes, all the existing tests except one pass (commit 28f4155). The test failure seems legitimate to me, though, because it wasn't counting the package created by the
__init__
file. Please let me know if I'm interpreting that wrong.