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
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() == 1from 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() == 1from 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() == 1from 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"""Example application package."""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"""Example test package."""pytest_plugins = ["tests.fixtures"]import pytestfrom app.stores import Owner, QueueStore
@pytest.fixturedef resource() -> object: return object()
@pytest.fixturedef owner(resource: object) -> Owner: return Owner(resource)
@pytest.fixturedef queue_store(resource: object) -> QueueStore: return QueueStore(resource)from app.stores import Owner, QueueStore
def test_retry(owner: Owner): queue = QueueStore(owner.resource) queue.retry() assert owner.read()"""Example application package."""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"""Example test package."""pytest_plugins = ["tests.fixtures"]import pytestfrom app.stores import Owner, QueueStore
@pytest.fixturedef resource() -> object: return object()
@pytest.fixturedef owner(resource: object) -> Owner: return Owner(resource)
@pytest.fixturedef queue_store(resource: object) -> QueueStore: return QueueStore(resource)def test_retry(queue_store): assert queue_store.retry()