Skip to content

respx decorator breaks pytest fixture #210

@gregbrowndev

Description

@gregbrowndev

Hi,

Uncommenting the decorator on the test below throws an error:

TypeError: test_respx() missing 1 required positional argument: 'httpx_client'

@pytest.fixture
def httpx_client() -> AsyncClient:
    # note I know we should be using `async with...`
    return httpx.AsyncClient()


async def send_request(client: AsyncClient, url: str) -> dict:
    r = await client.get(url)
    return r.json()


# @respx.mock(assert_all_called=True)  # uncomment this
def test_respx(
    respx_mock: MockRouter,
    httpx_client: AsyncClient,
):
    url = "https://example.org/"
    data = {"message": "Hello"}

    respx_mock.get(url).mock(
        return_value=httpx.Response(200, json=data)
    )

    loop = asyncio.get_event_loop()
    
    coroutine = send_request(httpx_client, url)
    actual = loop.run_until_complete(coroutine)
    
    assert data == actual

I'm doing a few things that might seem odd:

  • I want to define httpx_client as a fixture - not odd, but I haven't seen this done in any of the docs. Note, creating AsyncClient in the test works fine.
  • The test is sync but testing HTTPX AsyncClient on asyncio.get_event_loop() instead of making the pytest async. This test is trying to prove some behaviour while we migrate to async HTTPX. Our application is synchronous but we want to make some concurrent requests within a deeply buried module.

Nonetheless, this seems like a bug in respx.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions