3434from _pytest .compat import get_real_func
3535from _pytest .compat import getimfunc
3636from _pytest .compat import getlocation
37+ from _pytest .compat import is_async_function
3738from _pytest .compat import is_generator
38- from _pytest .compat import iscoroutinefunction
3939from _pytest .compat import NOTSET
4040from _pytest .compat import REGEX_TYPE
4141from _pytest .compat import safe_getattr
@@ -159,7 +159,7 @@ def pytest_configure(config):
159159 )
160160
161161
162- def async_warn (nodeid : str ) -> None :
162+ def async_warn_and_skip (nodeid : str ) -> None :
163163 msg = "async def functions are not natively supported and have been skipped.\n "
164164 msg += (
165165 "You need to install a suitable plugin for your async framework, for example:\n "
@@ -175,33 +175,13 @@ def async_warn(nodeid: str) -> None:
175175@hookimpl (trylast = True )
176176def pytest_pyfunc_call (pyfuncitem : "Function" ):
177177 testfunction = pyfuncitem .obj
178-
179- try :
180- # ignoring type as the import is invalid in py37 and mypy thinks its a error
181- from unittest import IsolatedAsyncioTestCase # type: ignore
182- except ImportError :
183- async_ok_in_stdlib = False
184- else :
185- async_ok_in_stdlib = isinstance (
186- getattr (testfunction , "__self__" , None ), IsolatedAsyncioTestCase
187- )
188-
189- if (
190- iscoroutinefunction (testfunction )
191- or (sys .version_info >= (3 , 6 ) and inspect .isasyncgenfunction (testfunction ))
192- ) and not async_ok_in_stdlib :
193- async_warn (pyfuncitem .nodeid )
178+ if is_async_function (testfunction ):
179+ async_warn_and_skip (pyfuncitem .nodeid )
194180 funcargs = pyfuncitem .funcargs
195181 testargs = {arg : funcargs [arg ] for arg in pyfuncitem ._fixtureinfo .argnames }
196182 result = testfunction (** testargs )
197183 if hasattr (result , "__await__" ) or hasattr (result , "__aiter__" ):
198- if async_ok_in_stdlib :
199- # todo: investigate moving this to the unittest plugin
200- # by a test call result hook
201- testcase = testfunction .__self__
202- testcase ._callMaybeAsync (lambda : result )
203- else :
204- async_warn (pyfuncitem .nodeid )
184+ async_warn_and_skip (pyfuncitem .nodeid )
205185 return True
206186
207187
0 commit comments