-
Notifications
You must be signed in to change notification settings - Fork 162
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
How do I know if a test function is async? #376
Comments
Unfortunately, there is currently no publicly supported way to inspect tests in the way you want.
I agree this is ubiquitous. The
I think pytest-asyncio should expose a public attribute for wrapped tests, which allows the user to query if this is a pytest-asyncio test. Hypothesis sets |
AFAIK, markers aren't set at the function level but at the node level, so wrapping the coroutine should have no effect on the markers. Am I wrong? |
|
Yeah, that's pretty much what I ended up doing: import inspect
from _pytest.compat import get_real_func
def pytest_generate_tests(metafunc):
if inspect.iscoroutinefunction(get_real_func(metafunc.function)):
... IIRC you can't just call |
|
Mhh, if I understand correctly, Edit: You can see the real code here: https://github.com/Refty/mongo-thingy/blob/879fd7cf7437c63d7bc1e7e37e4dcf0cdf7ac882/tests/conftest.py#L55-L76 |
@ramnes You're right. Let's assume we want a similar function that works on Python functions/coroutines in pytest_generate_tests. That means the function would have to check whether strict mode or auto mode is used, whether an asyncio marker is present and perform some additional introspection of the Python function/coroutine to see if it's a Hypothesis test, for example. Technically, all this logic already exists, but it currently operates on pytest.Function items. I'm a bit reluctant to refactor the code away from pytest items to work on plain Python objects. I'm not really sure how to proceed with this issue at the moment. Your comment from July where you proposed using |
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 usingasync_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:
I would have expected this one to work, because the README says it should:
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):
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 usingasync_mode=auto
?Cheers!
The text was updated successfully, but these errors were encountered: