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 Dependencies becomes a pytest fixture
# named after the class attribute.
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(group, *, container_fixture="di_container", pytest_scope="function", module=None)

Walk group (a Group subclass) 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. Pass module= explicitly when stack introspection cannot identify the caller.

📚 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-0.1.0.tar.gz (4.4 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-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di_pytest-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","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-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ed258af8514b96439100232734b761c410957ec455c226284c5f396f5334d96e
MD5 17e95f508e8a86558ce20677e1c8e30b
BLAKE2b-256 34f7fcb9b7ef2f61cc275341359d906bcc77b5fef4d6abeedf61d6656eab10dd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modern_di_pytest-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","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-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b439c8ac81b45b5e3c08592ce485191e784ccd737245faa7c92b962ce43b7ad9
MD5 40353226f7bb4c33da18121e1aa01565
BLAKE2b-256 369283cf79b62faf0557a724dccc36e49d5c6641e61859e594d41f9acfe88bf8

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