Skip to content
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

Autouse fixtures in Packages (__init__.py) #4085

Open
nicoddemus opened this issue Oct 6, 2018 · 4 comments
Open

Autouse fixtures in Packages (__init__.py) #4085

nicoddemus opened this issue Oct 6, 2018 · 4 comments
Labels
topic: fixtures anything involving fixtures directly or indirectly type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@nicoddemus
Copy link
Member

nicoddemus commented Oct 6, 2018

Autouse fixtures defined in package __init__.py files are not being executed for tests under the package.

Consider:

# content of pkg/__init__.py
import pytest

values = []

@pytest.fixture(scope="module", autouse=True)
def startup():
    values.append("started")

# content of pkg/test_x.py
from . import values

def test_x():
    assert values == ['started']

This fails because startup() is never called.

If however we replace startup by a xunit-style setup_module function:

# content of pkg/__init__.py
import pytest

values = []

# @pytest.fixture(scope="module", autouse=True)
# def startup():
#     values.append("started")

def setup_module():
    values.append("started")

Then the test passes.

Discovered this bug while working on #3094. 😬

As discussed below, we have to discuss if this is a bug or not.

@nicoddemus nicoddemus added type: bug problem that needs to be addressed topic: fixtures anything involving fixtures directly or indirectly labels Oct 6, 2018
@nicoddemus nicoddemus changed the title module-scoped fixtures not working with Packages Autouse fixtures not working with Packages Oct 6, 2018
@nicoddemus
Copy link
Member Author

nicoddemus commented Oct 7, 2018

Looking at the code, I'm not so sure if this is a bug or just how things are intended to work.

setup_module gets called because after the package gets collected setup_module and teardown_module will be called on it because of how SetupState works.

But for fixtures we don't have the same guarantees, as fixtures that are meant to be shared are supposed to go into conftest files instead.

To fix this will mean to make autouse fixtures in packages to apply to all test modules under it, like a conftest.py file would.

@RonnyPfannschmidt thoughts?

@RonnyPfannschmidt
Copy link
Member

@nicoddemus structurally we simply never had packages share structural locations with test files

i beleive we need to take a close look on what should go to conftest, package and how things interact, and then deprecate some things harshly

our life cycle policies are unable to adequately handle the issue at hand, its going to be a mess

@nicoddemus
Copy link
Member Author

OK, so I will go on with #3094 (I can workaround this) and defer this discussion for later.

@nicoddemus nicoddemus added type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature and removed type: bug problem that needs to be addressed labels Oct 7, 2018
@nicoddemus nicoddemus changed the title Autouse fixtures not working with Packages Autouse fixtures in Packages (__init__.py) Oct 7, 2018
@blacklight
Copy link

I'm bumping into this issue a few years later. I agree that workarounds can be found, but this isn't the way things are expected to work. If everything in the tests' root __init__.py gets executed before the tests, then autouse=True fixtures should also be included.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: fixtures anything involving fixtures directly or indirectly type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

3 participants