pytest-dev / pytest-asyncio

Asyncio support for pytest
https://pytest-asyncio.readthedocs.io
Apache License 2.0
1.4k stars 146 forks source link

Parametrizing `event_loop_policy` parametrizes all tests #796

Open aceg1k opened 6 months ago

aceg1k commented 6 months ago

Contrary to the statements in the documentation (here and here), the fixture is not only applied to all pytest-asyncio tests, but also applied to all other tests.

import asyncio
import pytest
import uvloop

@pytest.fixture(
    scope="session",
    params=(asyncio.get_event_loop_policy(), uvloop.EventLoopPolicy()),
    ids=("asyncio", "uvloop"),
)
def event_loop_policy(request):
    return request.param

@pytest.mark.asyncio
async def test_async():
    pass

def test_sync():
    pass

Output:

plugins: asyncio-0.23.5
asyncio: mode=Mode.STRICT
collected 4 items                                                                                                       

test_event_loop_policy.py::test_async[asyncio] PASSED                                                             [ 25%]
test_event_loop_policy.py::test_sync[asyncio] PASSED                                                              [ 50%]
test_event_loop_policy.py::test_async[uvloop] PASSED                                                              [ 75%]
test_event_loop_policy.py::test_sync[uvloop] PASSED                                                               [100%]
seifertm commented 2 months ago

Thanks for the report and the reproducer!

The event loop policy fixture is defined as an autouse fixture. I don't exactly recall why, but I think the reason is that it's now used by the deprecated event_loop fixture and had to be marked as autouse for backwards compatibility.

It's obviously a bug that this also parametrizes all sync tests and needs to be addressed.

However, I don't expect this issue to be fixed before v1.0 when the legacy event_loop fixture is gone.