docs: migrate to lifespan and update associated tests

This commit is contained in:
Areeb455 2026-02-21 23:01:38 +00:00
parent 473d68bd63
commit bde1624566
2 changed files with 30 additions and 3 deletions

View File

@ -0,0 +1,28 @@
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.testclient import TestClient
items = {}
@asynccontextmanager
async def lifespan(app: FastAPI):
items["foo"] = {"name": "Fighters"}
items["bar"] = {"name": "Tenders"}
yield
app = FastAPI(lifespan=lifespan)
@app.get("/items/{item_id}")
async def read_items(item_id: str):
return items[item_id]
def test_read_items():
with TestClient(app) as client:
response = client.get("/items/foo")
assert response.status_code == 200
assert response.json() == {"name": "Fighters"}

View File

@ -1,7 +1,6 @@
import pytest
def test_main():
with pytest.warns(DeprecationWarning):
from docs_src.app_testing.tutorial003_py310 import test_read_items
from docs_src.app_testing.tutorial003_py310 import test_read_items
test_read_items()