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

Specifying configuration( -c config/pytest.ini) from non-project-root directory leads to local fixtures ignore #13246

Open
andrewshkovskii opened this issue Feb 22, 2025 · 1 comment
Labels
topic: config related to config handling, argument parsing and config file type: regression indicates a problem that was introduced in a release which was working previously

Comments

@andrewshkovskii
Copy link

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.3 and Ubuntu 24.04.2 LTS
  • There are Makefile commands to run to check that the bug exists. make bug-showcase runs the tests, fails and shows the bug. make no-bug-showcase should not fail
@RonnyPfannschmidt
Copy link
Member

I believe this is a regression wrt finding the initial directories

The bug causes the assigned prefix of the conftest to be empty

A coworker discovered a similar misbehavior but without config files and without such an easy reproducer.

I'll verify if it's the same codepath

@Zac-HD Zac-HD added type: regression indicates a problem that was introduced in a release which was working previously topic: config related to config handling, argument parsing and config file labels Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: config related to config handling, argument parsing and config file type: regression indicates a problem that was introduced in a release which was working previously
Projects
None yet
Development

No branches or pull requests

3 participants