Skip to content

Commit cfac682

Browse files
WIP: first stake at running stdli asyncio unittests again
1 parent 1d244b3 commit cfac682

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/_pytest/python.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,26 @@ def async_warn(nodeid: str) -> None:
175175
@hookimpl(trylast=True)
176176
def pytest_pyfunc_call(pyfuncitem: "Function"):
177177
testfunction = pyfuncitem.obj
178-
if iscoroutinefunction(testfunction) or (
179-
sys.version_info >= (3, 6) and inspect.isasyncgenfunction(testfunction)
180-
):
178+
179+
try:
180+
# ignoring type as the import is invalid in py37 and mypy thinks its a error
181+
from unittest import IsolatedAsyncioTestCase # type: ignore
182+
except ImportError:
183+
async_ok_in_stdlib = False
184+
else:
185+
async_ok_in_stdlib = isinstance(testfunction.__self__, IsolatedAsyncioTestCase)
186+
187+
if (
188+
iscoroutinefunction(testfunction)
189+
or (sys.version_info >= (3, 6) and inspect.isasyncgenfunction(testfunction))
190+
) and not async_ok_in_stdlib:
181191
async_warn(pyfuncitem.nodeid)
182192
funcargs = pyfuncitem.funcargs
183193
testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}
184194
result = testfunction(**testargs)
185-
if hasattr(result, "__await__") or hasattr(result, "__aiter__"):
195+
if (
196+
hasattr(result, "__await__") or hasattr(result, "__aiter__")
197+
) and not async_ok_in_stdlib:
186198
async_warn(pyfuncitem.nodeid)
187199
return True
188200

testing/example_scripts/pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[pytest]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from unittest import IsolatedAsyncioTestCase # type: ignore
2+
3+
4+
class AsyncArguments(IsolatedAsyncioTestCase):
5+
async def test_something_async(self):
6+
async def addition(x, y):
7+
return x + y
8+
9+
self.assertEqual(await addition(2, 2), 4)
10+
11+
async def test_something_async_fails(self):
12+
async def addition(x, y):
13+
return x + y
14+
15+
self.assertEqual(await addition(2, 2), 3)

0 commit comments

Comments
 (0)