What is conftest.py for in Pytest?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
conftest.py is Pytest’s local configuration and fixture-sharing mechanism. It lets you define reusable fixtures, hooks, and test behavior for a directory tree without forcing every test module to import a central helper module manually.
Why conftest.py Exists
As a test suite grows, certain setup patterns repeat:
- creating a test client
- preparing database fixtures
- defining custom command-line options
- registering hooks that tweak collection or reporting
You could put all of that in ordinary Python files and import it everywhere, but that becomes noisy and easy to forget. Pytest solves the problem by auto-discovering conftest.py files based on directory location.
That means tests can simply request a fixture by name, and Pytest finds it through the fixture-discovery rules.
Fixture Sharing by Directory Scope
A conftest.py file applies to tests in its own directory and subdirectories. That makes it ideal for organizing shared test behavior hierarchically.
A simple example looks like this:
Then any test under tests/ can use that fixture without importing it.
That “no import required” behavior is the main reason conftest.py feels special.
More Than Fixtures
conftest.py is not just for fixtures. It can also define Pytest hooks and custom command-line options.
For example, you can add a flag that controls whether slow tests run.
Then a hook can mark or skip tests based on that option.
This keeps test behavior close to the tests it affects.
When To Use conftest.py Versus a Plugin
A good rule is:
- use
conftest.pyfor local test-suite behavior tied to a project or subtree - use a Pytest plugin when the behavior is reusable across many repositories
If the fixture only matters to one application’s tests, conftest.py is the natural home. If the feature is generic enough to publish or share broadly, a plugin is usually cleaner.
This distinction keeps conftest.py from becoming a dumping ground for every test utility the project has ever accumulated.
Organizing Large Test Suites
Large projects often use multiple conftest.py files. A top-level file can define cross-project fixtures, while deeper directories add fixtures specific to API tests, database tests, or UI tests.
For example:
- '
tests/conftest.pyfor shared clients and global options' - '
tests/api/conftest.pyfor HTTP-specific fixtures' - '
tests/db/conftest.pyfor database setup'
That structure works well because Pytest resolves fixtures according to directory scope and normal fixture lookup rules.
Common Pitfalls
The most common mistake is putting unrelated business logic into conftest.py. It should support the tests, not become a miscellaneous utilities module.
Another common issue is defining fixtures too broadly. A fixture placed high in the tree becomes visible to many tests, which can create naming collisions or accidental coupling.
Developers also sometimes try to import conftest.py directly from application code. That is usually the wrong design. conftest.py is for Pytest’s discovery system, not for general-purpose imports.
Finally, if fixture behavior becomes hard to trace, the problem is often scope and organization rather than Pytest itself. Split large files into clearer layers and move generic behavior into helper modules or plugins when appropriate.
Summary
- '
conftest.pyis Pytest’s directory-scoped place for shared fixtures, hooks, and test configuration.' - Tests under that directory tree can use its fixtures without explicit imports.
- It is useful for local test behavior such as clients, database setup, and command-line options.
- Use plugins for broadly reusable behavior and keep
conftest.pyfocused on project-specific testing needs. - Organize multiple
conftest.pyfiles by directory when the test suite grows.
Related reading
- What is double colon in Python when subscripting sequences?
- What is dtype''O'', in pandas?
- What is exactly sklearn.pipeline.Pipeline?
- what is Gridsearch.cv_results_ , could any explain all the things in that i.e mean_test_score etc .?
- What is SAT and what it is good for?
- What is SUT and where did it come from?
- What is How to use getattr in Python?
- What is memoization and how can I use it in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.