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
Cannot inject HTTPX client as pytest fixture #185
Comments
From your stacktrace I don't understand how Could you please comment all |
Also, I see you are not using |
Thanks for the quick reply. Maybe as a bit of context. This test is trying to emulate a sync function that uses httpx to make a bunch of concurrent requests. Basically calling async from a sync function. It does this by putting the coroutines on the event loop like in the test. The sync function is buried deep in the app, so this is to contain the The pytest is synchronous, hence it doesn't require @mocketize
def test_mocket():
httpx_client = httpx.AsyncClient()
url = "https://example.org/"
data = {"message": "Hello"}
Entry.single_register(
Entry.GET,
url,
body=json.dumps(data),
headers={'content-type': 'application/json'}
)
loop = asyncio.get_event_loop()
coroutine = send_request(httpx_client, url)
actual = loop.run_until_complete(coroutine)
assert data == actual The registered mocks using |
I'll look into this, maybe this is the real problem! Thanks |
It doesn't look specifically like a httpx issue. This test passes (calling out to a real http API): import asyncio
import httpx
import pytest
from httpx import AsyncClient
async def send_request(client: AsyncClient, url: str) -> dict:
r = await client.get(url)
return r.json()
@pytest.fixture
def httpx_client() -> AsyncClient:
return httpx.AsyncClient()
def test_async(httpx_client: AsyncClient):
url = "https://dummyjson.com/products/1"
loop = asyncio.get_event_loop()
coroutine = send_request(httpx_client, url)
actual = loop.run_until_complete(coroutine)
assert "id" in actual However, if you add the decorator, you get the error below (different to the original one) Stacktrace
|
This looks like a |
It looks like the client from the fixture is living in a non-perfectly-mocked reality. Like you said, the fix is in enabling import json
import httpx
import pytest
from mocket.mockhttp import Entry
from mocket import Mocketizer
@pytest.fixture
def httpx_client() -> httpx.AsyncClient:
with Mocketizer():
yield httpx.AsyncClient()
@pytest.mark.asyncio
async def test_httpx(httpx_client):
url = "https://foo.bar/"
data = {"message": "Hello"}
Entry.single_register(
Entry.GET,
url,
body=json.dumps(data),
headers={"content-type": "application/json"},
)
async with httpx_client as client:
response = await client.get(url)
assert response.json() == data |
I've just added the above test to |
Hi,
Thanks for fixing the previous issue super quickly. However, I've noticed a
json.decoder.JSONDecodeError
problem when you inject HTTPX'AsyncClient
as a pytest fixture. If you put the@mocketize
decorator on the fixture it fixes the problem. However, this seems a bit odd.Stacktrack
Also, I know there's a lot of weird/questionable stuff going on in this test but I'm just trying to verify the behaviour around using async HTTPX within a sync app.
Thanks
The text was updated successfully, but these errors were encountered: