Skip to content

Commit 1840838

Browse files
authored
Merge pull request #17 from chrisoaks/support-indirect-parameterization
Support indirect parameterization
2 parents 792691b + 7c1ff57 commit 1840838

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

pytest_asyncio_cooperative/fixtures.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ def function_args(func):
1515
def _get_fixture(item, arg_name, fixture=None):
1616
"""
1717
Sometimes fixture names clash with plugin fixtures.
18-
We priortise fixtures that are defined inside the user's module
18+
We prioritise fixtures that are defined inside the user's module
1919
"""
2020
if arg_name == "request":
2121
# Support parameterized fixture
22-
if fixture and fixture.params:
23-
item._request.param = item._pyfuncitem.callspec.params[fixture.argname]
22+
if fixture:
23+
try:
24+
item._request.param = item._pyfuncitem.callspec.params[fixture.argname]
25+
except AttributeError as e:
26+
pass
2427
return item._request
2528

2629
if arg_name == "self":

pytest_asyncio_cooperative/plugin.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -304,20 +304,21 @@ async def run_tests(tasks, max_tasks):
304304
activate_assert_rewrite(item)
305305

306306
# Run the tests using cooperative multitasking
307-
_run_test_loop(tasks, session, run_tests)
308-
309-
# Run failed flakey tests
310-
if flakes_to_retry:
311-
_run_test_loop(flakes_to_retry, session, run_tests)
312-
313-
# Run synchronous tests
314-
session.items = regular_items
315-
for i, item in enumerate(session.items):
316-
nextitem = session.items[i + 1] if i + 1 < len(session.items) else None
317-
item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
318-
if session.shouldfail:
319-
raise session.Failed(session.shouldfail)
320-
if session.shouldstop:
321-
raise session.Interrupted(session.shouldstop)
322-
323-
return True
307+
if not previous_collectonly:
308+
_run_test_loop(tasks, session, run_tests)
309+
310+
# Run failed flakey tests
311+
if flakes_to_retry:
312+
_run_test_loop(flakes_to_retry, session, run_tests)
313+
314+
# Run synchronous tests
315+
session.items = regular_items
316+
for i, item in enumerate(session.items):
317+
nextitem = session.items[i + 1] if i + 1 < len(session.items) else None
318+
item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
319+
if session.shouldfail:
320+
raise session.Failed(session.shouldfail)
321+
if session.shouldstop:
322+
raise session.Interrupted(session.shouldstop)
323+
324+
return True

tests/test_fixture.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@ async def test_a(tmp_path):
9595
result.assert_outcomes(passed=1)
9696

9797

98+
def test_indirect(testdir):
99+
testdir.makepyfile(
100+
"""
101+
import asyncio
102+
import pytest
103+
@pytest.fixture
104+
def to_str(request):
105+
yield str(request.param)
106+
107+
108+
@pytest.mark.asyncio_cooperative
109+
@pytest.mark.parametrize("to_str, expected", [(1, "1"), (2, "2"), (3, "3")], indirect=[
110+
"to_str"])
111+
async def test_a(to_str, expected):
112+
await asyncio.sleep(2)
113+
assert to_str == expected
114+
"""
115+
)
116+
117+
result = testdir.runpytest()
118+
result.assert_outcomes(passed=3)
119+
120+
98121
def test_fixtures(testdir):
99122
testdir.makeconftest("""""")
100123

0 commit comments

Comments
 (0)