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

Prevent DeprecationWarning from reaching user code #779

Merged
merged 1 commit into from Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/source/reference/changelog.rst
Expand Up @@ -2,10 +2,11 @@
Changelog
=========

0.23.5 (UNRELEASED)
0.23.5 (2024-02-09)
===================
- Declare compatibility with pytest 8 `#737 <https://github.com/pytest-dev/pytest-asyncio/issues/737>`_
- Fix typing errors with recent versions of mypy `#769 <https://github.com/pytest-dev/pytest-asyncio/issues/769>`_
- Prevent DeprecationWarning about internal use of `asyncio.get_event_loop()` from affecting test cases `#757 <https://github.com/pytest-dev/pytest-asyncio/issues/757>`_

Known issues
------------
Expand Down
4 changes: 3 additions & 1 deletion pytest_asyncio/plugin.py
Expand Up @@ -667,7 +667,9 @@ def _removesuffix(s: str, suffix: str) -> str:
def _temporary_event_loop_policy(policy: AbstractEventLoopPolicy) -> Iterator[None]:
old_loop_policy = asyncio.get_event_loop_policy()
try:
old_loop = asyncio.get_event_loop()
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
old_loop = asyncio.get_event_loop()
except RuntimeError:
old_loop = None
asyncio.set_event_loop_policy(policy)
Expand Down
19 changes: 19 additions & 0 deletions tests/markers/test_class_scope.py
Expand Up @@ -287,3 +287,22 @@ async def test_does_not_fail(self, sets_event_loop_to_none, n):
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=2)


def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
pytester: pytest.Pytester,
):
pytester.makepyfile(
dedent(
"""\
import pytest

@pytest.mark.asyncio(scope="class")
class TestClass:
async def test_anything(self):
pass
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(warnings=0, passed=1)
18 changes: 18 additions & 0 deletions tests/markers/test_function_scope.py
Expand Up @@ -179,3 +179,21 @@ async def test_does_not_fail(sets_event_loop_to_none, n):
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=2)


def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
pytester: Pytester,
):
pytester.makepyfile(
dedent(
"""\
import pytest

@pytest.mark.asyncio
async def test_anything():
pass
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(warnings=0, passed=1)
18 changes: 18 additions & 0 deletions tests/markers/test_module_scope.py
Expand Up @@ -344,3 +344,21 @@ async def test_does_not_fail(sets_event_loop_to_none, n):
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=2)


def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
pytester: Pytester,
):
pytester.makepyfile(
dedent(
"""\
import pytest

@pytest.mark.asyncio(scope="module")
async def test_anything():
pass
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(warnings=0, passed=1)
19 changes: 19 additions & 0 deletions tests/markers/test_package_scope.py
Expand Up @@ -350,3 +350,22 @@ async def test_does_not_fail(sets_event_loop_to_none, n):
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=2)


def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
pytester: Pytester,
):
pytester.makepyfile(
__init__="",
test_module=dedent(
"""\
import pytest

@pytest.mark.asyncio(scope="package")
async def test_anything():
pass
"""
),
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(warnings=0, passed=1)
18 changes: 18 additions & 0 deletions tests/markers/test_session_scope.py
Expand Up @@ -415,3 +415,21 @@ async def test_does_not_fail(sets_event_loop_to_none, n):
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=2)


def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
pytester: Pytester,
):
pytester.makepyfile(
dedent(
"""\
import pytest

@pytest.mark.asyncio(scope="session")
async def test_anything():
pass
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(warnings=0, passed=1)