Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions changelog.d/796.fixed.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Parametrized ``event_loop_policy`` fixtures no longer parametrize synchronous tests.
20 changes: 20 additions & 0 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
metafunc.definition
)
if specialized_item_class is None:
_remove_autouse_event_loop_policy_for_sync_tests(metafunc)
return

asyncio_marker = _resolve_asyncio_marker(metafunc.definition)
Expand Down Expand Up @@ -788,6 +789,25 @@ def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
)


def _remove_autouse_event_loop_policy_for_sync_tests(
metafunc: pytest.Metafunc,
) -> None:
fixture_name = event_loop_policy.__name__
fixtureinfo = metafunc.definition._fixtureinfo
if fixture_name in fixtureinfo.argnames:
return
if any(
fixture_name in marker.args
for marker in metafunc.definition.iter_markers(name="usefixtures")
):
return
if fixture_name in metafunc.fixturenames:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This breaks valid usage for the same reason described here #1454

metafunc.fixturenames.remove(fixture_name)
if fixture_name in fixtureinfo.names_closure:
fixtureinfo.names_closure.remove(fixture_name)
fixtureinfo.name2fixturedefs.pop(fixture_name, None)


@contextlib.contextmanager
def _temporary_event_loop(loop: AbstractEventLoop) -> Iterator[None]:
try:
Expand Down
40 changes: 40 additions & 0 deletions tests/markers/test_event_loop_policy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from textwrap import dedent

from pytest import Pytester


def test_parametrized_loop_policy_does_not_parametrize_sync_tests(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(dedent("""\
import asyncio

import pytest

class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
pass

@pytest.fixture(
params=[
asyncio.DefaultEventLoopPolicy(),
CustomEventLoopPolicy(),
],
)
def event_loop_policy(request):
return request.param

@pytest.mark.asyncio
async def test_async():
assert isinstance(
asyncio.get_event_loop_policy(),
(asyncio.DefaultEventLoopPolicy, CustomEventLoopPolicy),
)

def test_sync():
assert True
"""))

result = pytester.runpytest("--asyncio-mode=strict")

result.assert_outcomes(passed=3)
Loading