From bde1624566c299afbc2b3c469198cd405e5c06c5 Mon Sep 17 00:00:00 2001 From: Areeb455 <141515705+Areeb455@users.noreply.github.com> Date: Sat, 21 Feb 2026 23:01:38 +0000 Subject: [PATCH] docs: migrate to lifespan and update associated tests --- docs_src/app_testing/tutorial003.py | 28 +++++++++++++++++++ .../test_testing/test_tutorial003.py | 5 ++-- 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 docs_src/app_testing/tutorial003.py diff --git a/docs_src/app_testing/tutorial003.py b/docs_src/app_testing/tutorial003.py new file mode 100644 index 0000000000..45f6f678c6 --- /dev/null +++ b/docs_src/app_testing/tutorial003.py @@ -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"} \ No newline at end of file diff --git a/tests/test_tutorial/test_testing/test_tutorial003.py b/tests/test_tutorial/test_testing/test_tutorial003.py index 196fc45559..bbadb296bb 100644 --- a/tests/test_tutorial/test_testing/test_tutorial003.py +++ b/tests/test_tutorial/test_testing/test_tutorial003.py @@ -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() +