diff --git a/docs/source/reference/changelog.rst b/docs/source/reference/changelog.rst index bb54c0b0..31f2d71e 100644 --- a/docs/source/reference/changelog.rst +++ b/docs/source/reference/changelog.rst @@ -2,10 +2,11 @@ Changelog ========= -0.23.5 (UNRELEASED) +0.23.5 (2024-02-09) =================== - Declare compatibility with pytest 8 `#737 `_ - Fix typing errors with recent versions of mypy `#769 `_ +- Prevent DeprecationWarning about internal use of `asyncio.get_event_loop()` from affecting test cases `#757 `_ Known issues ------------ diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index b238955d..36066cf2 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -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) diff --git a/tests/markers/test_class_scope.py b/tests/markers/test_class_scope.py index 9ec3ed9c..3c77bab0 100644 --- a/tests/markers/test_class_scope.py +++ b/tests/markers/test_class_scope.py @@ -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) diff --git a/tests/markers/test_function_scope.py b/tests/markers/test_function_scope.py index 25ff609f..7a5f8533 100644 --- a/tests/markers/test_function_scope.py +++ b/tests/markers/test_function_scope.py @@ -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) diff --git a/tests/markers/test_module_scope.py b/tests/markers/test_module_scope.py index cf6b2f60..01fdc324 100644 --- a/tests/markers/test_module_scope.py +++ b/tests/markers/test_module_scope.py @@ -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) diff --git a/tests/markers/test_package_scope.py b/tests/markers/test_package_scope.py index e0f44322..c80289be 100644 --- a/tests/markers/test_package_scope.py +++ b/tests/markers/test_package_scope.py @@ -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) diff --git a/tests/markers/test_session_scope.py b/tests/markers/test_session_scope.py index 9049c172..b8b747a0 100644 --- a/tests/markers/test_session_scope.py +++ b/tests/markers/test_session_scope.py @@ -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)