pytest-dev / pytest-dev/pytest
Adding parameterized fixture in pytest_generate_test is not executing the fixture's code, even if it is marked as "indirect"
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 8.1.1, I'm trying to add a parameterized fixture to a test during pytest_generate_tests. While it's possible to do so when the test is expecting a parameter with the fixture name, trying to add a "usefixtures" mark in order to invoke an undeclared fixture, does not.
Minimal reproduction:
import pytest
def pytest_generate_tests(metafunc):
metafunc.definition.own_markers.append(pytest.mark.usefixtures('myfix'))
# metafunc.fixturenames.append('myfix')
metafunc.parametrize('myfix', ['myfix-1', 'myfix-2', 'myfix-3'], indirect=True)
@pytest.fixture()
def myfix(request):
raise Exception(request.param)
def test_me():
pass
In this form, the code results in collection error, claiming In test_me: function uses no fixture 'myfix'
If the commented out line is uncommented, which, according to @RonnyPfannschmidt shouldn't be done), the test is running and looks as if it's parameterized (even without the "usefixtures" part) , but the fixture's code is not invoked.
Other attempts I've tried are described here: https://github.com/pytest-dev/pytest/discussions/12205#discussioncomment-9083200
There should be a way to conditionally add a fixture to a test, and according to some older discussions, it has been the case in the past
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 with the minimal reproduction in the issue and compare pytest_generate_tests, usefixtures, and indirect parametrization during collection. Trace the fixture discovery and collection behavior, then verify that a conditionally added fixture is both parameterized and executed without manually appending to metafunc.fixturenames.
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
- 45/100