Skip to content

Commit

Permalink
Emit a warning when a async def function is not handled by a plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Feb 25, 2019
1 parent 4cecea2 commit abf01da
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelog/2224.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A warning is emitted when a ``async`` test function is encountered and is not handled by any
of the existing async frameworks (such as ``pytest-asyncio`` or ``pytest-trio``).
8 changes: 8 additions & 0 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ def pytest_configure(config):
@hookimpl(trylast=True)
def pytest_pyfunc_call(pyfuncitem):
testfunction = pyfuncitem.obj
if inspect.iscoroutinefunction(testfunction):
msg = "test function {} is a coroutine and not natively supported.\n"
msg += "you need to install a suitable plugin for your async framework, for example:\n"
msg += " - pytest-asyncio\n"
msg += " - pytest-trio\n"
msg += " - pytest-tornasync"
warnings.warn(PytestWarning(msg.format(pyfuncitem.nodeid)))
return None
funcargs = pyfuncitem.funcargs
testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}
testfunction(**testargs)
Expand Down
20 changes: 20 additions & 0 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,3 +1179,23 @@ def test_fixture_mock_integration(testdir):
def test_usage_error_code(testdir):
result = testdir.runpytest("-unknown-option-")
assert result.ret == EXIT_USAGEERROR


@pytest.mark.skipif(
sys.version_info[:2] < (3, 5), reason="async def syntax python 3.5+ only"
)
@pytest.mark.filterwarnings("default")
def test_warn_on_async_function(testdir):
testdir.makepyfile(
test_async="""
async def test_1():
pass
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(
[
"*test function test_async.py::test_1 is a coroutine*",
"*1 passed, 1 warnings in*",
]
)

0 comments on commit abf01da

Please sign in to comment.