To share fixtures across test files in pytest, put them in a file named conftest.py. pytest loads it automatically, and every test in that directory and its subdirectories can request the fixtures by name, with no import. This is how you keep one definition of a database, client or sample data and use it everywhere.
This guide explains how pytest conftest.py files work: where to place them, how overriding works, what else they can hold, and the mistakes to avoid. It uses pytest 8 on Python 3.13.
conftest.py is a local plugin file. pytest collects it during test discovery, before it imports the test modules next to it. Fixtures and hooks defined there apply to the tests in the same directory and in every directory below it.
Never import from conftest.py in your test files. Fixtures arrive by name through function arguments, and importing the module can cause the file to be loaded twice.
Start with a fixture inside a single test file:
When a second file needs it, move the fixture to tests/conftest.py:
Both files use the same fixture. Run pytest --fixtures to confirm where pytest found it; the output shows the file for each fixture.
When several fixtures share a name, pytest uses the one closest to the test. The lookup order is: the test module, then the nearest conftest.py, then parent directories, then plugins. This lets a subfolder customise a shared default:
A fixture with the same name can request the parent fixture by using its own name as a parameter, as shown. A test module can also override a fixture locally, for example to inject a different user for one file.
Fixtures are the main use, but conftest.py is also the place for the following.
| Item | Example |
|---|---|
| Hooks | pytest_collection_modifyitems to add markers or reorder tests |
| Command line options | pytest_addoption to add --runslow |
| Shared plugins | pytest_plugins = ["tests.plugins.db"] in the top-level file |
| Marker registration | config.addinivalue_line("markers", "slow: slow test") in pytest_configure |
A small example that adds a --runslow option and skips slow tests by default:
Note that pytest_plugins is only allowed in the top-level conftest.py (at the rootdir of the run), not in nested ones.
conftest.py small. Put shared fixtures at the lowest directory that needs them, so a reader can find where a fixture is defined.tests/helpers.py and import them.__init__.py or use --import-mode=importlib to avoid import conflicts.pytest --setup-show to see the order in which fixtures are created and destroyed across files.Yes, one per directory is normal. Fixtures from each apply to their own subtree, and closer files override farther ones.
No. pytest injects them by name. Importing them is unnecessary and can cause confusing duplicate registrations.
Yes. A root conftest.py applies to all tests below it, and it also makes pytest add its directory to sys.path in the default import mode, which some projects rely on for imports.
Package them as a pytest plugin and register it through an entry point. For one repository, a top-level conftest.py with pytest_plugins is enough.
A conftest.py file shares fixtures, hooks and options with every test in its directory tree, without any import. The closest definition wins, so subfolders can override or extend a fixture. Keep each file focused, place fixtures at the lowest useful level, and use pytest --fixtures and --setup-show to check what pytest actually loads.