Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions changelog.d/1275.downstream.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tests are run in the same pytest process, instead of spawning a subprocess with `pytest.Pytester.runpytest_subprocess`. This prevents the test suite from accidentally using a system installation of pytest-asyncio, which could result in test errors.
2 changes: 1 addition & 1 deletion tests/hypothesis/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def test_mark_inner(n):
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1)


Expand Down
27 changes: 21 additions & 6 deletions tests/markers/test_class_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import asyncio
import sys
from textwrap import dedent

import pytest
Expand Down Expand Up @@ -143,8 +144,15 @@ async def test_does_not_use_custom_event_loop_policy():
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=2)
pytest_args = ["--asyncio-mode=strict"]
if sys.version_info >= (3, 14):
pytest_args.extend(["-W", "default"])
result = pytester.runpytest(*pytest_args)
if sys.version_info >= (3, 14):
result.assert_outcomes(passed=2, warnings=3)
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
else:
result.assert_outcomes(passed=2)


def test_asyncio_mark_respects_parametrized_loop_policies(
Expand Down Expand Up @@ -175,8 +183,15 @@ async def test_parametrized_loop(self, request):
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(passed=2)
pytest_args = ["--asyncio-mode=strict"]
if sys.version_info >= (3, 14):
pytest_args.extend(["-W", "default"])
result = pytester.runpytest(*pytest_args)
if sys.version_info >= (3, 14):
result.assert_outcomes(passed=2, warnings=2)
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
else:
result.assert_outcomes(passed=2)


def test_asyncio_mark_provides_class_scoped_loop_to_fixtures(
Expand Down Expand Up @@ -205,7 +220,7 @@ async def test_runs_is_same_loop_as_fixture(self, my_fixture):
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)


Expand Down Expand Up @@ -293,5 +308,5 @@ async def test_anything(self):
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(warnings=0, passed=1)
25 changes: 20 additions & 5 deletions tests/markers/test_function_scope.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
from textwrap import dedent

from pytest import Pytester
Expand Down Expand Up @@ -87,7 +88,7 @@ async def test_warns():
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "--assert=plain")
result = pytester.runpytest("--asyncio-mode=strict", "-W", "default")
result.assert_outcomes(passed=1, warnings=1)
result.stdout.fnmatch_lines("*DeprecationWarning*")

Expand Down Expand Up @@ -119,8 +120,15 @@ async def test_uses_custom_event_loop_policy():
"""
),
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
pytest_args = ["--asyncio-mode=strict"]
if sys.version_info >= (3, 14):
pytest_args.extend(["-W", "default"])
result = pytester.runpytest(*pytest_args)
if sys.version_info >= (3, 14):
result.assert_outcomes(passed=1, warnings=2)
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
else:
result.assert_outcomes(passed=1)


def test_asyncio_mark_respects_parametrized_loop_policies(
Expand Down Expand Up @@ -157,8 +165,15 @@ async def test_parametrized_loop():
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=2)
pytest_args = ["--asyncio-mode=strict"]
if sys.version_info >= (3, 14):
pytest_args.extend(["-W", "default"])
result = pytester.runpytest(*pytest_args)
if sys.version_info >= (3, 14):
result.assert_outcomes(passed=2, warnings=3)
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
else:
result.assert_outcomes(passed=2)


def test_asyncio_mark_provides_function_scoped_loop_to_fixtures(
Expand Down
12 changes: 8 additions & 4 deletions tests/markers/test_invalid_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
def test_no_error_when_scope_passed_as_sole_keyword_argument(
pytester: pytest.Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
Expand All @@ -19,14 +20,15 @@ async def test_anything():
"""
)
)
result = pytester.runpytest_subprocess()
result = pytester.runpytest("--assert=plain")
result.assert_outcomes(passed=1)
result.stdout.no_fnmatch_line("*ValueError*")


def test_error_when_scope_passed_as_positional_argument(
pytester: pytest.Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
Expand All @@ -38,7 +40,7 @@ async def test_anything():
"""
)
)
result = pytester.runpytest_subprocess()
result = pytester.runpytest("--assert=plain")
result.assert_outcomes(errors=1)
result.stdout.fnmatch_lines(
["*ValueError: mark.asyncio accepts only a keyword argument*"]
Expand All @@ -48,6 +50,7 @@ async def test_anything():
def test_error_when_wrong_keyword_argument_is_passed(
pytester: pytest.Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
Expand All @@ -59,7 +62,7 @@ async def test_anything():
"""
)
)
result = pytester.runpytest_subprocess()
result = pytester.runpytest("--assert=plain")
result.assert_outcomes(errors=1)
result.stdout.fnmatch_lines(
["*ValueError: mark.asyncio accepts only a keyword argument 'loop_scope'*"]
Expand All @@ -69,6 +72,7 @@ async def test_anything():
def test_error_when_additional_keyword_arguments_are_passed(
pytester: pytest.Pytester,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
Expand All @@ -80,7 +84,7 @@ async def test_anything():
"""
)
)
result = pytester.runpytest_subprocess()
result = pytester.runpytest("--assert=plain")
result.assert_outcomes(errors=1)
result.stdout.fnmatch_lines(
["*ValueError: mark.asyncio accepts only a keyword argument*"]
Expand Down
27 changes: 21 additions & 6 deletions tests/markers/test_module_scope.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
from textwrap import dedent

from pytest import Pytester
Expand Down Expand Up @@ -87,8 +88,15 @@ async def test_does_not_use_custom_event_loop_policy():
"""
),
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=2)
pytest_args = ["--asyncio-mode=strict"]
if sys.version_info >= (3, 14):
pytest_args.extend(["-W", "default"])
result = pytester.runpytest(*pytest_args)
if sys.version_info >= (3, 14):
result.assert_outcomes(passed=2, warnings=3)
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
else:
result.assert_outcomes(passed=2)


