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
import pytest
class Probe: def __init__(self, connection): self.connection = connection
@pytest.fixturedef probe_factory() -> type[Probe]: return Probe
def test_connection(probe_factory: type[Probe], connection): probe = Probe(connection) assert probe.connection is connectionimport pytest
class Probe: def __init__(self, connection): self.connection = connection
@pytest.fixturedef probe_factory() -> type[Probe]: return Probe
def test_connection(probe_factory: type[Probe], connection): probe = probe_factory(connection) assert probe.connection is connectionfrom 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() == 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 read_store(store: Port): return store.read()
def _read(): store = Store() return read_store(store)
def test_read(): assert _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 read_store(store: Port): return store.read()
def _read(store: Port): return read_store(store)
def test_read(store: Port): assert _read(store) == 1class Reader: def read(self): return True
def test_first(): assert Reader().read()
def test_second(): assert Reader().read()import pytest
class Reader: def read(self): return True
@pytest.fixturedef reader(): return Reader()
def test_first(reader): assert reader.read()
def test_second(reader): assert reader.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)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()