Sub topic 16
Organizing tests in a growing project
This chapter assumes you can write and run pytest tests, use fixtures, and have configured testpaths in pyproject.toml. It covers how to lay out a test suite once it outgrows a single file.
A layout that scales
Keep tests in a top-level tests/ directory, separate from the source code. Mirror the structure of the package so each test file is easy to find.
project/
├── pyproject.toml
├── src/
│ └── shop/
│ ├── __init__.py
│ ├── cart.py
│ └── billing.py
└── tests/
├── conftest.py
├── unit/
│ ├── test_cart.py
│ └── test_billing.py
└── integration/
├── conftest.py
└── test_checkout.pyName each test file after the module it tests. When test_cart.py fails, you know to look in cart.py.
Where fixtures live
A conftest.py file shares fixtures with every test in its directory and below. Place each fixture at the lowest level that needs it.
tests/conftest.pyholds fixtures used across the whole suite, such as a sample cart.tests/integration/conftest.pyholds fixtures only integration tests need, such as a database connection.
Fixtures defined closer to a test override fixtures with the same name defined higher up. This lets one directory customize shared behavior without touching other tests.
# tests/conftest.py
import pytest
@pytest.fixture
def prices():
return [10, 20]# tests/unit/test_cart.py
from shop.cart import total
def test_total_uses_shared_fixture(prices):
assert total(prices) == 30The test never imports prices. Pytest finds it in conftest.py automatically.
Separating fast and slow tests
Directories group tests by kind, and markers select them. Combine both. Register the markers so pytest does not warn about unknown names.
[tool.pytest.ini_options]
testpaths = ["tests"]
pythonpath = ["src"]
markers = [
"integration: tests that touch a database or network",
"slow: marks tests as slow (deselect with -m 'not slow')",
]The pythonpath setting adds src to the import path, so from shop.cart import total works without installing the package.
You can then run only part of the suite:
pytest tests/unit
pytest tests/integration
pytest -m "not integration"Run tests/unit on every save, and run the full suite before you commit or in continuous integration.
Common errors
- Duplicate test file names. Two files named
test_utils.pyin different folders can raise an "import file mismatch" error unless each folder has an__init__.py, or the names differ. Unique names are the simplest fix. - Fixtures in the wrong place. Putting every fixture in the root
conftest.pymakes it long and hides which tests depend on what. Move a fixture down when only one directory uses it. - Importing from
conftest.py. Do not import helpers from it. Pytest loads it for fixtures only. Put shared helper functions in a normal module such astests/helpers.py. - Unregistered markers. A typo like
@pytest.mark.slwosilently creates a new marker. Adding--strict-markerstoaddoptsturns that into an error.
Exercise
Your suite has fixtures prices (used by all tests) and db_conn (used only by tests in tests/integration). Where should each be defined, and which command runs everything except the integration tests?
Show answer
Define prices in tests/conftest.py and db_conn in tests/integration/conftest.py. Run pytest -m "not integration" if the integration tests are marked, or pytest tests/unit to run only the unit directory.