Skip to content

Pytest override session dependency  #153

@foarsitter

Description

@foarsitter

When using the fastapi.testclient.TestClient with pytest, the app doesn't use the session fixture resulting in writes to the database.

To solve this problem, pytest needs to tell the app to use the same session as used for testing. This can be done by using dependency_overrides.

@pytest.fixture(autouse=True)
def _app_session(app, session):
    app.dependency_overrides[fastapi_sqla.sqla.default_session_dep] = lambda: session

At least, this is how I solved the problem. Is there a beter approach or should I send a PR to document this?

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