Skip to content

Fix FastAPI lifespan integration in pytest #393

Open
@Zaczero

Description

@Zaczero

Describe the bug

Currently because of broken lifespan integration in pytest, you need to duplicate code like:

@pytest_asyncio.fixture(scope="session")
async def app_client(app):
    await create_index_templates()
    await create_collection_index()

    async with AsyncClient(
        transport=ASGITransport(app=app), base_url="http://test-server"
    ) as c:
        yield c

despite having:

@asynccontextmanager
async def lifespan(app: FastAPI):
    """Lifespan handler for FastAPI app. Initializes index templates and collections at startup."""
    await create_index_templates()
    await create_collection_index()
    yield

It's fairly simple to fix, feel free to use this code:

This should make the tests more consistent with the actual deployment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions