pytest-dev / pytest-dev/pytest-asyncio
How do I know if a test function is async?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.7k
- Forks
- 207
- Avg merge
- 5h 35m
- Merged PRs (30d)
- 9
Description
Hey there, thanks for the awesome work. :)
I'm trying to check if a function is async or not within pytest_generate_tests. I'm using async_mode=auto and I would like to keep it that way.
My first bet was to simply use inspect.isawaitable(metafunc.function), but I quickly understood that pytest-asyncio wraps my async test functions into synchronous ones, which makes sense.
So the next thing I tried was:
def pytest_generate_tests(metafunc):
if metafunc.definition.get_closest_marker("asyncio"):
...
I would have expected this one to work, because the README says it should:
In auto mode, the
pytest.mark.asynciomarker can be omitted, the marker is added automatically to async test functions.
So either there's something obvious I'm missing here, or the README is wrong.
What I'm doing right now (but that's really just a hack):
def pytest_generate_tests(metafunc):
if inspect.getsource(metafunc.function)[0:5] == "async":
...
Is there any better way right now? If not, do you want me to open a PR to automatically mark async test functions with pytest.mark.asyncio when using async_mode=auto?
Cheers!
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with pytest_generate_tests and the README passage describing async_mode=auto and automatic marker insertion. Trace when metafunc.definition.get_closest_marker("asyncio") is evaluated relative to wrapping, then verify the behavior with a focused pytest-asyncio test. Done means the documented marker behavior is consistent with observed behavior and the test covers async auto mode.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100