Не проходят интеграционные тесты, если их больше 1, FastAPI async 'NoneType' object has no attribute 'send'
Есть 2 функции, которые создают заказ и выводят инфу о заказах. По отдельности тесты проходят, но если сразу 2 теста писать, то возвращается ошибка 'NoneType' object has no attribute 'send'. Не могу понять, что не так.
@pytest_asyncio.fixture()
async def client():
async with AsyncClient(
transport=ASGITransport(app=app),
base_url="http://test"
) as ac:
yield ac
@pytest.mark.asyncio
async def test_post_create_order(client):
response = await client.post("/orders/create_order", json={
"name": "name",
"description": "description",
"price": 100,
"image_url": "image",
"quantity": 1,
})
assert response.status_code == 200
data = response.json()
print('otvet', data)
assert data == {
'status_code': 201,
'transaction': 'Successful'
}
@pytest.mark.asyncio
async def test_get_all_orders(client):
response = await client.get("/orders/all_orders")
print(response)
assert response.status_code == 200
data = response.json()
Ошибка:
2025-03-09 13:32:26.289 | ERROR | app.main:log_middleware:23 - Request to /orders/all_orders failed: 'NoneType' object has no attribute 'send'
<Response [500 Internal Server Error]>
Ответы (1 шт):
Автор решения: Илья Белоусов
→ Ссылка
Хм, нашел инфу, поставить в декораторе loop_scope="session", и все тесты проходят, но не совсем понимаю, почему это сработало и что вообще оно делает
@pytest_asyncio.fixture(loop_scope="session")
async def client():
async with AsyncClient(
transport=ASGITransport(app=app),
base_url="http://test"
) as ac:
yield ac
@pytest.mark.asyncio(loop_scope="session")
async def test_post_create_order(client):
response = await client.post("/orders/create_order", json={
"name": "name",
"description": "description",
"price": 100,
"image_url": "image",
"quantity": 1,
})
assert response.status_code == 200
data = response.json()
print('otvet', data)
assert data == {
'status_code': 201,
'transaction': 'Successful'
}
@pytest.mark.asyncio(loop_scope="session")
async def test_get_all_orders(client):
response = await client.get("/orders/all_orders")
print(response)
assert response.status_code == 200
data = response.json()
print(data)
# assert len(data) > 1