From 044358b0e411951f9270d921dc8565205d447eab Mon Sep 17 00:00:00 2001 From: Michael Seifert Date: Tue, 3 Oct 2023 15:53:07 +0200 Subject: [PATCH] [refactor] Run parametrized loop test inside Pytester to prevent the event loop implementation from polluting the test environment. Signed-off-by: Michael Seifert --- .../async_fixtures/test_parametrized_loop.py | 57 ++++++++++++------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/tests/async_fixtures/test_parametrized_loop.py b/tests/async_fixtures/test_parametrized_loop.py index 2fb8befa..2bdbe5e8 100644 --- a/tests/async_fixtures/test_parametrized_loop.py +++ b/tests/async_fixtures/test_parametrized_loop.py @@ -1,31 +1,46 @@ -import asyncio +from textwrap import dedent -import pytest +from pytest import Pytester -TESTS_COUNT = 0 +def test_event_loop_parametrization(pytester: Pytester): + pytester.makepyfile( + dedent( + """\ + import asyncio -def teardown_module(): - # parametrized 2 * 2 times: 2 for 'event_loop' and 2 for 'fix' - assert TESTS_COUNT == 4 + import pytest + import pytest_asyncio + TESTS_COUNT = 0 -@pytest.fixture(scope="module", params=[1, 2]) -def event_loop(request): - request.param - loop = asyncio.new_event_loop() - yield loop - loop.close() + def teardown_module(): + # parametrized 2 * 2 times: 2 for 'event_loop' and 2 for 'fix' + assert TESTS_COUNT == 4 -@pytest.fixture(params=["a", "b"]) -async def fix(request): - await asyncio.sleep(0) - return request.param + @pytest.fixture(scope="module", params=[1, 2]) + def event_loop(request): + request.param + loop = asyncio.new_event_loop() + yield loop + loop.close() -@pytest.mark.asyncio -async def test_parametrized_loop(fix): - await asyncio.sleep(0) - global TESTS_COUNT - TESTS_COUNT += 1 + + @pytest_asyncio.fixture(params=["a", "b"]) + async def fix(request): + await asyncio.sleep(0) + return request.param + + + @pytest.mark.asyncio + async def test_parametrized_loop(fix): + await asyncio.sleep(0) + global TESTS_COUNT + TESTS_COUNT += 1 + """ + ) + ) + result = pytester.runpytest_subprocess("--asyncio-mode=strict") + result.assert_outcomes(passed=4)