Skip to content

Commit 9f0c454

Browse files
committed
add test for (and stop blocking) module scope fixtures
#56
1 parent 3a3900f commit 9f0c454

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

pytest_twisted.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ def fixture(*args, **kwargs):
143143
except IndexError:
144144
scope = kwargs.get('scope', 'function')
145145

146-
if scope != 'function':
146+
if scope not in ['function', 'module']:
147+
# TODO: add test for session scope (and that's it, right?)
148+
# then remove this and update docs
147149
raise AsyncFixtureUnsupportedScopeError.from_scope(scope=scope)
148150

149151
def decorator(f):

testing/test_basic.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,3 +854,53 @@ def test_succeed():
854854
testdir.makepyfile(test_file)
855855
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
856856
assert "WrongReactorAlreadyInstalledError" in rr.stderr.str()
857+
858+
859+
@skip_if_no_async_generators()
860+
def test_async_fixture_module_scope(testdir, cmd_opts):
861+
test_file = """
862+
from twisted.internet import reactor, defer
863+
import pytest
864+
import pytest_twisted
865+
866+
check_me = 0
867+
868+
@pytest_twisted.async_yield_fixture(scope="module")
869+
async def foo():
870+
global check_me
871+
872+
if check_me != 0:
873+
raise Exception('check_me already modified before fixture run')
874+
875+
check_me = 1
876+
877+
yield 42
878+
879+
if check_me != 3:
880+
raise Exception(
881+
'check_me not updated properly: {}'.format(check_me),
882+
)
883+
884+
check_me = 0
885+
886+
def test_first(foo):
887+
global check_me
888+
889+
assert check_me == 1
890+
assert foo == 42
891+
892+
check_me = 2
893+
# check_me += 1
894+
895+
def test_second(foo):
896+
global check_me
897+
898+
assert check_me == 2
899+
assert foo == 42
900+
901+
check_me = 3
902+
# check_me += 1
903+
"""
904+
testdir.makepyfile(test_file)
905+
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
906+
assert_outcomes(rr, {"passed": 2})

0 commit comments

Comments
 (0)