Skip to content

Commit bdf459a

Browse files
committed
Only function scope supported for async fixtures
1 parent 72558a4 commit bdf459a

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ Waiting for deferreds in fixtures
8888
async/await fixtures
8989
====================
9090
``async``/``await`` fixtures can be used along with ``yield`` for normal
91-
pytest fixture semantics of setup, value, and teardown.
91+
pytest fixture semantics of setup, value, and teardown. At present only
92+
function scope is supported.
9293

9394
@pytest_twisted.async_fixture
9495
async def foo():

pytest_twisted.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ def from_generator(cls, generator):
3030
)
3131

3232

33+
class AsyncFixtureUnsupportedScopeError(Exception):
34+
@classmethod
35+
def from_scope(cls, scope):
36+
return cls(
37+
'Unsupported scope used for async fixture: {}'.format(scope)
38+
)
39+
40+
3341
class _config:
3442
external_reactor = False
3543

@@ -110,6 +118,14 @@ def __init__(self, coroutine, mark):
110118
def _marked_async_fixture(mark):
111119
@functools.wraps(pytest.fixture)
112120
def fixture(*args, **kwargs):
121+
try:
122+
scope = args[0]
123+
except IndexError:
124+
scope = kwargs.get('scope', 'function')
125+
126+
if scope != 'function':
127+
raise AsyncFixtureUnsupportedScopeError.from_scope(scope=scope)
128+
113129
def marker(f):
114130
@functools.wraps(f)
115131
def w(*args, **kwargs):
@@ -159,7 +175,7 @@ def _pytest_pyfunc_call(pyfuncitem):
159175
)
160176
else:
161177
raise UnrecognizedCoroutineMarkError.from_mark(
162-
wrapper.mark,
178+
mark=wrapper.mark,
163179
)
164180
else:
165181
arg_value = funcargs[arg]

testing/test_basic.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,54 @@ def test_succeed(foo):
345345
assert_outcomes(rr, {"passed": 2, "failed": 3})
346346

347347

348+
@skip_if_no_async_generators()
349+
def test_async_fixture_function_scope(testdir, cmd_opts):
350+
test_file = """
351+
from twisted.internet import reactor, defer
352+
import pytest
353+
import pytest_twisted
354+
355+
check_me = 0
356+
357+
@pytest_twisted.async_yield_fixture(scope="function")
358+
async def foo():
359+
global check_me
360+
361+
if check_me != 0:
362+
raise Exception('check_me already modified before fixture run')
363+
364+
check_me = 1
365+
366+
yield 42
367+
368+
if check_me != 2:
369+
raise Exception(
370+
'check_me not updated properly: {}'.format(check_me),
371+
)
372+
373+
check_me = 0
374+
375+
def test_first(foo):
376+
global check_me
377+
378+
assert check_me == 1
379+
assert foo == 42
380+
381+
check_me = 2
382+
383+
def test_second(foo):
384+
global check_me
385+
386+
assert check_me == 1
387+
assert foo == 42
388+
389+
check_me = 2
390+
"""
391+
testdir.makepyfile(test_file)
392+
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
393+
assert_outcomes(rr, {"passed": 2})
394+
395+
348396
def test_blockon_in_hook(testdir, cmd_opts, request):
349397
skip_if_reactor_not(request, "default")
350398
conftest_file = """

0 commit comments

Comments
 (0)