Skip to content

Commit 4b1908d

Browse files
committed
[fix] Prevent DeprecationWarning from bubbling to user code.
Signed-off-by: Michael Seifert <[email protected]>
1 parent fc6d6cf commit 4b1908d

File tree

7 files changed

+97
-2
lines changed

7 files changed

+97
-2
lines changed

docs/source/reference/changelog.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
Changelog
33
=========
44

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

1011
Known issues
1112
------------

pytest_asyncio/plugin.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,9 @@ def _removesuffix(s: str, suffix: str) -> str:
667667
def _temporary_event_loop_policy(policy: AbstractEventLoopPolicy) -> Iterator[None]:
668668
old_loop_policy = asyncio.get_event_loop_policy()
669669
try:
670-
old_loop = asyncio.get_event_loop()
670+
with warnings.catch_warnings():
671+
warnings.simplefilter("ignore", DeprecationWarning)
672+
old_loop = asyncio.get_event_loop()
671673
except RuntimeError:
672674
old_loop = None
673675
asyncio.set_event_loop_policy(policy)

tests/markers/test_class_scope.py

+19
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,22 @@ async def test_does_not_fail(self, sets_event_loop_to_none, n):
287287
)
288288
result = pytester.runpytest("--asyncio-mode=strict")
289289
result.assert_outcomes(passed=2)
290+
291+
292+
def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
293+
pytester: pytest.Pytester,
294+
):
295+
pytester.makepyfile(
296+
dedent(
297+
"""\
298+
import pytest
299+
300+
@pytest.mark.asyncio(scope="class")
301+
class TestClass:
302+
async def test_anything(self):
303+
pass
304+
"""
305+
)
306+
)
307+
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
308+
result.assert_outcomes(warnings=0, passed=1)

tests/markers/test_function_scope.py

+18
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,21 @@ async def test_does_not_fail(sets_event_loop_to_none, n):
179179
)
180180
result = pytester.runpytest("--asyncio-mode=strict")
181181
result.assert_outcomes(passed=2)
182+
183+
184+
def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
185+
pytester: Pytester,
186+
):
187+
pytester.makepyfile(
188+
dedent(
189+
"""\
190+
import pytest
191+
192+
@pytest.mark.asyncio
193+
async def test_anything():
194+
pass
195+
"""
196+
)
197+
)
198+
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
199+
result.assert_outcomes(warnings=0, passed=1)

tests/markers/test_module_scope.py

+18
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,21 @@ async def test_does_not_fail(sets_event_loop_to_none, n):
344344
)
345345
result = pytester.runpytest("--asyncio-mode=strict")
346346
result.assert_outcomes(passed=2)
347+
348+
349+
def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
350+
pytester: Pytester,
351+
):
352+
pytester.makepyfile(
353+
dedent(
354+
"""\
355+
import pytest
356+
357+
@pytest.mark.asyncio(scope="module")
358+
async def test_anything():
359+
pass
360+
"""
361+
)
362+
)
363+
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
364+
result.assert_outcomes(warnings=0, passed=1)

tests/markers/test_package_scope.py

+19
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,22 @@ async def test_does_not_fail(sets_event_loop_to_none, n):
350350
)
351351
result = pytester.runpytest("--asyncio-mode=strict")
352352
result.assert_outcomes(passed=2)
353+
354+
355+
def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
356+
pytester: Pytester,
357+
):
358+
pytester.makepyfile(
359+
__init__="",
360+
test_module=dedent(
361+
"""\
362+
import pytest
363+
364+
@pytest.mark.asyncio(scope="package")
365+
async def test_anything():
366+
pass
367+
"""
368+
),
369+
)
370+
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
371+
result.assert_outcomes(warnings=0, passed=1)

tests/markers/test_session_scope.py

+18
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,21 @@ async def test_does_not_fail(sets_event_loop_to_none, n):
415415
)
416416
result = pytester.runpytest("--asyncio-mode=strict")
417417
result.assert_outcomes(passed=2)
418+
419+
420+
def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
421+
pytester: Pytester,
422+
):
423+
pytester.makepyfile(
424+
dedent(
425+
"""\
426+
import pytest
427+
428+
@pytest.mark.asyncio(scope="session")
429+
async def test_anything():
430+
pass
431+
"""
432+
)
433+
)
434+
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
435+
result.assert_outcomes(warnings=0, passed=1)

0 commit comments

Comments
 (0)