I have tried everything I've been able to find, but I can't get async tests working.
I get RuntimeError: This event loop is already running
running TestClient
(which makes sense based on the docs), but I get httpx.ConnectError: [Errno 8] nodename nor servname provided, or not known
using httpx AsyncClient
.
I have a simple test:
@pytest.fixture(scope="module")
async def async_client() -> Generator:
async with AsyncClient(app=app, base_url='http://0.0.0.0') as client:
yield client
@pytest.mark.asyncio@mock.patch('apps.core.views.requests.get', new=mocked_get_request)
async def test_get_non_json_response(async_client: AsyncClient):
response = await async_client.get("/mymedia")
assertEqual(response.json()['error']['message']['message'], 'Not json')
Where /media
is:
@app.get('/mymedia')
async def my_media(request: Request, cache: RedisCacheBackend = Depends(redis_cache)):
return await my_media_ep(request, cache=cache)
my_media_ep
is a long function with multiple async api calls.
I have also tried it as suggested in the Async Tests docs, but get the same error.
Any suggestions or examples?