Skip to content

Commit 2e340e7

Browse files
committed
Emit a warning when a async def function is not handled by a plugin
Fix #2224
1 parent 9059722 commit 2e340e7

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

changelog/2224.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A warning is emitted when a ``async`` test function is encountered and is not handled by any
2+
of the existing async frameworks (such as ``pytest-asyncio`` or ``pytest-trio``).

src/_pytest/python.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ def pytest_configure(config):
156156
@hookimpl(trylast=True)
157157
def pytest_pyfunc_call(pyfuncitem):
158158
testfunction = pyfuncitem.obj
159+
iscoroutinefunction = getattr(inspect, "iscoroutinefunction", None)
160+
if iscoroutinefunction is not None and iscoroutinefunction(testfunction):
161+
msg = "test function {} is a coroutine and not natively supported.\n"
162+
msg += "you need to install a suitable plugin for your async framework, for example:\n"
163+
msg += " - pytest-asyncio\n"
164+
msg += " - pytest-trio\n"
165+
msg += " - pytest-tornasync"
166+
warnings.warn(PytestWarning(msg.format(pyfuncitem.nodeid)))
167+
return None
159168
funcargs = pyfuncitem.funcargs
160169
testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}
161170
testfunction(**testargs)

testing/acceptance_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,3 +1179,23 @@ def test_fixture_mock_integration(testdir):
11791179
def test_usage_error_code(testdir):
11801180
result = testdir.runpytest("-unknown-option-")
11811181
assert result.ret == EXIT_USAGEERROR
1182+
1183+
1184+
@pytest.mark.skipif(
1185+
sys.version_info[:2] < (3, 5), reason="async def syntax python 3.5+ only"
1186+
)
1187+
@pytest.mark.filterwarnings("default")
1188+
def test_warn_on_async_function(testdir):
1189+
testdir.makepyfile(
1190+
test_async="""
1191+
async def test_1():
1192+
pass
1193+
"""
1194+
)
1195+
result = testdir.runpytest()
1196+
result.stdout.fnmatch_lines(
1197+
[
1198+
"*test function test_async.py::test_1 is a coroutine*",
1199+
"*1 passed, 1 warnings in*",
1200+
]
1201+
)

0 commit comments

Comments
 (0)