diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 70b486fb1..951da792b 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -151,10 +151,7 @@ def pytest_configure(config): @hookimpl(trylast=True) def pytest_pyfunc_call(pyfuncitem): - testfunction = pyfuncitem.obj - if iscoroutinefunction(testfunction) or ( - sys.version_info >= (3, 6) and inspect.isasyncgenfunction(testfunction) - ): + def async_warn(): msg = "async def functions are not natively supported and have been skipped.\n" msg += "You need to install a suitable plugin for your async framework, for example:\n" msg += " - pytest-asyncio\n" @@ -162,9 +159,17 @@ def pytest_pyfunc_call(pyfuncitem): msg += " - pytest-tornasync" warnings.warn(PytestUnhandledCoroutineWarning(msg.format(pyfuncitem.nodeid))) skip(msg="async def function and no async plugin installed (see warnings)") + + testfunction = pyfuncitem.obj + if iscoroutinefunction(testfunction) or ( + sys.version_info >= (3, 6) and inspect.isasyncgenfunction(testfunction) + ): + async_warn() funcargs = pyfuncitem.funcargs testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames} - testfunction(**testargs) + result = testfunction(**testargs) + if hasattr(result, "__await__") or hasattr(result, "__aiter__"): + async_warn() return True diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 55e3b966d..7abbe734e 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -1192,6 +1192,8 @@ def test_warn_on_async_function(testdir): pass async def test_2(): pass + def test_3(): + return test_2() """ ) result = testdir.runpytest() @@ -1199,8 +1201,9 @@ def test_warn_on_async_function(testdir): [ "test_async.py::test_1", "test_async.py::test_2", + "test_async.py::test_3", "*async def functions are not natively supported*", - "*2 skipped, 2 warnings in*", + "*3 skipped, 3 warnings in*", ] ) # ensure our warning message appears only once @@ -1220,6 +1223,8 @@ def test_warn_on_async_gen_function(testdir): yield async def test_2(): yield + def test_3(): + return test_2() """ ) result = testdir.runpytest() @@ -1227,8 +1232,9 @@ def test_warn_on_async_gen_function(testdir): [ "test_async.py::test_1", "test_async.py::test_2", + "test_async.py::test_3", "*async def functions are not natively supported*", - "*2 skipped, 2 warnings in*", + "*3 skipped, 3 warnings in*", ] ) # ensure our warning message appears only once