pytest-dev / pytest-dev/pytest
`scope=module` parametrized fixtures invoked multiple times, depending on fixture names
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
Using pytest 6.2.5. Consider the following simple setup:
####### conftest.py
import pytest
@pytest.fixture(scope='module', params=[1, 2])
def fix(request):
print('*** fix called with', request.param)
return request.param
@pytest.fixture(scope='module', params=[3, 4])
def fix2(fix, request):
print('fix2 called with', request.param)
return 10*request.param + fix
@pytest.fixture(scope='module', params=[3, 4])
def afix2(fix, request):
print('afix2 called with', request.param)
return 10*request.param + fix
####### test_fix2.py
def test_a(fix2):
pass
def test_b(fix2):
pass
####### test_afix2.py
def test_a(afix2):
pass
def test_b(afix2):
pass
which yields on pytest -s:
test_afix2.py *** fix called with 1
afix2 called with 3
..*** fix called with 2
afix2 called with 3
..afix2 called with 4
..*** fix called with 1
afix2 called with 4
..
test_fix2.py *** fix called with 1
fix2 called with 3
..fix2 called with 4
..*** fix called with 2
fix2 called with 4
..fix2 called with 3
..
I.e., in test_fix2.py, the independent fixture fix is called two times, as expected. However, in test_afix2.py, it is called three times, two of them with the same request.param. This contradicts the scope='module'.
The only difference I could make out is the different name (fix2 vs. afix2) of the dependent fixture.
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
Start by reproducing the behavior with the conftest.py fixtures and test_fix2.py and test_afix2.py examples, running pytest -s. Compare fixture setup and caching across the two dependent fixture names, then inspect the relevant pytest fixture parametrization and scope handling. Done means module-scoped fix is invoked once per parameter combination without the duplicate request.param call shown for afix2.
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
- 35/100