Metadata-Version: 2.4
Name: modern-di-pytest
Version: 2.1.0
Summary: Pytest integration for modern-di — turn DI dependencies into fixtures
Keywords: dependency-injection,di,ioc-container,modern-di,pytest,fixtures,python
Author: Artur Shiriev
Author-email: Artur Shiriev <me@shiriev.ru>
License-Expression: MIT
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Framework :: Pytest
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Typing :: Typed
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Testing
Requires-Dist: modern-di>=2.25,<3
Requires-Dist: pytest>=7
Requires-Python: >=3.10, <4
Project-URL: Homepage, https://modern-di.modern-python.org
Project-URL: Documentation, https://modern-di.modern-python.org
Project-URL: Repository, https://github.com/modern-python/modern-di-pytest
Project-URL: Issues, https://github.com/modern-python/modern-di-pytest/issues
Project-URL: Changelog, https://github.com/modern-python/modern-di-pytest/releases
Description-Content-Type: text/markdown

<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)"  srcset="https://raw.githubusercontent.com/modern-python/.github/main/brand/projects/modern-di-pytest/lockup-dark.svg">
    <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/modern-python/.github/main/brand/projects/modern-di-pytest/lockup-light.svg">
    <img alt="modern-di-pytest" src="https://raw.githubusercontent.com/modern-python/.github/main/brand/projects/modern-di-pytest/lockup.png" width="420">
  </picture>
</p>

[![PyPI version](https://img.shields.io/pypi/v/modern-di-pytest.svg)](https://pypi.org/project/modern-di-pytest/)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/modern-di-pytest.svg)](https://pypi.org/project/modern-di-pytest/)
[![Downloads](https://static.pepy.tech/badge/modern-di-pytest/month)](https://pepy.tech/projects/modern-di-pytest)
[![Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)](https://github.com/modern-python/modern-di-pytest/actions/workflows/ci.yml)
[![CI](https://github.com/modern-python/modern-di-pytest/actions/workflows/ci.yml/badge.svg)](https://github.com/modern-python/modern-di-pytest/actions/workflows/ci.yml)
[![License](https://img.shields.io/github/license/modern-python/modern-di-pytest.svg)](https://github.com/modern-python/modern-di-pytest/blob/main/LICENSE)
[![GitHub stars](https://img.shields.io/github/stars/modern-python/modern-di-pytest)](https://github.com/modern-python/modern-di-pytest/stargazers)
[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![ty](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ty/main/assets/badge/v0.json)](https://github.com/astral-sh/ty)

Pytest integration for [Modern-DI](https://github.com/modern-python/modern-di) — turn any DI dependency into a pytest fixture with one line.

## Installation

```bash
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:

```python
# 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:

```python
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

```python
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`:

```python
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](https://pypi.org/project/modern-di-pytest)

## 📝 [License](LICENSE)

## Part of `modern-python`

Built on [`modern-di`](https://github.com/modern-python/modern-di), a dependency-injection framework with IoC container and scopes.

Browse the full list of templates and libraries in
[`modern-python`](https://github.com/modern-python) — see the org profile for the categorized index.
