pytest-dev / pytest-dev/pytest
Autouse fixtures in Packages (__init__.py)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the example using pkg/init.py and pkg/test_x.py, comparing the autouse fixture with setup_module. Then inspect pytest's fixture discovery and package collection behavior to determine whether package-level autouse fixtures are intended to run, and add regression coverage only after the expected behavior is resolved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100