From f0b6709cf5459ae9de8f317634e3f1dd6be481d9 Mon Sep 17 00:00:00 2001 From: Sylvain Ackermann Date: Thu, 17 Feb 2022 13:42:16 +0100 Subject: [PATCH 1/2] Fix asyncio auto mode not marking static methods. --- pytest_asyncio/plugin.py | 3 +++ tests/modes/test_auto_mode.py | 50 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 1ecfbe49..0ccfe0c9 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -320,6 +320,9 @@ def pytest_pycollect_makeitem( if not collector.funcnamefilter(name): return None _preprocess_async_fixtures(collector.config, _HOLDER) + if isinstance(obj, staticmethod): + # staticmethods need to be unwrapped. + obj = getattr(obj, "__func__", False) if ( _is_coroutine(obj) or _is_hypothesis_test(obj) diff --git a/tests/modes/test_auto_mode.py b/tests/modes/test_auto_mode.py index 157ffded..fc4d2df0 100644 --- a/tests/modes/test_auto_mode.py +++ b/tests/modes/test_auto_mode.py @@ -87,3 +87,53 @@ async def test_a(self, fixture_a): ) result = testdir.runpytest("--asyncio-mode=auto") result.assert_outcomes(passed=1) + + +def test_auto_mode_static_method(testdir): + testdir.makepyfile( + dedent( + """\ + import asyncio + + pytest_plugins = 'pytest_asyncio' + + + class TestA: + + @staticmethod + async def test_a(): + await asyncio.sleep(0) + """ + ) + ) + result = testdir.runpytest("--asyncio-mode=auto") + result.assert_outcomes(passed=1) + + +def test_auto_mode_static_method_fixture(testdir): + testdir.makepyfile( + dedent( + """\ + import asyncio + import pytest + + pytest_plugins = 'pytest_asyncio' + + + class TestA: + + @staticmethod + @pytest.fixture + async def fixture_a(): + await asyncio.sleep(0) + return 1 + + @staticmethod + async def test_a(fixture_a): + await asyncio.sleep(0) + assert fixture_a == 1 + """ + ) + ) + result = testdir.runpytest("--asyncio-mode=auto") + result.assert_outcomes(passed=1) From 127d25050b780187f5d07b4eacdb18fcc9549528 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Mon, 21 Feb 2022 11:29:48 +0200 Subject: [PATCH 2/2] Update pytest_asyncio/plugin.py --- pytest_asyncio/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 0ccfe0c9..c31683ab 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -322,7 +322,7 @@ def pytest_pycollect_makeitem( _preprocess_async_fixtures(collector.config, _HOLDER) if isinstance(obj, staticmethod): # staticmethods need to be unwrapped. - obj = getattr(obj, "__func__", False) + obj = obj.__func__ if ( _is_coroutine(obj) or _is_hypothesis_test(obj)