-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpytest_asyncio_plugin.py
More file actions
32 lines (24 loc) · 1.03 KB
/
pytest_asyncio_plugin.py
File metadata and controls
32 lines (24 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Very small asyncio plugin for pytest.
The real project depends on ``pytest-asyncio`` to run ``async def`` test
functions. To keep the exercise self contained we provide a minimal
implementation that recognises ``@pytest.mark.asyncio`` and executes the test
coroutine using :func:`asyncio.run`.
"""
from __future__ import annotations
import asyncio
import inspect
from typing import Any
def pytest_configure(config) -> None: # pragma: no cover - plugin hook
config.addinivalue_line("markers", "asyncio: mark test as an async coroutine")
def pytest_pyfunc_call(pyfuncitem): # pragma: no cover - plugin hook
if inspect.iscoroutinefunction(pyfuncitem.function):
# Only pass fixtures that the test function actually expects to avoid
# unexpected keyword arguments from autouse fixtures.
kwargs = {
name: pyfuncitem.funcargs[name]
for name in pyfuncitem._fixtureinfo.argnames
}
coro = pyfuncitem.obj(**kwargs)
asyncio.run(coro)
return True
return None