Skip to content

Address warning about missing default fixture loop scope in tests #923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Aug 25, 2024
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
6ec9829
refactor: Set default asyncio event loop for fixtures in setup.cfg
seifertm Aug 23, 2024
81c9ad3
refactor: Addressed warning about missing default fixture scope confi…
seifertm Aug 23, 2024
f990587
refactor: Addressed warning about missing default fixture loop scope …
seifertm Aug 23, 2024
f548101
refactor: Addressed warnings about missing default fixture loop in te…
seifertm Aug 23, 2024
40fb982
refactor: Addressed warnings about missing default fixture loop in te…
seifertm Aug 23, 2024
0a32b27
refactor: Addressed warnings about missing default fixture loop scope…
seifertm Aug 23, 2024
810dad1
refactor: Addressed warnings about missing default fixture loop scope…
seifertm Aug 23, 2024
838f573
refactor: Addressed warnings about missing default fixture loop scope…
seifertm Aug 23, 2024
58b934f
refactor: test_auto_mode uses pytester, instead of testdir.
seifertm Aug 23, 2024
6006456
refactor: Addressed warnings about missing default fixture loop scope…
seifertm Aug 23, 2024
9e9d036
refactor: Replaced use of testdir with pytester in test_strict_mode.
seifertm Aug 23, 2024
9556f8b
refactor: Addressed warnings about missing default fixture loop scope…
seifertm Aug 23, 2024
9eb9cec
refactor: Addressed warnings about missing default fixture loop scope…
seifertm Aug 23, 2024
5d256b8
refactor: Addressed warnings about missing default fixture loop scope…
seifertm Aug 23, 2024
440dccd
refactor: Addressed warning about missing default fixture loop scope …
seifertm Aug 24, 2024
474a613
refactor: Addressed warnings about missing default fixture loop scope…
seifertm Aug 24, 2024
63f1400
refactor: Addressed warnings about missing default fixture loop scope…
seifertm Aug 24, 2024
45649bd
refactor: Addressed warnigs about missing default fixture loop scope …
seifertm Aug 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
class TestClassScopedLoop:
loop: asyncio.AbstractEventLoop

@pytest_asyncio.fixture(scope="class")
@pytest_asyncio.fixture(loop_scope="class")
async def my_fixture(self):
TestClassScopedLoop.loop = asyncio.get_running_loop()

1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@ python_files = test_*.py *_example.py
addopts = -rsx --tb=short
testpaths = docs tests
asyncio_mode = auto
asyncio_default_fixture_loop_scope = function
junit_family=xunit2
filterwarnings =
error
6 changes: 4 additions & 2 deletions tests/async_fixtures/test_async_fixtures_with_finalizer.py
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@

import pytest

import pytest_asyncio


@pytest.mark.asyncio(loop_scope="module")
async def test_module_with_event_loop_finalizer(port_with_event_loop_finalizer):
@@ -25,7 +27,7 @@ def event_loop():
loop.close()


@pytest.fixture(scope="module")
@pytest_asyncio.fixture(loop_scope="module", scope="module")
async def port_with_event_loop_finalizer(request):
def port_finalizer(finalizer):
async def port_afinalizer():
@@ -40,7 +42,7 @@ async def port_afinalizer():
return True


@pytest.fixture(scope="module")
@pytest_asyncio.fixture(loop_scope="module", scope="module")
async def port_with_get_event_loop_finalizer(request):
def port_finalizer(finalizer):
async def port_afinalizer():
3 changes: 2 additions & 1 deletion tests/async_fixtures/test_shared_module_fixture.py
Original file line number Diff line number Diff line change
@@ -4,12 +4,13 @@


