Skip to content
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

backport #6927: run async testcase methods #7068

Merged
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
1 change: 1 addition & 0 deletions changelog/6924.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure a ``unittest.IsolatedAsyncioTestCase`` is actually awaited.
26 changes: 22 additions & 4 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,33 @@ def async_warn(nodeid: str) -> None:
@hookimpl(trylast=True)
def pytest_pyfunc_call(pyfuncitem: "Function"):
testfunction = pyfuncitem.obj
if iscoroutinefunction(testfunction) or (
sys.version_info >= (3, 6) and inspect.isasyncgenfunction(testfunction)
):

try:
# ignoring type as the import is invalid in py37 and mypy thinks its a error
from unittest import IsolatedAsyncioTestCase # type: ignore
except ImportError:
async_ok_in_stdlib = False
else:
async_ok_in_stdlib = isinstance(
getattr(testfunction, "__self__", None), IsolatedAsyncioTestCase
)

if (
iscoroutinefunction(testfunction)
or (sys.version_info >= (3, 6) and inspect.isasyncgenfunction(testfunction))
) and not async_ok_in_stdlib:
async_warn(pyfuncitem.nodeid)
funcargs = pyfuncitem.funcargs
testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}
result = testfunction(**testargs)
if hasattr(result, "__await__") or hasattr(result, "__aiter__"):
async_warn(pyfuncitem.nodeid)
if async_ok_in_stdlib:
# todo: investigate moving this to the unittest plugin
# by a test call result hook
testcase = testfunction.__self__
testcase._callMaybeAsync(lambda: result)
else:
async_warn(pyfuncitem.nodeid)
return True


Expand Down
2 changes: 2 additions & 0 deletions testing/example_scripts/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
# dummy pytest.ini to ease direct running of example scripts
15 changes: 15 additions & 0 deletions testing/example_scripts/unittest/test_unittest_asyncio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from unittest import IsolatedAsyncioTestCase # type: ignore


class AsyncArguments(IsolatedAsyncioTestCase):
async def test_something_async(self):
async def addition(x, y):
return x + y

self.assertEqual(await addition(2, 2), 4)

async def test_something_async_fails(self):
async def addition(x, y):
return x + y

self.assertEqual(await addition(2, 2), 3)
8 changes: 8 additions & 0 deletions testing/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,3 +1129,11 @@ def test(self):
result = testdir.runpytest("--trace", str(p1))
assert len(calls) == 2
assert result.ret == 0


def test_async_support(testdir):
pytest.importorskip("unittest.async_case")

testdir.copy_example("unittest/test_unittest_asyncio.py")
reprec = testdir.inline_run()
reprec.assertoutcome(failed=1, passed=1)