{"id":21805,"library":"pytest-fastapi-deps","title":"pytest-fastapi-deps","description":"A pytest fixture that allows easy replacement of FastAPI dependencies for testing. Version 0.2.3 (PyPI). It wraps FastAPI's dependency override mechanism in a clean fixture, enabling overrides at the test level with context managers. Release cadence is irregular.","status":"active","version":"0.2.3","language":"python","source_language":"en","source_url":"https://github.com/pksol/pytest-fastapi-deps","tags":["pytest","fastapi","testing","dependency-injection","fixture"],"install":[{"cmd":"pip install pytest-fastapi-deps","lang":"bash","label":"Install from PyPI"}],"dependencies":[],"imports":[{"note":"This is the correct import path.","symbol":"fastapi_dep","correct":"from pytest_fastapi_deps import fastapi_dep"}],"quickstart":{"code":"from fastapi import FastAPI, Depends\nfrom pytest_fastapi_deps import fastapi_dep\n\napp = FastAPI()\n\ndef get_token():\n    return \"original\"\n\n@app.get(\"/token\")\ndef read_token(token: str = Depends(get_token)):\n    return {\"token\": token}\n\n# In test file\nimport pytest\nfrom fastapi.testclient import TestClient\n\n@pytest.fixture\ndef client():\n    with fastapi_dep(app).override({get_token: lambda: \"overridden\"}):\n        yield TestClient(app)\n\ndef test_token(client):\n    response = client.get(\"/token\")\n    assert response.json() == {\"token\": \"overridden\"}","lang":"python","description":"Basic usage: override a dependency with a lambda."},"warnings":[{"fix":"Use `with fastapi_dep(app).override(...)` instead of assigning the fixture to a variable without entering the context.","message":"The fixture must be used as a context manager; nested overrides are allowed but order matters.","severity":"gotcha","affected_versions":">=0.1.4"},{"fix":"Upgrade to 0.2.0+ or wrap plain objects in a lambda: `lambda: 'value'`","message":"Plain objects can be passed as replacements since v0.2.0; prior versions required wrapping in a callable.","severity":"gotcha","affected_versions":"<0.2.0"},{"fix":"Create a new app or use separate fixtures to avoid cross-test contamination.","message":"The override is applied globally for the app instance during the context; ensure tests don't share the same app fixture if isolation is needed.","severity":"gotcha","affected_versions":">=0.1.4"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Use `from pytest_fastapi_deps import fastapi_dep` (singular).","cause":"Incorrect import. Some users try `from pytest_fastapi_deps import fastapi_deps` (plural) or the wrong symbol.","error":"AttributeError: module 'pytest_fastapi_deps' has no attribute 'fastapi_dep'"},{"fix":"Upgrade to 0.2.0+ or wrap the value in a lambda: `override={get_token: lambda: value}`","cause":"Passing a non-callable as the replacement in versions <0.2.0.","error":"ValueError: Dependency override must be a callable or a class"},{"fix":"Use the method as a context manager: `with fastapi_dep(app).override(...):`","cause":"Calling `fastapi_dep(app).override(...)` outside of a `with` block, or after the context has exited.","error":"RuntimeError: No override context active"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}