def test_asyncio_mark_provides_package_scoped_loop_strict_mode(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
conftest=dedent(
"""\
import pytest_asyncio
@pytest_asyncio.fixture(scope="module")
@pytest_asyncio.fixture(loop_scope="module", scope="module")
async def async_shared_module_fixture():
return True
"""
4 changes: 4 additions & 0 deletions tests/hypothesis/test_base.py
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@


def test_hypothesis_given_decorator_before_asyncio_mark(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -42,6 +43,7 @@ async def test_mark_and_parametrize(x, y):


def test_can_use_explicit_event_loop_fixture(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = module")
pytester.makepyfile(
dedent(
"""\
@@ -78,6 +80,7 @@ async def test_explicit_fixture_request(event_loop, n):


def test_async_auto_marked(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -100,6 +103,7 @@ async def test_hypothesis(n: int):

def test_sync_not_auto_marked(pytester: Pytester):
"""Assert that synchronous Hypothesis functions are not marked with asyncio"""
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
12 changes: 11 additions & 1 deletion tests/markers/test_class_scope.py
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ def sample_fixture():
def test_asyncio_mark_provides_class_scoped_loop_when_applied_to_functions(
pytester: pytest.Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -56,6 +57,7 @@ async def test_this_runs_in_same_loop(self):
def test_asyncio_mark_provides_class_scoped_loop_when_applied_to_class(
pytester: pytest.Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -81,6 +83,7 @@ async def test_this_runs_in_same_loop(self):
def test_asyncio_mark_raises_when_class_scoped_is_request_without_class(
pytester: pytest.Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -101,6 +104,7 @@ async def test_has_no_surrounding_class():


def test_asyncio_mark_is_inherited_to_subclasses(pytester: pytest.Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -129,6 +133,7 @@ async def test_this_runs_in_same_loop(self):
def test_asyncio_mark_respects_the_loop_policy(
pytester: pytest.Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -166,6 +171,7 @@ async def test_does_not_use_custom_event_loop_policy():
def test_asyncio_mark_respects_parametrized_loop_policies(
pytester: pytest.Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -197,6 +203,7 @@ async def test_parametrized_loop(self, request):
def test_asyncio_mark_provides_class_scoped_loop_to_fixtures(
pytester: pytest.Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -226,6 +233,7 @@ async def test_runs_is_same_loop_as_fixture(self, my_fixture):
def test_asyncio_mark_allows_combining_class_scoped_fixture_with_function_scoped_test(
pytester: pytest.Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -237,7 +245,7 @@ def test_asyncio_mark_allows_combining_class_scoped_fixture_with_function_scoped
loop: asyncio.AbstractEventLoop
class TestMixedScopes:
@pytest_asyncio.fixture(scope="class")
@pytest_asyncio.fixture(loop_scope="class", scope="class")
async def async_fixture(self):
global loop
loop = asyncio.get_running_loop()
@@ -257,6 +265,7 @@ async def test_runs_in_different_loop_as_fixture(self, async_fixture):
def test_asyncio_mark_handles_missing_event_loop_triggered_by_fixture(
pytester: pytest.Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -292,6 +301,7 @@ async def test_does_not_fail(self, sets_event_loop_to_none, n):
def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
pytester: pytest.Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
11 changes: 11 additions & 0 deletions tests/markers/test_function_scope.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@


def test_asyncio_mark_provides_function_scoped_loop_strict_mode(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -29,6 +30,7 @@ async def test_does_not_run_in_same_loop():


def test_loop_scope_function_provides_function_scoped_event_loop(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -54,6 +56,7 @@ async def test_does_not_run_in_same_loop():


def test_raises_when_scope_and_loop_scope_arguments_are_present(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -70,6 +73,7 @@ async def test_raises():


def test_warns_when_scope_argument_is_present(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -89,6 +93,7 @@ async def test_warns():
def test_function_scope_supports_explicit_event_loop_fixture_request(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -111,6 +116,7 @@ async def test_remember_loop(event_loop):
def test_asyncio_mark_respects_the_loop_policy(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -141,6 +147,7 @@ async def test_uses_custom_event_loop_policy():
def test_asyncio_mark_respects_parametrized_loop_policies(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -178,6 +185,7 @@ async def test_parametrized_loop():
def test_asyncio_mark_provides_function_scoped_loop_to_fixtures(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -208,6 +216,7 @@ async def test_runs_is_same_loop_as_fixture(my_fixture):
def test_asyncio_mark_handles_missing_event_loop_triggered_by_fixture(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -242,6 +251,7 @@ async def test_does_not_fail(sets_event_loop_to_none, n):
def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -260,6 +270,7 @@ async def test_anything():
def test_asyncio_mark_does_not_duplicate_other_marks_in_auto_mode(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makeconftest(
dedent(
"""\
19 changes: 15 additions & 4 deletions tests/markers/test_module_scope.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@


def test_asyncio_mark_works_on_module_level(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -56,6 +57,7 @@ def sample_fixture():


def test_asyncio_mark_provides_module_scoped_loop_strict_mode(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -88,6 +90,7 @@ async def test_this_runs_in_same_loop(self):
def test_raise_when_event_loop_fixture_is_requested_in_addition_to_scoped_loop(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -109,6 +112,7 @@ async def test_remember_loop(event_loop):
def test_asyncio_mark_respects_the_loop_policy(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
custom_policy=dedent(
@@ -163,6 +167,7 @@ async def test_does_not_use_custom_event_loop_policy():
def test_asyncio_mark_respects_parametrized_loop_policies(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -194,6 +199,7 @@ async def test_parametrized_loop():
def test_asyncio_mark_provides_module_scoped_loop_to_fixtures(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -206,7 +212,7 @@ def test_asyncio_mark_provides_module_scoped_loop_to_fixtures(
loop: asyncio.AbstractEventLoop
@pytest_asyncio.fixture(scope="module")
@pytest_asyncio.fixture(loop_scope="module", scope="module")
async def my_fixture():
global loop
loop = asyncio.get_running_loop()
@@ -224,6 +230,7 @@ async def test_runs_is_same_loop_as_fixture(my_fixture):
def test_asyncio_mark_allows_combining_module_scoped_fixture_with_class_scoped_test(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -234,7 +241,7 @@ def test_asyncio_mark_allows_combining_module_scoped_fixture_with_class_scoped_t
loop: asyncio.AbstractEventLoop
@pytest_asyncio.fixture(scope="module")
@pytest_asyncio.fixture(loop_scope="module", scope="module")
async def async_fixture():
global loop
loop = asyncio.get_running_loop()
@@ -255,6 +262,7 @@ async def test_runs_in_different_loop_as_fixture(self, async_fixture):
def test_asyncio_mark_allows_combining_module_scoped_fixture_with_function_scoped_test(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_mixed_scopes=dedent(
@@ -266,7 +274,7 @@ def test_asyncio_mark_allows_combining_module_scoped_fixture_with_function_scope
loop: asyncio.AbstractEventLoop
@pytest_asyncio.fixture(scope="module")
@pytest_asyncio.fixture(loop_scope="module", scope="module")
async def async_fixture():
global loop
loop = asyncio.get_running_loop()
@@ -285,6 +293,7 @@ async def test_runs_in_different_loop_as_fixture(async_fixture):
def test_allows_combining_module_scoped_asyncgen_fixture_with_function_scoped_test(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -295,7 +304,7 @@ def test_allows_combining_module_scoped_asyncgen_fixture_with_function_scoped_te
loop: asyncio.AbstractEventLoop
@pytest_asyncio.fixture(scope="module")
@pytest_asyncio.fixture(loop_scope="module", scope="module")
async def async_fixture():
global loop
loop = asyncio.get_running_loop()
@@ -315,6 +324,7 @@ async def test_runs_in_different_loop_as_fixture(async_fixture):
def test_asyncio_mark_handles_missing_event_loop_triggered_by_fixture(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -349,6 +359,7 @@ async def test_does_not_fail(sets_event_loop_to_none, n):
def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
18 changes: 14 additions & 4 deletions tests/markers/test_package_scope.py
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
def test_asyncio_mark_provides_package_scoped_loop_strict_mode(pytester: Pytester):
package_name = pytester.path.name
subpackage_name = "subpkg"
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
shared_module=dedent(
@@ -69,6 +70,7 @@ async def test_subpackage_runs_in_different_loop():
def test_raise_when_event_loop_fixture_is_requested_in_addition_to_scoped_loop(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_raises=dedent(
@@ -90,6 +92,7 @@ async def test_remember_loop(event_loop):
def test_asyncio_mark_respects_the_loop_policy(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
conftest=dedent(
@@ -151,6 +154,7 @@ async def test_also_uses_custom_event_loop_policy():
def test_asyncio_mark_respects_parametrized_loop_policies(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_parametrization=dedent(
@@ -183,6 +187,7 @@ async def test_parametrized_loop():
def test_asyncio_mark_provides_package_scoped_loop_to_fixtures(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
package_name = pytester.path.name
pytester.makepyfile(
__init__="",
@@ -194,7 +199,7 @@ def test_asyncio_mark_provides_package_scoped_loop_to_fixtures(
from {package_name} import shared_module
@pytest_asyncio.fixture(scope="package")
@pytest_asyncio.fixture(loop_scope="package", scope="package")
async def my_fixture():
shared_module.loop = asyncio.get_running_loop()
"""
@@ -229,6 +234,7 @@ async def test_runs_in_same_loop_as_fixture(my_fixture):
def test_asyncio_mark_allows_combining_package_scoped_fixture_with_module_scoped_test(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_mixed_scopes=dedent(
@@ -240,7 +246,7 @@ def test_asyncio_mark_allows_combining_package_scoped_fixture_with_module_scoped
loop: asyncio.AbstractEventLoop
@pytest_asyncio.fixture(scope="package")
@pytest_asyncio.fixture(loop_scope="package", scope="package")
async def async_fixture():
global loop
loop = asyncio.get_running_loop()
@@ -259,6 +265,7 @@ async def test_runs_in_different_loop_as_fixture(async_fixture):
def test_asyncio_mark_allows_combining_package_scoped_fixture_with_class_scoped_test(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_mixed_scopes=dedent(
@@ -270,7 +277,7 @@ def test_asyncio_mark_allows_combining_package_scoped_fixture_with_class_scoped_
loop: asyncio.AbstractEventLoop
@pytest_asyncio.fixture(scope="package")
@pytest_asyncio.fixture(loop_scope="package", scope="package")
async def async_fixture():
global loop
loop = asyncio.get_running_loop()
@@ -290,6 +297,7 @@ async def test_runs_in_different_loop_as_fixture(self, async_fixture):
def test_asyncio_mark_allows_combining_package_scoped_fixture_with_function_scoped_test(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_mixed_scopes=dedent(
@@ -301,7 +309,7 @@ def test_asyncio_mark_allows_combining_package_scoped_fixture_with_function_scop
loop: asyncio.AbstractEventLoop
@pytest_asyncio.fixture(scope="package")
@pytest_asyncio.fixture(loop_scope="package", scope="package")
async def async_fixture():
global loop
loop = asyncio.get_running_loop()
@@ -320,6 +328,7 @@ async def test_runs_in_different_loop_as_fixture(async_fixture):
def test_asyncio_mark_handles_missing_event_loop_triggered_by_fixture(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_loop_is_none=dedent(
@@ -355,6 +364,7 @@ async def test_does_not_fail(sets_event_loop_to_none, n):
def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_module=dedent(
24 changes: 18 additions & 6 deletions tests/markers/test_session_scope.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@

def test_asyncio_mark_provides_session_scoped_loop_strict_mode(pytester: Pytester):
package_name = pytester.path.name
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
shared_module=dedent(
@@ -70,6 +71,7 @@ async def test_subpackage_runs_in_same_loop():
def test_raise_when_event_loop_fixture_is_requested_in_addition_to_scoped_loop(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_raises=dedent(
@@ -91,6 +93,7 @@ async def test_remember_loop(event_loop):
def test_asyncio_mark_respects_the_loop_policy(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
conftest=dedent(
@@ -152,6 +155,7 @@ async def test_also_uses_custom_event_loop_policy():
def test_asyncio_mark_respects_parametrized_loop_policies(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_parametrization=dedent(
@@ -185,6 +189,7 @@ def test_asyncio_mark_provides_session_scoped_loop_to_fixtures(
pytester: Pytester,
):
package_name = pytester.path.name
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
conftest=dedent(
@@ -195,7 +200,7 @@ def test_asyncio_mark_provides_session_scoped_loop_to_fixtures(
from {package_name} import shared_module
@pytest_asyncio.fixture(scope="session")
@pytest_asyncio.fixture(loop_scope="session", scope="session")
async def my_fixture():
shared_module.loop = asyncio.get_running_loop()
"""
@@ -234,6 +239,7 @@ async def test_runs_in_same_loop_as_fixture(my_fixture):
def test_asyncio_mark_allows_combining_session_scoped_fixture_with_package_scoped_test(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_mixed_scopes=dedent(
@@ -245,7 +251,7 @@ def test_asyncio_mark_allows_combining_session_scoped_fixture_with_package_scope
loop: asyncio.AbstractEventLoop
@pytest_asyncio.fixture(scope="session")
@pytest_asyncio.fixture(loop_scope="session", scope="session")
async def async_fixture():
global loop
loop = asyncio.get_running_loop()
@@ -264,6 +270,7 @@ async def test_runs_in_different_loop_as_fixture(async_fixture):
def test_asyncio_mark_allows_combining_session_scoped_fixture_with_module_scoped_test(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_mixed_scopes=dedent(
@@ -275,7 +282,7 @@ def test_asyncio_mark_allows_combining_session_scoped_fixture_with_module_scoped
loop: asyncio.AbstractEventLoop
@pytest_asyncio.fixture(scope="session")
@pytest_asyncio.fixture(loop_scope="session", scope="session")
async def async_fixture():
global loop
loop = asyncio.get_running_loop()
@@ -294,6 +301,7 @@ async def test_runs_in_different_loop_as_fixture(async_fixture):
def test_asyncio_mark_allows_combining_session_scoped_fixture_with_class_scoped_test(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_mixed_scopes=dedent(
@@ -305,7 +313,7 @@ def test_asyncio_mark_allows_combining_session_scoped_fixture_with_class_scoped_
loop: asyncio.AbstractEventLoop
@pytest_asyncio.fixture(scope="session")
@pytest_asyncio.fixture(loop_scope="session", scope="session")
async def async_fixture():
global loop
loop = asyncio.get_running_loop()
@@ -325,6 +333,7 @@ async def test_runs_in_different_loop_as_fixture(self, async_fixture):
def test_asyncio_mark_allows_combining_session_scoped_fixture_with_function_scoped_test(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_mixed_scopes=dedent(
@@ -336,7 +345,7 @@ def test_asyncio_mark_allows_combining_session_scoped_fixture_with_function_scop
loop: asyncio.AbstractEventLoop
@pytest_asyncio.fixture(scope="session")
@pytest_asyncio.fixture(loop_scope="session", scope="session")
async def async_fixture():
global loop
loop = asyncio.get_running_loop()
@@ -355,6 +364,7 @@ async def test_runs_in_different_loop_as_fixture(async_fixture):
def test_allows_combining_session_scoped_asyncgen_fixture_with_function_scoped_test(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_mixed_scopes=dedent(
@@ -366,7 +376,7 @@ def test_allows_combining_session_scoped_asyncgen_fixture_with_function_scoped_t
loop: asyncio.AbstractEventLoop
@pytest_asyncio.fixture(scope="session")
@pytest_asyncio.fixture(loop_scope="session", scope="session")
async def async_fixture():
global loop
loop = asyncio.get_running_loop()
@@ -386,6 +396,7 @@ async def test_runs_in_different_loop_as_fixture(async_fixture):
def test_asyncio_mark_handles_missing_event_loop_triggered_by_fixture(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -420,6 +431,7 @@ async def test_does_not_fail(sets_event_loop_to_none, n):
def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
53 changes: 34 additions & 19 deletions tests/modes/test_auto_mode.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from textwrap import dedent

from pytest import Pytester

def test_auto_mode_cmdline(testdir):
testdir.makepyfile(

def test_auto_mode_cmdline(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import asyncio
@@ -15,12 +18,21 @@ async def test_a():
"""
)
)
result = testdir.runpytest("--asyncio-mode=auto")
result = pytester.runpytest("--asyncio-mode=auto")
result.assert_outcomes(passed=1)


def test_auto_mode_cfg(testdir):
testdir.makepyfile(
def test_auto_mode_cfg(pytester: Pytester):
pytester.makeini(
dedent(
"""\
[pytest]
asyncio_default_fixture_loop_scope = function
asyncio_mode = auto
"""
)
)
pytester.makepyfile(
dedent(
"""\
import asyncio
@@ -33,13 +45,13 @@ async def test_a():
"""
)
)
testdir.makefile(".ini", pytest="[pytest]\nasyncio_mode = auto\n")
result = testdir.runpytest()
result = pytester.runpytest("--asyncio-mode=auto")
result.assert_outcomes(passed=1)


def test_auto_mode_async_fixture(testdir):
testdir.makepyfile(
def test_auto_mode_async_fixture(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import asyncio
@@ -58,12 +70,13 @@ async def test_a(fixture_a):
"""
)
)
result = testdir.runpytest("--asyncio-mode=auto")
result = pytester.runpytest("--asyncio-mode=auto")
result.assert_outcomes(passed=1)


def test_auto_mode_method_fixture(testdir):
testdir.makepyfile(
def test_auto_mode_method_fixture(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import asyncio
@@ -85,12 +98,13 @@ async def test_a(self, fixture_a):
"""
)
)
result = testdir.runpytest("--asyncio-mode=auto")
result = pytester.runpytest("--asyncio-mode=auto")
result.assert_outcomes(passed=1)


def test_auto_mode_static_method(testdir):
testdir.makepyfile(
def test_auto_mode_static_method(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import asyncio
@@ -106,12 +120,13 @@ async def test_a():
"""
)
)
result = testdir.runpytest("--asyncio-mode=auto")
result = pytester.runpytest("--asyncio-mode=auto")
result.assert_outcomes(passed=1)


def test_auto_mode_static_method_fixture(testdir):
testdir.makepyfile(
def test_auto_mode_static_method_fixture(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import asyncio
@@ -135,5 +150,5 @@ async def test_a(fixture_a):
"""
)
)
result = testdir.runpytest("--asyncio-mode=auto")
result = pytester.runpytest("--asyncio-mode=auto")
result.assert_outcomes(passed=1)
46 changes: 30 additions & 16 deletions tests/modes/test_strict_mode.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from textwrap import dedent

from pytest import Pytester

def test_strict_mode_cmdline(testdir):
testdir.makepyfile(

def test_strict_mode_cmdline(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import asyncio
@@ -16,12 +19,21 @@ async def test_a():
"""
)
)
result = testdir.runpytest("--asyncio-mode=strict")
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)


def test_strict_mode_cfg(testdir):
testdir.makepyfile(
def test_strict_mode_cfg(pytester: Pytester):
pytester.makeini(
dedent(
"""\
[pytest]
asyncio_default_fixture_loop_scope = function
asyncio_mode = strict
"""
)
)
pytester.makepyfile(
dedent(
"""\
import asyncio
@@ -35,13 +47,13 @@ async def test_a():
"""
)
)
testdir.makefile(".ini", pytest="[pytest]\nasyncio_mode = strict\n")
result = testdir.runpytest()
result = pytester.runpytest()
result.assert_outcomes(passed=1)


def test_strict_mode_method_fixture(testdir):
testdir.makepyfile(
def test_strict_mode_method_fixture(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import asyncio
@@ -64,12 +76,13 @@ async def test_a(self, fixture_a):
"""
)
)
result = testdir.runpytest("--asyncio-mode=auto")
result = pytester.runpytest("--asyncio-mode=auto")
result.assert_outcomes(passed=1)


def test_strict_mode_ignores_unmarked_coroutine(testdir):
testdir.makepyfile(
def test_strict_mode_ignores_unmarked_coroutine(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import pytest
@@ -79,13 +92,14 @@ async def test_anything():
"""
)
)
result = testdir.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(skipped=1, warnings=1)
result.stdout.fnmatch_lines(["*async def functions are not natively supported*"])


def test_strict_mode_ignores_unmarked_fixture(testdir):
testdir.makepyfile(
def test_strict_mode_ignores_unmarked_fixture(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import pytest
@@ -100,7 +114,7 @@ async def test_anything(any_fixture):
"""
)
)
result = testdir.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(skipped=1, warnings=2)
result.stdout.fnmatch_lines(
[
8 changes: 5 additions & 3 deletions tests/test_asyncio_fixture.py
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
from textwrap import dedent

import pytest
from pytest import Pytester

import pytest_asyncio

@@ -43,8 +44,9 @@ async def test_fixture_with_params(fixture_with_params):


@pytest.mark.parametrize("mode", ("auto", "strict"))
def test_sync_function_uses_async_fixture(testdir, mode):
testdir.makepyfile(
def test_sync_function_uses_async_fixture(pytester: Pytester, mode):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import pytest_asyncio
@@ -60,5 +62,5 @@ def test_sync_function_uses_async_fixture(always_true):
"""
)
)
result = testdir.runpytest(f"--asyncio-mode={mode}")
result = pytester.runpytest(f"--asyncio-mode={mode}")
result.assert_outcomes(passed=1)
2 changes: 2 additions & 0 deletions tests/test_doctest.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@


def test_plugin_does_not_interfere_with_doctest_collection(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
'''\
@@ -20,6 +21,7 @@ def any_function():


def test_plugin_does_not_interfere_with_doctest_textfile_collection(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makefile(".txt", "") # collected as DoctestTextfile
pytester.makepyfile(
__init__="",
6 changes: 6 additions & 0 deletions tests/test_event_loop_fixture_finalizer.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@


def test_event_loop_fixture_finalizer_returns_fresh_loop_after_test(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -36,6 +37,7 @@ def test_2():
def test_event_loop_fixture_finalizer_handles_loop_set_to_none_sync(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -53,6 +55,7 @@ def test_sync(event_loop):
def test_event_loop_fixture_finalizer_handles_loop_set_to_none_async_without_fixture(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -72,6 +75,7 @@ async def test_async_without_explicit_fixture_request():
def test_event_loop_fixture_finalizer_handles_loop_set_to_none_async_with_fixture(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -94,6 +98,7 @@ async def test_async_with_explicit_fixture_request(event_loop):
def test_event_loop_fixture_finalizer_raises_warning_when_fixture_leaves_loop_unclosed(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -121,6 +126,7 @@ async def test_ends_with_unclosed_loop():
def test_event_loop_fixture_finalizer_raises_warning_when_test_leaves_loop_unclosed(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
7 changes: 7 additions & 0 deletions tests/test_explicit_event_loop_fixture_request.py
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
def test_emit_warning_when_event_loop_is_explicitly_requested_in_coroutine(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -27,6 +28,7 @@ async def test_coroutine_emits_warning(event_loop):
def test_emit_warning_when_event_loop_is_explicitly_requested_in_coroutine_method(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -49,6 +51,7 @@ async def test_coroutine_emits_warning(self, event_loop):
def test_emit_warning_when_event_loop_is_explicitly_requested_in_coroutine_staticmethod(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -72,6 +75,7 @@ async def test_coroutine_emits_warning(event_loop):
def test_emit_warning_when_event_loop_is_explicitly_requested_in_coroutine_fixture(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -98,6 +102,7 @@ async def test_uses_fixture(emits_warning):
def test_emit_warning_when_event_loop_is_explicitly_requested_in_async_gen_fixture(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -124,6 +129,7 @@ async def test_uses_fixture(emits_warning):
def test_does_not_emit_warning_when_event_loop_is_explicitly_requested_in_sync_function(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -141,6 +147,7 @@ def test_uses_fixture(event_loop):
def test_does_not_emit_warning_when_event_loop_is_explicitly_requested_in_sync_fixture(
pytester: Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
3 changes: 3 additions & 0 deletions tests/test_import.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@


def test_import_warning_does_not_cause_internal_error(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -19,6 +20,7 @@ async def test_errors_out():


def test_import_warning_in_package_does_not_cause_internal_error(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__=dedent(
"""\
@@ -37,6 +39,7 @@ async def test_errors_out():


def test_does_not_import_unrelated_packages(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pkg_dir = pytester.mkpydir("mypkg")
pkg_dir.joinpath("__init__.py").write_text(
dedent(
4 changes: 4 additions & 0 deletions tests/test_is_async_test.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@


def test_returns_false_for_sync_item(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -28,6 +29,7 @@ def pytest_collection_modifyitems(items):


def test_returns_true_for_marked_coroutine_item_in_strict_mode(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -53,6 +55,7 @@ def pytest_collection_modifyitems(items):


def test_returns_false_for_unmarked_coroutine_item_in_strict_mode(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -77,6 +80,7 @@ def pytest_collection_modifyitems(items):


def test_returns_true_for_unmarked_coroutine_item_in_auto_mode(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
7 changes: 5 additions & 2 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ async def test_asyncio_marker():


def test_asyncio_marker_compatibility_with_xfail(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -45,6 +46,7 @@ async def test_asyncio_marker_fail():


def test_asyncio_auto_mode_compatibility_with_xfail(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -101,8 +103,9 @@ async def test_event_loop_before_fixture(self, loop):
assert await loop.run_in_executor(None, self.foo) == 1


def test_invalid_asyncio_mode(testdir):
result = testdir.runpytest("-o", "asyncio_mode=True")
def test_invalid_asyncio_mode(pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
result = pytester.runpytest("-o", "asyncio_mode=True")
result.stderr.no_fnmatch_line("INTERNALERROR> *")
result.stderr.fnmatch_lines(
"ERROR: 'True' is not a valid asyncio_mode. Valid modes: auto, strict."
7 changes: 7 additions & 0 deletions tests/test_skips.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@


def test_asyncio_strict_mode_skip(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -22,6 +23,7 @@ async def test_no_warning_on_skip():


def test_asyncio_auto_mode_skip(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -39,6 +41,7 @@ async def test_no_warning_on_skip():


def test_asyncio_strict_mode_module_level_skip(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -57,6 +60,7 @@ async def test_is_skipped():


def test_asyncio_auto_mode_module_level_skip(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -74,6 +78,7 @@ async def test_is_skipped():


def test_asyncio_auto_mode_wrong_skip_usage(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -91,6 +96,7 @@ async def test_is_skipped():


def test_unittest_skiptest_compatibility(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
@@ -108,6 +114,7 @@ async def test_is_skipped():


def test_skip_in_module_does_not_skip_package(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
__init__="",
test_skip=dedent(