also warn on awaitable or async iterable test results

This commit is contained in:
Thomas Grainger 2019-08-15 12:03:06 +01:00
parent 0ba774a7c3
commit 6b9d729ed3
No known key found for this signature in database
GPG Key ID: E452A1247BAC1A88
2 changed files with 18 additions and 7 deletions

View File

@ -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

View File

@ -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