Skip to content
Sarj Code Standards

prefer-pytest-dependency-fixtures

Reuse supplied pytest fixtures and pass test collaborators through fixture dependencies.

Why

Constructing collaborators in a test bypasses pytest's dependency graph, caching and overrides even when only one test currently needs the setup.

Fix

Request function-scoped store, service and settings fixtures by parameter. Use a typed factory fixture for variants or multiple instances. Keep construction under test explicit and preserve setup ordering and the actual owner's cleanup; do not create global singletons or autouse fixtures.

Examples

Before — flagged Direct construction bypasses the requested typed factory
tests/test_probe.py
import pytest
class Probe:
def __init__(self, connection):
self.connection = connection
@pytest.fixture
def probe_factory() -> type[Probe]:
return Probe
def test_connection(probe_factory: type[Probe], connection):
probe = Probe(connection)
assert probe.connection is connection
After — preferred The fixture factory receives the same borrowed connection
tests/test_probe.py
import pytest
class Probe:
def __init__(self, connection):
self.connection = connection
@pytest.fixture
def probe_factory() -> type[Probe]:
return Probe
def test_connection(probe_factory: type[Probe], connection):
probe = probe_factory(connection)
assert probe.connection is connection
Before — flagged A test constructs its service's store
tests/test_service.py
from typing import Protocol
class Port(Protocol):
def read(self): ...
class Store:
def read(self):
return 1
class Service:
def __init__(self, store: Port):
self.store = store
def read(self):
return self.store.read()
def test_read():
store = Store()
service = Service(store)
assert service.read() == 1
After — preferred The store arrives through pytest
tests/test_service.py
from typing import Protocol
class Port(Protocol):
def read(self): ...
class Store:
def read(self):
return 1
class Service:
def __init__(self, store: Port):
self.store = store
def read(self):
return self.store.read()
def test_read(store):
service = Service(store)
assert service.read() == 1
Before — flagged A second store bypasses the supplied fixture
tests/test_store.py
from typing import Protocol
class Port(Protocol):
def read(self): ...
class Store:
def read(self):
return 1
class Service:
def __init__(self, store: Port):
self.store = store
def read(self):
return self.store.read()
def test_read(store: Store):
replacement = Store()
assert replacement.read() == 1
After — preferred Use the supplied store fixture
tests/test_store.py
from typing import Protocol
class Port(Protocol):
def read(self): ...
class Store:
def read(self):
return 1
class Service:
def __init__(self, store: Port):
self.store = store
def read(self):
return self.store.read()
def test_read(store: Store):
assert store.read() == 1
Before — flagged A test helper constructs the function's store dependency
tests/test_read.py
from typing import Protocol
class Port(Protocol):
def read(self): ...
class Store:
def read(self):
return 1
class Service:
def __init__(self, store: Port):
self.store = store
def read(self):
return self.store.read()
def read_store(store: Port):
return store.read()
def _read():
store = Store()
return read_store(store)
def test_read():
assert _read() == 1
After — preferred The fixture dependency is passed through the helper
tests/test_read.py
from typing import Protocol
class Port(Protocol):
def read(self): ...
class Store:
def read(self):
return 1
class Service:
def __init__(self, store: Port):
self.store = store
def read(self):
return self.store.read()
def read_store(store: Port):
return store.read()
def _read(store: Port):
return read_store(store)
def test_read(store: Port):
assert _read(store) == 1
Before — flagged Repeated service operations should request the pytest fixture
tests/test_reader.py
class Reader:
def read(self):
return True
def test_first():
assert Reader().read()
def test_second():
assert Reader().read()
After — preferred A fixture owns the service lifetime without a global singleton
tests/test_reader.py
import pytest
class Reader:
def read(self):
return True
@pytest.fixture
def reader():
return Reader()
def test_first(reader):
assert reader.read()
def test_second(reader):
assert reader.read()
Before — flagged A visible plugin fixture owns the collaborator
app/__init__.py
"""Example application package."""
app/stores.py
from abc import ABC, abstractmethod
class Queue(ABC):
@abstractmethod
def retry(self) -> bool: ...
class QueueStore(Queue):
def __init__(self, resource: object) -> None:
self.resource = resource
def retry(self) -> bool:
return True
class Owner:
def __init__(self, resource: object) -> None:
self.resource = resource
def read(self) -> bool:
return True
tests/__init__.py
"""Example test package."""
tests/conftest.py
pytest_plugins = ["tests.fixtures"]
tests/fixtures.py
import pytest
from app.stores import Owner, QueueStore
@pytest.fixture
def resource() -> object:
return object()
@pytest.fixture
def owner(resource: object) -> Owner:
return Owner(resource)
@pytest.fixture
def queue_store(resource: object) -> QueueStore:
return QueueStore(resource)
tests/test_retry.py
from app.stores import Owner, QueueStore
def test_retry(owner: Owner):
queue = QueueStore(owner.resource)
queue.retry()
assert owner.read()
After — preferred Request the collaborator through pytest
app/__init__.py
"""Example application package."""
app/stores.py
from abc import ABC, abstractmethod
class Queue(ABC):
@abstractmethod
def retry(self) -> bool: ...
class QueueStore(Queue):
def __init__(self, resource: object) -> None:
self.resource = resource
def retry(self) -> bool:
return True
class Owner:
def __init__(self, resource: object) -> None:
self.resource = resource
def read(self) -> bool:
return True
tests/__init__.py
"""Example test package."""
tests/conftest.py
pytest_plugins = ["tests.fixtures"]
tests/fixtures.py
import pytest
from app.stores import Owner, QueueStore
@pytest.fixture
def resource() -> object:
return object()
@pytest.fixture
def owner(resource: object) -> Owner:
return Owner(resource)
@pytest.fixture
def queue_store(resource: object) -> QueueStore:
return QueueStore(resource)
tests/test_retry.py
def test_retry(queue_store):
assert queue_store.retry()