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) 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.0.3.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.0.3-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di_pytest-2.0.3.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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.0.3.tar.gz
Algorithm Hash digest
SHA256 c865fe9c643ea7b21004eef734e0b5d531400505660e4ff3de512808e46663fa
MD5 93b041630a732cd72df0e6ca118fc051
BLAKE2b-256 8051a36465bfc67dc3bbb6e72935256a0db96ca25fc7541dd107dbba85d63b97

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modern_di_pytest-2.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2b95e16a664b3f3ea62f17565dc06902bda4da8765fe79c010a715301e513da2
MD5 2353153dc57a39ea5604685cca416a11
BLAKE2b-256 3c4faae885ff6fdba78d3d1eb13fcf728445eb24f19a5b9d0d808436100dc3b4

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