Skip to content

Commit 044358b

Browse files
committed
[refactor] Run parametrized loop test inside Pytester to prevent the event loop implementation from polluting the test environment.
Signed-off-by: Michael Seifert <[email protected]>
1 parent 009648a commit 044358b

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed
+36-21
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,46 @@
1-
import asyncio
1+
from textwrap import dedent
22

3-
import pytest
3+
from pytest import Pytester
44

5-
TESTS_COUNT = 0
65

6+
def test_event_loop_parametrization(pytester: Pytester):
7+
pytester.makepyfile(
8+
dedent(
9+
"""\
10+
import asyncio
711
8-
def teardown_module():
9-
# parametrized 2 * 2 times: 2 for 'event_loop' and 2 for 'fix'
10-
assert TESTS_COUNT == 4
12+
import pytest
13+
import pytest_asyncio
1114
15+
TESTS_COUNT = 0
1216
13-
@pytest.fixture(scope="module", params=[1, 2])
14-
def event_loop(request):
15-
request.param
16-
loop = asyncio.new_event_loop()
17-
yield loop
18-
loop.close()
1917
18+
def teardown_module():
19+
# parametrized 2 * 2 times: 2 for 'event_loop' and 2 for 'fix'
20+
assert TESTS_COUNT == 4
2021
21-
@pytest.fixture(params=["a", "b"])
22-
async def fix(request):
23-
await asyncio.sleep(0)
24-
return request.param
2522
23+
@pytest.fixture(scope="module", params=[1, 2])
24+
def event_loop(request):
25+
request.param
26+
loop = asyncio.new_event_loop()
27+
yield loop
28+
loop.close()
2629
27-
@pytest.mark.asyncio
28-
async def test_parametrized_loop(fix):
29-
await asyncio.sleep(0)
30-
global TESTS_COUNT
31-
TESTS_COUNT += 1
30+
31+
@pytest_asyncio.fixture(params=["a", "b"])
32+
async def fix(request):
33+
await asyncio.sleep(0)
34+
return request.param
35+
36+
37+
@pytest.mark.asyncio
38+
async def test_parametrized_loop(fix):
39+
await asyncio.sleep(0)
40+
global TESTS_COUNT
41+
TESTS_COUNT += 1
42+
"""
43+
)
44+
)
45+
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
46+
result.assert_outcomes(passed=4)

0 commit comments

Comments
 (0)