diff --git a/pytest_twisted.py b/pytest_twisted.py index 0415b1c..1bab51d 100644 --- a/pytest_twisted.py +++ b/pytest_twisted.py @@ -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): diff --git a/testing/test_basic.py b/testing/test_basic.py index 8cd7822..d888ca2 100755 --- a/testing/test_basic.py +++ b/testing/test_basic.py @@ -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})