Skip to content
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

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

Merged
merged 18 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
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
Expand Up @@ -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()

Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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
Expand Up @@ -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):
Expand All @@ -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():
Expand All @@ -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():
Expand Down
3 changes: 2 additions & 1 deletion tests/async_fixtures/test_shared_module_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down
4 changes: 4 additions & 0 deletions tests/hypothesis/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand All @@ -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(
"""\
Expand Down
12 changes: 11 additions & 1 deletion tests/markers/test_class_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""\
Expand All @@ -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(
"""\
Expand All @@ -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(
"""\
Expand All @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand All @@ -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()
Expand All @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand Down
11 changes: 11 additions & 0 deletions tests/markers/test_function_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""\
Expand All @@ -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(
"""\
Expand All @@ -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(
"""\
Expand All @@ -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(
"""\
Expand All @@ -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(
"""\
Expand All @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand All @@ -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(
"""\
Expand Down
19 changes: 15 additions & 4 deletions tests/markers/test_module_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand All @@ -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(
Expand Down Expand Up @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand All @@ -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()
Expand All @@ -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(
"""\
Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -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()
Expand All @@ -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(
"""\
Expand All @@ -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()
Expand All @@ -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(
"""\
Expand Down Expand Up @@ -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(
"""\
Expand Down
Loading