Skip to content

How do I know if a test function is async? #376

Open
@ramnes

Description

@ramnes

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.asyncio marker 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions