From 9310655c829d74f34195f3504577983241b7e299 Mon Sep 17 00:00:00 2001 From: Michael Seifert Date: Thu, 4 Jul 2024 10:17:56 +0200 Subject: [PATCH 1/2] [test] Run some tests in a Pytester subprocess to avoid PytestAssertRewriteWarning. Signed-off-by: Michael Seifert --- tests/hypothesis/test_base.py | 4 ++-- tests/markers/test_function_scope.py | 2 +- tests/markers/test_module_scope.py | 2 +- tests/modes/test_strict_mode.py | 4 ++-- tests/test_asyncio_mark.py | 14 +++++++------- tests/test_event_loop_fixture_finalizer.py | 6 +++--- ...test_event_loop_fixture_override_deprecation.py | 8 ++++---- tests/test_explicit_event_loop_fixture_request.py | 10 +++++----- 8 files changed, 25 insertions(+), 25 deletions(-) diff --git a/tests/hypothesis/test_base.py b/tests/hypothesis/test_base.py index fa12f2b3..2d2171bd 100644 --- a/tests/hypothesis/test_base.py +++ b/tests/hypothesis/test_base.py @@ -23,7 +23,7 @@ async def test_mark_inner(n): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(passed=1) @@ -67,7 +67,7 @@ async def test_explicit_fixture_request(event_loop, n): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(passed=1, warnings=2) result.stdout.fnmatch_lines( [ diff --git a/tests/markers/test_function_scope.py b/tests/markers/test_function_scope.py index 7a5f8533..2057a128 100644 --- a/tests/markers/test_function_scope.py +++ b/tests/markers/test_function_scope.py @@ -43,7 +43,7 @@ async def test_remember_loop(event_loop): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(passed=1, warnings=1) result.stdout.fnmatch_lines( '*is asynchronous and explicitly requests the "event_loop" fixture*' diff --git a/tests/markers/test_module_scope.py b/tests/markers/test_module_scope.py index 01fdc324..5cc6a2a7 100644 --- a/tests/markers/test_module_scope.py +++ b/tests/markers/test_module_scope.py @@ -48,7 +48,7 @@ def sample_fixture(): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(passed=2, warnings=2) result.stdout.fnmatch_lines( '*is asynchronous and explicitly requests the "event_loop" fixture*' diff --git a/tests/modes/test_strict_mode.py b/tests/modes/test_strict_mode.py index 3afc9f5b..220410be 100644 --- a/tests/modes/test_strict_mode.py +++ b/tests/modes/test_strict_mode.py @@ -79,7 +79,7 @@ async def test_anything(): """ ) ) - result = testdir.runpytest("--asyncio-mode=strict", "-W default") + result = testdir.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(skipped=1, warnings=1) result.stdout.fnmatch_lines(["*async def functions are not natively supported*"]) @@ -100,7 +100,7 @@ async def test_anything(any_fixture): """ ) ) - result = testdir.runpytest("--asyncio-mode=strict", "-W default") + result = testdir.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(skipped=1, warnings=2) result.stdout.fnmatch_lines( [ diff --git a/tests/test_asyncio_mark.py b/tests/test_asyncio_mark.py index b514cbcd..20ac173d 100644 --- a/tests/test_asyncio_mark.py +++ b/tests/test_asyncio_mark.py @@ -15,7 +15,7 @@ def test_a(): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(passed=1) result.stdout.fnmatch_lines( ["*is marked with '@pytest.mark.asyncio' but it is not an async function.*"] @@ -36,7 +36,7 @@ async def test_a(): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(xfailed=1, warnings=1) result.stdout.fnmatch_lines( ["*Tests based on asynchronous generators are not supported*"] @@ -54,7 +54,7 @@ async def test_a(): """ ) ) - result = pytester.runpytest("--asyncio-mode=auto", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=auto", "-W default") result.assert_outcomes(xfailed=1, warnings=1) result.stdout.fnmatch_lines( ["*Tests based on asynchronous generators are not supported*"] @@ -76,7 +76,7 @@ async def test_a(self): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(xfailed=1, warnings=1) result.stdout.fnmatch_lines( ["*Tests based on asynchronous generators are not supported*"] @@ -96,7 +96,7 @@ async def test_a(): """ ) ) - result = pytester.runpytest("--asyncio-mode=auto", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=auto", "-W default") result.assert_outcomes(xfailed=1, warnings=1) result.stdout.fnmatch_lines( ["*Tests based on asynchronous generators are not supported*"] @@ -119,7 +119,7 @@ async def test_a(): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(xfailed=1, warnings=1) result.stdout.fnmatch_lines( ["*Tests based on asynchronous generators are not supported*"] @@ -139,7 +139,7 @@ async def test_a(): """ ) ) - result = pytester.runpytest("--asyncio-mode=auto", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=auto", "-W default") result.assert_outcomes(xfailed=1, warnings=1) result.stdout.fnmatch_lines( ["*Tests based on asynchronous generators are not supported*"] diff --git a/tests/test_event_loop_fixture_finalizer.py b/tests/test_event_loop_fixture_finalizer.py index eabb54a3..ae260261 100644 --- a/tests/test_event_loop_fixture_finalizer.py +++ b/tests/test_event_loop_fixture_finalizer.py @@ -84,7 +84,7 @@ async def test_async_with_explicit_fixture_request(event_loop): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(passed=1, warnings=1) result.stdout.fnmatch_lines( '*is asynchronous and explicitly requests the "event_loop" fixture*' @@ -113,7 +113,7 @@ async def test_ends_with_unclosed_loop(): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W", "default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W", "default") result.assert_outcomes(passed=1, warnings=2) result.stdout.fnmatch_lines("*unclosed event loop*") @@ -135,6 +135,6 @@ async def test_ends_with_unclosed_loop(): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W", "default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W", "default") result.assert_outcomes(passed=1, warnings=1) result.stdout.fnmatch_lines("*unclosed event loop*") diff --git a/tests/test_event_loop_fixture_override_deprecation.py b/tests/test_event_loop_fixture_override_deprecation.py index 45afc542..683f0963 100644 --- a/tests/test_event_loop_fixture_override_deprecation.py +++ b/tests/test_event_loop_fixture_override_deprecation.py @@ -22,7 +22,7 @@ async def test_emits_warning(): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(passed=1, warnings=1) result.stdout.fnmatch_lines( ["*event_loop fixture provided by pytest-asyncio has been redefined*"] @@ -50,7 +50,7 @@ async def test_emits_warning_when_requested_explicitly(event_loop): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(passed=1, warnings=2) result.stdout.fnmatch_lines( ["*event_loop fixture provided by pytest-asyncio has been redefined*"] @@ -80,7 +80,7 @@ def test_emits_no_warning(): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict") + result = pytester.runpytest_subprocess("--asyncio-mode=strict") result.assert_outcomes(passed=1, warnings=0) @@ -107,5 +107,5 @@ def test_emits_warning(uses_event_loop): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(passed=1, warnings=1) diff --git a/tests/test_explicit_event_loop_fixture_request.py b/tests/test_explicit_event_loop_fixture_request.py index 4cac85f7..e09893fa 100644 --- a/tests/test_explicit_event_loop_fixture_request.py +++ b/tests/test_explicit_event_loop_fixture_request.py @@ -17,7 +17,7 @@ async def test_coroutine_emits_warning(event_loop): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(passed=1, warnings=1) result.stdout.fnmatch_lines( ['*is asynchronous and explicitly requests the "event_loop" fixture*'] @@ -39,7 +39,7 @@ async def test_coroutine_emits_warning(self, event_loop): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(passed=1, warnings=1) result.stdout.fnmatch_lines( ['*is asynchronous and explicitly requests the "event_loop" fixture*'] @@ -62,7 +62,7 @@ async def test_coroutine_emits_warning(event_loop): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(passed=1, warnings=1) result.stdout.fnmatch_lines( ['*is asynchronous and explicitly requests the "event_loop" fixture*'] @@ -88,7 +88,7 @@ async def test_uses_fixture(emits_warning): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(passed=1, warnings=1) result.stdout.fnmatch_lines( ['*is asynchronous and explicitly requests the "event_loop" fixture*'] @@ -114,7 +114,7 @@ async def test_uses_fixture(emits_warning): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict", "-W default") + result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default") result.assert_outcomes(passed=1, warnings=1) result.stdout.fnmatch_lines( ['*is asynchronous and explicitly requests the "event_loop" fixture*'] From d15c9fc3346fcd97bf66926b571564ab4cefe491 Mon Sep 17 00:00:00 2001 From: Michael Seifert Date: Sun, 19 May 2024 14:00:57 +0200 Subject: [PATCH 2/2] [fix] Fixed a bug that causes markers to be duplicated for async test functions. Signed-off-by: Michael Seifert --- docs/source/reference/changelog.rst | 9 ++++++++ pytest_asyncio/plugin.py | 3 ++- tests/markers/test_function_scope.py | 31 ++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/docs/source/reference/changelog.rst b/docs/source/reference/changelog.rst index bdec69db..1e2b0f20 100644 --- a/docs/source/reference/changelog.rst +++ b/docs/source/reference/changelog.rst @@ -2,6 +2,15 @@ Changelog ========= +0.23.8 (UNRELEASED) +=================== +- Fixes a bug that caused duplicate markers in async tests `#813 `_ + +Known issues +------------ +As of v0.23, pytest-asyncio attaches an asyncio event loop to each item of the test suite (i.e. session, packages, modules, classes, functions) and allows tests to be run in those loops when marked accordingly. Pytest-asyncio currently assumes that async fixture scope is correlated with the new event loop scope. This prevents fixtures from being evaluated independently from the event loop scope and breaks some existing test suites (see `#706`_). For example, a test suite may require all fixtures and tests to run in the same event loop, but have async fixtures that are set up and torn down for each module. If you're affected by this issue, please continue using the v0.21 release, until it is resolved. + + 0.23.7 (2024-05-19) =================== - Silence deprecation warnings about unclosed event loops that occurred with certain CPython patch releases `#817 `_ diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index cd4c4ede..d3d1fcf7 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -405,7 +405,8 @@ def _from_function(cls, function: Function, /) -> Function: keywords=function.keywords, originalname=function.originalname, ) - subclass_instance.own_markers.extend(function.own_markers) + subclass_instance.own_markers = function.own_markers + assert subclass_instance.own_markers == function.own_markers subclassed_function_signature = inspect.signature(subclass_instance.obj) if "event_loop" in subclassed_function_signature.parameters: subclass_instance.warn( diff --git a/tests/markers/test_function_scope.py b/tests/markers/test_function_scope.py index 2057a128..eded4552 100644 --- a/tests/markers/test_function_scope.py +++ b/tests/markers/test_function_scope.py @@ -197,3 +197,34 @@ async def test_anything(): ) result = pytester.runpytest_subprocess("--asyncio-mode=strict") result.assert_outcomes(warnings=0, passed=1) + + +def test_asyncio_mark_does_not_duplicate_other_marks_in_auto_mode( + pytester: Pytester, +): + pytester.makeconftest( + dedent( + """\ + def pytest_configure(config): + config.addinivalue_line( + "markers", "dummy_marker: mark used for testing purposes" + ) + """ + ) + ) + pytester.makepyfile( + dedent( + """\ + import pytest + + @pytest.mark.dummy_marker + async def test_markers_not_duplicated(request): + markers = [] + for node, marker in request.node.iter_markers_with_node(): + markers.append(marker) + assert len(markers) == 2 + """ + ) + ) + result = pytester.runpytest_subprocess("--asyncio-mode=auto") + result.assert_outcomes(warnings=0, passed=1)