Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
Python interview question
Pytest & fixturesMiddleCommonPractical

What is the purpose of a `conftest.py` file in a pytest-based test automation project?

Answer

`conftest.py` is a special file pytest automatically discovers and loads without an explicit import, used to define fixtures, hooks and plugins that should be shared across multiple test files in the same directory (and its subdirectories) without every test file needing to import them individually. Placing shared fixtures like a configured WebDriver instance, an authenticated API client, or test data factories in `conftest.py` avoids duplicating that setup logic across dozens of test modules, and multiple `conftest.py` files at different directory levels let broader, expensive fixtures live near the project root while narrower, feature-specific ones live closer to the tests that use them.

Strong answer includes

  • automatically discovered by pytest, no explicit import needed
  • shares fixtures/hooks/plugins across all test files in that directory tree
  • multiple conftest.py files at different levels scope shared setup appropriately

Practical example

A root-level `conftest.py` might provide a `browser` fixture shared by every UI test, while a `tests/checkout/conftest.py` adds a checkout-specific `cart_with_items` fixture used only by tests in that subdirectory.

#pytest#test-automation

Sources

pytest documentationpytest · Fixture scope, parametrization, markers and plugin architecture