Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest integration #193

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ classifiers = [
dependencies = [
'exceptiongroup>=1.1.3; python_version<"3.11"',
]
[project.entry-points.pytest11]
dishka = "dishka.integrations.pytest"

[project.urls]
"Source" = "https://github.com/reagento/dishka"
Expand Down
32 changes: 32 additions & 0 deletions src/dishka/integrations/pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from inspect import Parameter
from typing import Any

import pytest

from dishka import Container
from .base import wrap_injection

CONTAINER_NAME = "dishka_container"


def dishka_fixture(name: str, cls: Any):
def temp_fixture(dishka_container):
return dishka_container.get(cls)

temp_fixture.__name__ = name
return pytest.fixture(temp_fixture)


def inject(func):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be possible to declare a own inject with a different fixture name as container name to enable different usages of dishka at the same time

i suspect pytest-asyncio is able to run on a async fixture, verification needed

additional_params = [Parameter(
name=CONTAINER_NAME,
annotation=Container,
kind=Parameter.KEYWORD_ONLY,
)]
return wrap_injection(
func=func,
remove_depends=True,
container_getter=lambda _, p: p[CONTAINER_NAME],
additional_params=additional_params,
is_async=False,
)
Empty file.
33 changes: 33 additions & 0 deletions tests/integrations/pytest/test_pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest

from dishka import FromDishka, Provider, Scope, make_container
from dishka.integrations.pytest import dishka_fixture, inject


@pytest.fixture(scope="session")
def dishka_container():
provider = Provider(scope=Scope.APP)
provider.provide(source=lambda: 42, provides=int)
return make_container(provider)


@inject
def test_inject_test(value: FromDishka[int]) -> None:
assert value == 42


@pytest.fixture()
@inject
def some_fixture(value: FromDishka[int]):
return value


def test_inject_fixture(some_fixture) -> None:
assert some_fixture == 42


explicit_fixture = dishka_fixture("explicit_fixture", int)


def test_explicit_fixture(explicit_fixture) -> None:
assert explicit_fixture == 42