Skip to content

Collect any tests from a package's __init__.py #3797

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

Merged
merged 1 commit into from
Aug 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ def isinitpath(self, path):
def collect(self):
this_path = self.fspath.dirpath()
pkg_prefix = None
yield Module(this_path.join("__init__.py"), self)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as is this isnt acceptable, since it doesn't check if the filenames is acceptable and might cause surprises for people that have objects with fitting names in packages

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh good catch @RonnyPfannschmidt. Just to be clear you mean to change this bit to something like:

init_module = this_path.join("__init__.py")
if init_module.check(file=1) and init_module.match(self.config.getini('python_files')):
    yield Module(init_module, self)

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should reuse the filename matching and we dont need to check for existence anymore

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the __init__.py is hardcoded at:

for pat in parent.config.getini("python_files") + ["__init__.py"]:

If you remove that, then you have to explicitly add the __init__.py pattern to "python_files" to enable the Package collection. I'm not sure what's the best way to look for packages but also not duplicate the filename matching.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gentle ping here? I'm not sure if it is worth reusing the filename matching for __init__ files, which is what I got from @RonnyPfannschmidt's comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packages have different behaviours than modules to begin with, so i dont see a problem there

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we can go ahead and use my suggestion?

init_module = this_path.join("__init__.py")
if init_module.check(file=1) and init_module.match(self.config.getini('python_files')):
    yield Module(init_module, self)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

for path in this_path.visit(rec=self._recurse, bf=True, sort=True):
# we will visit our own __init__.py file, in which case we skip it
if path.basename == "__init__.py" and path.dirpath() == this_path:
Expand Down