Skip to content
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

[feature request] AsyncClient+ pytest fixture #25

Open
baggiponte opened this issue Sep 21, 2024 · 5 comments
Open

[feature request] AsyncClient+ pytest fixture #25

baggiponte opened this issue Sep 21, 2024 · 5 comments

Comments

@baggiponte
Copy link

Ciao! 😊

Was following tip number 5 and use httpx AsyncClient over the TestClient. So:

import pytest
from httpx import ASGITransport, AsyncClient

from path.to.app import app


@pytest.mark.asyncio
async def test_app_starts() -> None:
    async with AsyncClient(
        transport=ASGITransport(app),
        base_url="http://localhost:8080",
    ) as client:
        response = await client.get("/")
    assert response.status_code == 200

I'd like to make a fixture out of the client. So I tried this:

from collections.abc import AsyncGenerator

import pytest
from httpx import ASGITransport, AsyncClient

from path.to.app import app


@pytest.fixture
async def client() -> AsyncGenerator[AsyncClient, None]:
    async with AsyncClient(
        transport=ASGITransport(app),
        base_url="http://localhost:8080",
    ) as client:
        yield client


@pytest.mark.asyncio
async def test_app_starts(client: AsyncClient) -> None:
    response = await client.get("/")
    assert response.status_code == 200

However when I use the fixture I get the following error:

AttributeError: 'async_generator' object has no attribute 'get'

Do you have some guidance? 😊

@Kludex
Copy link
Owner

Kludex commented Sep 21, 2024

It's a pytest-asyncio problem.

I don't know what is it exactly, but if you use @pytest_asyncio.fixture instead of @pytest.fixture it solves the issue.

I use @pytest.mark.anyio instead of @pytest.mark.asyncio since anyio is already a dependency.

@Kludex Kludex closed this as completed Sep 21, 2024
@baggiponte
Copy link
Author

It's a pytest-asyncio problem.

I don't know what is it exactly, but if you use @pytest_asyncio.fixture instead of @pytest.fixture it solves the issue.

It works! 😊 Thanks. If you think it's worthy an addition, I'd be glad to open a PR (even just a callout on the page).

I use @pytest.mark.anyio instead of @pytest.mark.asyncio since anyio is already a dependency.

Tried this out but errors since it requires trio (which shouldn't be a required dependency, I think?).

@Kludex
Copy link
Owner

Kludex commented Sep 23, 2024

Tried this out but errors since it requires trio (which shouldn't be a required dependency, I think?).

This should set only asyncio, instead of using both asyncio and trio.

@pytest.fixture
def anyio_backend():
    return 'asyncio'

It works! 😊 Thanks. If you think it's worthy an addition, I'd be glad to open a PR (even just a callout on the page).

I think it would make sense to recommend pytest.mark.anyio instead of pytest.mark.asyncio just for convenience, since you already have it installed... I would accept a PR with this recommendation.

@baggiponte
Copy link
Author

Works! Will add this to the backlog and open a PR soon (hopefully).

@Kludex
Copy link
Owner

Kludex commented Sep 23, 2024

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants