Skip to content

Commit d3a8a95

Browse files
committed
Execute 'async def' test functions
pytest didn't use the result of test functions but `async def` functions return an awaitable object. Though `async def` functions were just passed like they don't have any problem, their codes actually didn't run. For users, it was really hard to detect the tests really were run or not. This patch makes to check the result of test functions and if it is an awaitable object then actually run the functions to ensure its code is run.
1 parent 64cb67b commit d3a8a95

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

_pytest/python.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,15 @@ def pytest_pyfunc_call(pyfuncitem):
151151
testargs = {}
152152
for arg in pyfuncitem._fixtureinfo.argnames:
153153
testargs[arg] = funcargs[arg]
154-
testfunction(**testargs)
154+
testreturn = testfunction(**testargs)
155+
if inspect.isawaitable(testreturn):
156+
try:
157+
import asyncio
158+
except ImportError:
159+
pass
160+
else:
161+
loop = asyncio.get_event_loop()
162+
loop.run_until_complete(testreturn)
155163
return True
156164

157165
def pytest_collect_file(path, parent):

testing/test_async.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sys
2+
3+
import traceback
4+
import pytest
5+
6+
@pytest.mark.skipif(sys.version_info < (3, 5), reason='async syntax available in Python 3.5+')
7+
def test_async_function(testdir):
8+
testdir.makepyfile("""
9+
async def test_async_function_py35():
10+
assert False
11+
""")
12+
# avoid importing asyncio into pytest's own process, which in turn imports logging (#8)
13+
result = testdir.runpytest_subprocess()
14+
result.stdout.fnmatch_lines(['*1 failed*'])

0 commit comments

Comments
 (0)