Skip to content

Execute 'async def' test functions #2175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions _pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ def _is_unittest_unexpected_success_a_failure():
return sys.version_info >= (3, 4)


if hasattr(inspect, 'isawaitable'):
isawaitable = inspect.isawaitable
else:
def isawaitable(f):
return False


if _PY3:
def safe_str(v):
"""returns v as string"""
Expand Down
8 changes: 6 additions & 2 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
isclass, isfunction, is_generator, _escape_strings,
REGEX_TYPE, STRING_TYPES, NoneType, NOTSET,
get_real_func, getfslineno, safe_getattr,
getlocation, enum,
getlocation, enum, isawaitable,
)

cutdir1 = py.path.local(pluggy.__file__.rstrip("oc"))
Expand Down Expand Up @@ -151,7 +151,11 @@ def pytest_pyfunc_call(pyfuncitem):
testargs = {}
for arg in pyfuncitem._fixtureinfo.argnames:
testargs[arg] = funcargs[arg]
testfunction(**testargs)
testreturn = testfunction(**testargs)
if isawaitable(testreturn):
import asyncio
loop = asyncio.get_event_loop()
loop.run_until_complete(testreturn)
return True

def pytest_collect_file(path, parent):
Expand Down
14 changes: 14 additions & 0 deletions testing/test_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import sys

import pytest


@pytest.mark.skipif(sys.version_info < (3, 5), reason='async syntax available in Python 3.5+')
def test_async_function(testdir):
testdir.makepyfile("""
async def test_async_function_py35():
assert False
""")
# avoid importing asyncio into pytest's own process, which in turn imports logging (#8)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(['*1 failed*'])