@pytest.fixture
async def user(session: AsyncSession) -> User:
# Session here is another fixture, it works the same as DbContext in EF Core
user = User(...)
session.add(user)
await session.flush()
return user
@pytest.fixture
async def user_http_client(fastapi_app: ASGIApp, user: User) -> httpx.AsyncClient:
async with httpx.AsyncClient(
app=fastapi_app,
headers={"Authorization": "Get token somehow using user"},
) as client:
yield client
async def test_get_me(user_http_client: httpx.AsyncClient) -> None:
response = await user_http_client.get("/users/me")
assert response.status_code == status.HTTP_200_OK
...
@pytest.fixture
async def user(session: AsyncSession) -> User:
# Session here is another fixture, it works the same as DbContext in EF Core
user = User(...)
session.add(user)
await session.flush()
return user
@pytest.fixture
async def user_http_client(fastapi_app: ASGIApp, user: User) -> httpx.AsyncClient:
async with httpx.AsyncClient(
app=fastapi_app,
headers={"Authorization": "Get token somehow using user"},
) as client:
yield client
async def test_get_me(user_http_client: httpx.AsyncClient) -> None:
response = await user_http_client.get("/users/me")
assert response.status_code == status.HTTP_200_OK
...