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

Conversation

jonozzz
Copy link

@jonozzz jonozzz commented Aug 10, 2018

Fixes #3773

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

Thanks @jonozzz, could you please add a test and a CHANGELOG entry?

@nicoddemus
Copy link
Member

Restarting CI manually due to Travis hiccup

@coveralls
Copy link

Coverage Status

Coverage increased (+0.05%) to 92.528% when pulling d0bd01b on jonozzz:fix-3773 into 4d8903f on pytest-dev:master.

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

Missing a test here, gentle ping 😁

@jonozzz
Copy link
Author

jonozzz commented Aug 16, 2018

@nicoddemus I'm actually waiting on @deeechun to answer. I don't think this was possible in pre-3.7.0, wasn't it?

@nicoddemus
Copy link
Member

I can get tests in __init__.py the issue in 3.6.4:

project/
  /tests
    test.py  # contains a single test
  __init__.py  # contains a single test

Running this in 3.6.4 gives:

λ pytest -v -o python_files=*.py
============================= test session starts =============================
...
collected 2 items

__init__.py::test_on_package PASSED                                      [ 50%]
tests/test_basic.py::test PASSED                                         [100%]

========================== 2 passed in 0.05 seconds ===========================

And on master:

 λ pytest -v -o python_files=*.pygit
============================= test session starts =============================
...
collected 0 items

======================== no tests ran in 0.03 seconds =========================

The key thing to remember is to set python_files=*.py.

@nicoddemus
Copy link
Member

Oh I just saw that @deeechun replied with basically the same thing. 😁

@@ -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

@nicoddemus nicoddemus merged commit d0bd01b into pytest-dev:master Aug 25, 2018
@nicoddemus
Copy link
Member

This was marked as merged but the actual code did not go in; probably some commits from this went into #3861, but the final code does not contain the changes here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants