Skip to main content

Pytest integration for modern-di — turn DI dependencies into fixtures

Project description

modern-di-pytest

PyPI version Supported Python versions Downloads Coverage CI License GitHub stars uv Ruff ty

Pytest integration for Modern-DI — turn any DI dependency into a pytest fixture with one line.

Installation

uv add --dev modern-di-pytest      # or: pip install modern-di-pytest

Usage

The user owns the root container fixture. Pick whatever pytest scope fits the test suite:

# conftest.py
import typing

import modern_di
import pytest
from modern_di_pytest import expose, modern_di_fixture

from app import ioc
from app.ioc import Dependencies
from app.services import EmailClient


@pytest.fixture
def di_container() -> typing.Iterator[modern_di.Container]:
    with modern_di.Container(groups=ioc.ALL_GROUPS, validate=True) as container:
        yield container


# Bulk: every Provider on each group becomes a pytest fixture
# named after the class attribute. Pass several groups in one call.
expose(Dependencies)

# Manual: turn a single type or Provider into a named fixture.
email_client = modern_di_fixture(EmailClient)

Tests then receive resolved dependencies by name:

from app.services import EmailClient, UserService


def test_listing(user_service: UserService) -> None:  # generated by expose(Dependencies)
    assert user_service.list_users() == []


def test_email(email_client: EmailClient) -> None:    # generated manually
    email_client.send("hi")

Pointing a fixture at a child container

import typing

import modern_di
import pytest
from modern_di_pytest import modern_di_fixture

from app.services import UserService


@pytest.fixture
def request_container(
    di_container: modern_di.Container,
) -> typing.Iterator[modern_di.Container]:
    with di_container.build_child_container(scope=modern_di.Scope.REQUEST) as container:
        yield container


request_user_service = modern_di_fixture(
    UserService, container_fixture="request_container"
)

Overrides

Use Container.override() directly — modern-di already ships a first-class override mechanism backed by a tree-shared OverridesRegistry:

import modern_di

from app.ioc import Dependencies
from app.services import UserService
from tests.fakes import FakeRepo


def test_with_override(
    di_container: modern_di.Container,
    user_service: UserService,
) -> None:
    di_container.override(Dependencies.user_repo, FakeRepo())
    try:
        assert user_service.list_users() == []
    finally:
        di_container.reset_override(Dependencies.user_repo)

API

modern_di_fixture(dependency, *, container_fixture="di_container", name=None, pytest_scope="function")

Turn a single dependency into a pytest fixture. dependency is either a type (resolved via container.resolve) or a Provider (resolved via container.resolve_provider). The returned object is a real pytest fixture — assign it to a module-level name and pytest will collect it.

expose(*groups, container_fixture="di_container", pytest_scope="function", module=None)

Walk each Group subclass in groups and inject one pytest fixture per Provider class attribute into the caller's module. Fixture names equal the class-attribute names. Non-Provider class attributes are skipped. A duplicate attribute name across groups raises ValueError. Pass module= explicitly when stack introspection cannot identify the caller.

📦 PyPI

📝 License

Part of modern-python

Built on modern-di, a dependency-injection framework with IoC container and scopes.

Browse the full list of templates and libraries in modern-python — see the org profile for the categorized index.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modern_di_pytest-2.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modern_di_pytest-2.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file modern_di_pytest-2.1.0.tar.gz.

File metadata

  • Download URL: modern_di_pytest-2.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for modern_di_pytest-2.1.0.tar.gz
Algorithm Hash digest
SHA256 1a36d3c4ac3efc8cb0ba6e1d95f90652353f83d62abf283cc3c8f0dc44fa961d
MD5 b1ebf997a1d10ae366d62263d1ee0efd
BLAKE2b-256 dcc7e7d7c96e4897ac2eaf1c17d42cc85688ee9c9c12e5080134ee8200d48f73

See more details on using hashes here.

File details

Details for the file modern_di_pytest-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: modern_di_pytest-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for modern_di_pytest-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d0460fd779bb2eab912888b8bd79549141dbbe52eece4ea2bfed38cc31461c63
MD5 9b977a37f840b1a93736b68c4f7f6d2a
BLAKE2b-256 c0f24ac0e34ae1a10b5d1fa4592dda1e07044e2770867d160ac009544e6d7293

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page