Skip to main content

Pytest integration for Modern-DI: turn DI dependencies into pytest fixtures

Project description

"Modern-DI-Pytest"

Supported versions downloads GitHub stars

Pytest integration for Modern-DI.

Turn any DI dependency into a pytest fixture with one line.

Install

uv add --dev 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.

Part of modern-python

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

📚 Documentation

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.1.tar.gz (4.0 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.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di_pytest-2.0.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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.1.tar.gz
Algorithm Hash digest
SHA256 67a470c89f4bddb0fa13161e4f680f026e33f5e04a57e9304a1feb87c3491d69
MD5 c6fdba14a20241c92d14dfff250462fb
BLAKE2b-256 b04ce394c1322cff08679e93fb0084d60e6367bd73c6eca44ad55aec71f14a5a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modern_di_pytest-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6328ec440a1907c13208c247b14bb64ec753cc9dcc6a9f6d66f8252c660ad72b
MD5 30e85bb55c151f2f347c5237c458b85c
BLAKE2b-256 f14292312c070f8f899830cca8ce6c4e2f82c523dcbd050d4e6f53838150485a

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