<#17762 Working with `conftest.py` fixtures in a P...
# github-notifications
q
#17762 Working with `conftest.py` fixtures in a Python monorepo New discussion created by alexey-tereshenkov-aiven In the layout of a monorepo
Copy code
project1
  tests/
    conftest.py (with project1 specific fixtures)
    test_module.py
project2
  tests/
    conftest.py (with project2 specific fixtures)
    test_module.py
conftest.py (with generic fixtures)
when running tests in
project1/tests
directory,
pytest
visits all ancestor directories until reaching the root collecting all
conftest.py
so that fixtures are available. From the docs:
Pants will also infer dependencies on any confest.py files in the current directory and any ancestor directories, which mirrors how Pytest behaves.
This means that when making changes to the root
conftest.py
, all tests will need to be run because every test depends on that root
conftest.py
. It's possible to disable this behavior:
You can turn off this feature by setting conftests = false in the [python-infer] scope.
However, it seems fair that all subprojects may want to use a generic fixture from the root
conftest.py
. But this negatively affects the dependency management within tests as all tests (some of them maybe slow!) will need to be run every time someone touches the root
conftest.py
(since you don't know whether the fixtures a test uses are actually modified). Importing individual fixtures (so that you would split the root
conftest.py
into individual modules) seems to be discouraged:
pytest
docs suggest you define all your fixtures within one single
conftest.py
file. There is an explicit import approach, but I am not sure what are the cons of this approach?
Copy code
# tests/conftest.py
import pytest
from fixtures.add import add
I see usage of
Copy code
pytest_plugins = (
   "fixtures.fixture_1",
   "fixtures.fixture_2",
)
in various repositories, but this doesn't work well with Pants dependency inference (as there are no
import
statements). What kind of approach have you decided to go with? pantsbuild/pants