Skip to content

Commit

Permalink
add test for (and stop blocking) module scope fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky committed Mar 4, 2020
1 parent 3a3900f commit 9f0c454
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pytest_twisted.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ def fixture(*args, **kwargs):
except IndexError:
scope = kwargs.get('scope', 'function')

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

def decorator(f):
Expand Down
50 changes: 50 additions & 0 deletions testing/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,3 +854,53 @@ def test_succeed():
testdir.makepyfile(test_file)
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
assert "WrongReactorAlreadyInstalledError" in rr.stderr.str()


@skip_if_no_async_generators()
def test_async_fixture_module_scope(testdir, cmd_opts):
test_file = """
from twisted.internet import reactor, defer
import pytest
import pytest_twisted
check_me = 0
@pytest_twisted.async_yield_fixture(scope="module")
async def foo():
global check_me
if check_me != 0:
raise Exception('check_me already modified before fixture run')
check_me = 1
yield 42
if check_me != 3:
raise Exception(
'check_me not updated properly: {}'.format(check_me),
)
check_me = 0
def test_first(foo):
global check_me
assert check_me == 1
assert foo == 42
check_me = 2
# check_me += 1
def test_second(foo):
global check_me
assert check_me == 2
assert foo == 42
check_me = 3
# check_me += 1
"""
testdir.makepyfile(test_file)
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
assert_outcomes(rr, {"passed": 2})

0 comments on commit 9f0c454

Please sign in to comment.