-
Notifications
You must be signed in to change notification settings - Fork 137
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
Rebind instance method fixtures to the same instance as the test #807
Changes from 1 commit
6242318
04fe34e
677a4ec
805f2fb
83a82c7
c70258a
77c2bff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,14 @@ | |
import sys | ||
from collections.abc import Iterator | ||
from contextlib import ExitStack, contextmanager | ||
from inspect import isasyncgenfunction, iscoroutinefunction | ||
from inspect import isasyncgenfunction, iscoroutinefunction, ismethod | ||
from typing import Any, cast | ||
|
||
import pytest | ||
import sniffio | ||
from _pytest.fixtures import SubRequest | ||
from _pytest.outcomes import Exit | ||
from pytest import FixtureDef, FixtureRequest | ||
|
||
from ._core._eventloop import get_all_backends, get_async_backend | ||
from ._core._exceptions import iterate_exceptions | ||
|
@@ -70,27 +72,45 @@ def pytest_configure(config: Any) -> None: | |
) | ||
|
||
|
||
def pytest_fixture_setup(fixturedef: Any, request: Any) -> None: | ||
def wrapper(*args, anyio_backend, **kwargs): # type: ignore[no-untyped-def] | ||
def pytest_fixture_setup(fixturedef: FixtureDef, request: FixtureRequest) -> None: | ||
def wrapper( | ||
*args: Any, anyio_backend: Any, request: SubRequest, **kwargs: Any | ||
) -> Any: | ||
# Rebind any fixture methods to the request instance | ||
if ( | ||
request.instance | ||
and ismethod(func) | ||
and type(func.__self__) is type(request.instance) | ||
): | ||
local_func = func.__func__.__get__(request.instance) | ||
else: | ||
local_func = func | ||
|
||
backend_name, backend_options = extract_backend_and_options(anyio_backend) | ||
if has_backend_arg: | ||
kwargs["anyio_backend"] = anyio_backend | ||
|
||
if has_request_arg: | ||
kwargs["request"] = anyio_backend | ||
Comment on lines
+93
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This overwrites the @pytest.fixture(params=['foo', 'bar'])
async def dummy_fixture(
request: pytest.FixtureRequest,
):
if request.param == 'foo':
do_this()
else:
do_that() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this was missed in the review. I'll make a fix promptly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've released a fix now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! |
||
|
||
with get_runner(backend_name, backend_options) as runner: | ||
if isasyncgenfunction(func): | ||
yield from runner.run_asyncgen_fixture(func, kwargs) | ||
if isasyncgenfunction(local_func): | ||
yield from runner.run_asyncgen_fixture(local_func, kwargs) | ||
else: | ||
yield runner.run_fixture(func, kwargs) | ||
yield runner.run_fixture(local_func, kwargs) | ||
|
||
# Only apply this to coroutine functions and async generator functions in requests | ||
# that involve the anyio_backend fixture | ||
func = fixturedef.func | ||
if isasyncgenfunction(func) or iscoroutinefunction(func): | ||
if "anyio_backend" in request.fixturenames: | ||
has_backend_arg = "anyio_backend" in fixturedef.argnames | ||
fixturedef.func = wrapper | ||
if not has_backend_arg: | ||
fixturedef.argnames += ("anyio_backend",) | ||
fixturedef.func = wrapper # type: ignore[misc] | ||
|
||
if not (has_backend_arg := "anyio_backend" in fixturedef.argnames): | ||
fixturedef.argnames += ("anyio_backend",) # type: ignore[misc] | ||
|
||
if not (has_request_arg := "request" in fixturedef.argnames): | ||
fixturedef.argnames += ("request",) # type: ignore[misc] | ||
|
||
|
||
@pytest.hookimpl(tryfirst=True) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not just unittest style tests, it also applies to class based pytest style tests