Skip to content

Why I can't use client: AsyncClinet in pytest #610

Closed Answered by seifertm
NightSkySK asked this question in Q&A
Discussion options

You must be logged in to vote

@NightSkySK Your issue is that you're using pytest-asyncio in strict mode, but you annotate your async fixtures with @pytest.fixture, rather than @pytest_asyncio.fixture.

Here is the reproducer I used to track down your issue:

from contextlib import asynccontextmanager
import pytest
import pytest_asyncio

class Client:
    async def get(self, s):
        print(s)


@asynccontextmanager
async def create_client():
    yield Client()

@pytest.mark.asyncio
async def test_create_client_inline():
    async with create_client() as client:
        await client.get("Hello")


@pytest_asyncio.fixture # works
#@pytest.fixture # fails
async def client():
    async with create_client() as c:
        y…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@seifertm
Comment options

Answer selected by NightSkySK
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants