Complete worked example
python
from fastapi import FastAPI
from fastapi.testclient import TestClient
def inspect_example(request):
app = FastAPI()
@app.get("/hello")
def hello():
return {"message": "hello"}
with TestClient(app) as client:
response = client.request(request.get("method", "GET"), request.get("path", "/hello"))
body = None if request.get("method") == "HEAD" else response.json()
return {"status": response.status_code, "body": body}- Reasoning
- The decorator associates the path and method with an endpoint. Returning a dictionary lets FastAPI produce a JSON response. A different path produces a not-found response, and a different method on the same path is not automatically another accepted operation. These are distinct contracts that a function-only test can miss. Each inspection creates a fresh application and enters the client context. There is no database, remote service or live feed. The helper returns only the response status and JSON body, allowing exact comparisons while leaving the supplied request description unchanged.