Skip to content

Commit 0cb3957

Browse files
committed
warn for async gen functions
1 parent a295a3d commit 0cb3957

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/_pytest/python.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ def pytest_configure(config):
152152
def pytest_pyfunc_call(pyfuncitem):
153153
testfunction = pyfuncitem.obj
154154
iscoroutinefunction = getattr(inspect, "iscoroutinefunction", None)
155-
if iscoroutinefunction is not None and iscoroutinefunction(testfunction):
155+
isasyncgenfunction = getattr(inspect, "isasyncgenfunction", None)
156+
if (iscoroutinefunction is not None and iscoroutinefunction(testfunction)) or (
157+
isasyncgenfunction is not None and isasyncgenfunction(testfunction)
158+
):
156159
msg = "Coroutine functions are not natively supported and have been skipped.\n"
157160
msg += "You need to install a suitable plugin for your async framework, for example:\n"
158161
msg += " - pytest-asyncio\n"

testing/acceptance_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,3 +1207,31 @@ async def test_2():
12071207
assert (
12081208
result.stdout.str().count("Coroutine functions are not natively supported") == 1
12091209
)
1210+
1211+
1212+
@pytest.mark.filterwarnings("default")
1213+
@pytest.mark.skipif(
1214+
sys.version_info < (3, 6), reason="async gen syntax available in Python 3.6+"
1215+
)
1216+
def test_warn_on_async_gen_function(testdir):
1217+
testdir.makepyfile(
1218+
test_async="""
1219+
async def test_1():
1220+
yield
1221+
async def test_2():
1222+
yield
1223+
"""
1224+
)
1225+
result = testdir.runpytest()
1226+
result.stdout.fnmatch_lines(
1227+
[
1228+
"test_async.py::test_1",
1229+
"test_async.py::test_2",
1230+
"*Coroutine functions are not natively supported*",
1231+
"*2 skipped, 2 warnings in*",
1232+
]
1233+
)
1234+
# ensure our warning message appears only once
1235+
assert (
1236+
result.stdout.str().count("Coroutine functions are not natively supported") == 1
1237+
)

0 commit comments

Comments
 (0)