pytest-dev / pytest-dev/pytest
Specifying configuration( `-c config/pytest.ini`) from non-project-root directory leads to local fixtures ignore
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
When pytest is run with a configuration file that is not in the project root directory, the rootdir becomes the pytest.ini directory(in my case, with pytest -c config/pytest.ini, the rootdir becomes config/ and fixture collection and caching(probably) is broken. Fixtures from different directory level conftests but with the same name are re-used wrongly(See Makefile https://github.com/andrewshkovskii/pytest-fixtures-collection-bug/blob/master/Makefile for examples). The fixture collected first will be re-used in other tests, ignoring the local fixture with the same name.
You can find the bug reproduction in this repo https://github.com/andrewshkovskii/pytest-fixtures-collection-bug
Example:
tests
--- tests1
---- test1.py checks that fixture value == 1
---- conftest.py
----- fixture value returns 1
--- tests2
---- test2.py checks that fixture value == 2
---- conftest.py
----- fixture value returns 2
This will make test2 fail since fixture value returns 1, ignoring local fixture from the local conftest:
pytest -c config/pytest.ini -vvv tests/tests2/test2.py::test2 tests/tests1/test1.py::test1
This works as expected:
pytest -c config/pytest.ini --rootdir=. -vvv tests/tests2/test2.py::test2 tests/tests1/test1.py::test1
uv pip list
Package Version
--------- -------
iniconfig 2.0.0
packaging 24.2
pluggy 1.5.0
pytest 8.3.4
pytest>=8.3andUbuntu 24.04.2 LTS- There are Makefile commands to run to check that the bug exists.
make bug-showcaseruns the tests, fails and shows the bug.make no-bug-showcaseshould not fail
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 linked reproduction's Makefile and run make bug-showcase and make no-bug-showcase using config/pytest.ini. Trace pytest's rootdir selection and fixture collection for the tests/tests1 and tests/tests2 conftest.py files; done means both showcase commands produce the expected results without reusing the wrong local fixture.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100