def test_asyncio_mark_respects_parametrized_loop_policies(
Expand Down Expand Up @@ -119,8 +127,15 @@ async def test_parametrized_loop():
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(passed=2)
pytest_args = ["--asyncio-mode=strict"]
if sys.version_info >= (3, 14):
pytest_args.extend(["-W", "default"])
result = pytester.runpytest(*pytest_args)
if sys.version_info >= (3, 14):
result.assert_outcomes(passed=2, warnings=2)
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
else:
result.assert_outcomes(passed=2)


def test_asyncio_mark_provides_module_scoped_loop_to_fixtures(
Expand Down Expand Up @@ -150,7 +165,7 @@ async def test_runs_is_same_loop_as_fixture(my_fixture):
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)


Expand Down Expand Up @@ -298,5 +313,5 @@ async def test_anything():
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(warnings=0, passed=1)
25 changes: 20 additions & 5 deletions tests/markers/test_package_scope.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
from textwrap import dedent

from pytest import Pytester
Expand Down Expand Up @@ -127,8 +128,15 @@ async def test_also_uses_custom_event_loop_policy():
"""
),
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=2)
pytest_args = ["--asyncio-mode=strict"]
if sys.version_info >= (3, 14):
pytest_args.extend(["-W", "default"])
result = pytester.runpytest(*pytest_args)
if sys.version_info >= (3, 14):
result.assert_outcomes(passed=2, warnings=3)
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
else:
result.assert_outcomes(passed=2)


def test_asyncio_mark_respects_parametrized_loop_policies(
Expand Down Expand Up @@ -160,8 +168,15 @@ async def test_parametrized_loop():
"""
),
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(passed=2)
pytest_args = ["--asyncio-mode=strict"]
if sys.version_info >= (3, 14):
pytest_args.extend(["-W", "default"])
result = pytester.runpytest(*pytest_args)
if sys.version_info >= (3, 14):
result.assert_outcomes(passed=2, warnings=2)
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
else:
result.assert_outcomes(passed=2)


def test_asyncio_mark_provides_package_scoped_loop_to_fixtures(
Expand Down Expand Up @@ -207,7 +222,7 @@ async def test_runs_in_same_loop_as_fixture(my_fixture):
"""
),
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)


Expand Down
27 changes: 21 additions & 6 deletions tests/markers/test_session_scope.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
from textwrap import dedent

from pytest import Pytester
Expand Down Expand Up @@ -128,8 +129,15 @@ async def test_also_uses_custom_event_loop_policy():
"""
),
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=2)
pytest_args = ["--asyncio-mode=strict"]
if sys.version_info >= (3, 14):
pytest_args.extend(["-W", "default"])
result = pytester.runpytest(*pytest_args)
if sys.version_info >= (3, 14):
result.assert_outcomes(passed=2, warnings=3)
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
else:
result.assert_outcomes(passed=2)


def test_asyncio_mark_respects_parametrized_loop_policies(
Expand Down Expand Up @@ -161,8 +169,15 @@ async def test_parametrized_loop():
"""
),
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(passed=2)
pytest_args = ["--asyncio-mode=strict"]
if sys.version_info >= (3, 14):
pytest_args.extend(["-W", "default"])
result = pytester.runpytest(*pytest_args)
if sys.version_info >= (3, 14):
result.assert_outcomes(passed=2, warnings=2)
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
else:
result.assert_outcomes(passed=2)


def test_asyncio_mark_provides_session_scoped_loop_to_fixtures(
Expand Down Expand Up @@ -212,7 +227,7 @@ async def test_runs_in_same_loop_as_fixture(my_fixture):
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)


Expand Down Expand Up @@ -423,5 +438,5 @@ async def test_anything():
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(warnings=0, passed=1)
8 changes: 4 additions & 4 deletions tests/modes/test_strict_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async def test_anything():
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result = pytester.runpytest("--asyncio-mode=strict", "-W default", "--assert=plain")
if pytest_version >= (8, 4, 0):
result.assert_outcomes(failed=1, skipped=0, warnings=0)
else:
Expand All @@ -119,7 +119,7 @@ async def test_anything(any_fixture):
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result = pytester.runpytest("--asyncio-mode=strict", "-W default", "--assert=plain")

if pytest_version >= (8, 4, 0):
result.assert_outcomes(failed=1, skipped=0, warnings=2)
Expand Down Expand Up @@ -155,7 +155,7 @@ async def test_anything(any_fixture):
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result = pytester.runpytest("--asyncio-mode=strict", "-W default", "--assert=plain")
if pytest_version >= (8, 4, 0):
result.assert_outcomes(passed=1, failed=0, skipped=0, warnings=2)
else:
Expand Down Expand Up @@ -202,7 +202,7 @@ async def test_anything(any_fixture):
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result = pytester.runpytest("--asyncio-mode=strict", "-W default", "--assert=plain")
if pytest_version >= (8, 4, 0):
result.assert_outcomes(passed=1, warnings=2)
else:
Expand Down
Loading