Registry / testing / pytest-fastapi-deps

pytest-fastapi-deps

JSON →
library0.2.3pypypi✓ verified 87d ago

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.

pip install pytest-fastapi-deps
INSTALL
IMPORT
SIG · PYTEST-FASTAPI-DEP
P
pytest-fastapi-deps
testingpythonv0.2.3
Install
5.0s avg
Import
1481ms
Disk
43MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 1.572s · 44.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 5.0s · import 1.390s · 44MB
43MB installed
● package 43MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fastapi_dep
from pytest_fastapi_deps import fastapi_dep
This is the correct import path.

Basic usage: override a dependency with a lambda.

from fastapi import FastAPI, Depends from pytest_fastapi_deps import fastapi_dep app = FastAPI() def get_token(): return "original" @app.get("/token") def read_token(token: str = Depends(get_token)): return {"token": token} # In test file import pytest from fastapi.testclient import TestClient @pytest.fixture def client(): with fastapi_dep(app).override({get_token: lambda: "overridden"}): yield TestClient(app) def test_token(client): response = client.get("/token") assert response.json() == {"token": "overridden"}
Debug
Known issues
gotchaThe fixture must be used as a context manager; nested overrides are allowed but order matters.
fix
Use `with fastapi_dep(app).override(...)` instead of assigning the fixture to a variable without entering the context.
affects: >=0.1.4
gotchaPlain objects can be passed as replacements since v0.2.0; prior versions required wrapping in a callable.
fix
Upgrade to 0.2.0+ or wrap plain objects in a lambda: `lambda: 'value'`
affects: <0.2.0
gotchaThe override is applied globally for the app instance during the context; ensure tests don't share the same app fixture if isolation is needed.
fix
Create a new app or use separate fixtures to avoid cross-test contamination.
affects: >=0.1.4
Errors
Common errors & fixes
AttributeError: module 'pytest_fastapi_deps' has no attribute 'fastapi_dep'
Incorrect import. Some users try `from pytest_fastapi_deps import fastapi_deps` (plural) or the wrong symbol.
fix
Use `from pytest_fastapi_deps import fastapi_dep` (singular).
ValueError: Dependency override must be a callable or a class
Passing a non-callable as the replacement in versions <0.2.0.
fix
Upgrade to 0.2.0+ or wrap the value in a lambda: `override={get_token: lambda: value}`
RuntimeError: No override context active
Calling `fastapi_dep(app).override(...)` outside of a `with` block, or after the context has exited.
fix
Use the method as a context manager: `with fastapi_dep(app).override(...):`
Upgrade
Version history
0.2.3latest on PyPI · released Jul 20, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
pytest-fastapi-deps — pip install pytest-fastapi-deps · libregistry