Skip to content

Commit

Permalink
[fix] Prevent DeprecationWarning from bubbling to user code.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>
  • Loading branch information
seifertm committed Feb 9, 2024
1 parent fc6d6cf commit 4b1908d
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 2 deletions.
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)

0 comments on commit 4b1908d

Please sign in to comment.