Browse Source
commitpull/14015/head176cd8c9efAuthor: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Sun Aug 31 15:20:12 2025 +0000 📝 Update release notes [skip ci] commit1884d76f61Author: Sebastián Ramírez <[email protected]> Date: Sun Aug 31 17:19:49 2025 +0200 🔧 Update sponsors: remove Platform.sh (#14027) commit8062aabdaaAuthor: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Sun Aug 31 15:02:29 2025 +0000 📝 Update release notes [skip ci] commitee9ccac1e5Author: Sebastián Ramírez <[email protected]> Date: Sun Aug 31 17:02:08 2025 +0200 🔧 Update sponsors: remove Mobb (#14026) commit5cd4c3b6bdAuthor: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Sun Aug 31 10:59:17 2025 +0000 📝 Update release notes [skip ci] commit4584f706bdAuthor: Jom Karlo Verzosa <[email protected]> Date: Sun Aug 31 18:58:56 2025 +0800 📝 Add deprecation info block about `dict()` in `docs/tutorial/body.md` (#13906) Co-authored-by: Sebastián Ramírez <[email protected]> commitba9c8fba0bAuthor: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Sun Aug 31 10:50:12 2025 +0000 📝 Update release notes [skip ci] commitd9249c1949Author: Valentyn <[email protected]> Date: Sun Aug 31 06:49:48 2025 -0400 📝 Fix Twitter to be X (Twitter) everywhere in documentation (#13809) Co-authored-by: Valentyn Druzhynin <[email protected]> Co-authored-by: Sebastián Ramírez <[email protected]> commita973e787afAuthor: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Sun Aug 31 10:33:32 2025 +0000 📝 Update release notes [skip ci] commit1088d2abd9Author: Ashish Pandey <[email protected]> Date: Sun Aug 31 16:17:57 2025 +0545 🐛 Prevent scroll-to-top on restart/fast buttons in `termynal.js` (#13714) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> commit98ec6a6079Author: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Sun Aug 31 10:29:48 2025 +0000 📝 Update release notes [skip ci] commit6b4d292f3aAuthor: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Sun Aug 31 10:29:27 2025 +0000 📝 Update release notes [skip ci] commitd4ddcc5878Author: z0z0r4 <[email protected]> Date: Sun Aug 31 18:29:21 2025 +0800 📝 Update testing events documentation (#13259) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Motov Yurii <[email protected]> Co-authored-by: Sebastián Ramírez <[email protected]> commit0e5832aa6dAuthor: Hotah Ma <[email protected]> Date: Sun Aug 31 18:29:01 2025 +0800 📝 Remove obsolete `url` field in error responses in docs (#13655) Co-authored-by: Motov Yurii <[email protected]> commitee2acd8abcAuthor: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Sun Aug 31 10:03:35 2025 +0000 📝 Update release notes [skip ci] commite902ed5fc6Author: Arnaud Durand <[email protected]> Date: Sun Aug 31 12:03:10 2025 +0200 📝 Bring the `scope` claim in line with the standard in `docs_src/security/tutorial005.py` (#11189) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Yurii Motov <[email protected]> commitcef1f166dfAuthor: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Sun Aug 31 09:59:28 2025 +0000 📝 Update release notes [skip ci] commit8e63f75919Author: Soul Lee <[email protected]> Date: Sun Aug 31 18:59:07 2025 +0900 📝 Update TrustedHostMiddleware Documentation (#11441) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Alejandra <[email protected]> Co-authored-by: Sofie Van Landeghem <[email protected]> Co-authored-by: Sebastián Ramírez <[email protected]> Co-authored-by: Motov Yurii <[email protected]> commite1b9cc00caAuthor: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Sun Aug 31 09:56:48 2025 +0000 📝 Update release notes [skip ci] commit408b8a9bebAuthor: Denny Biasiolli <[email protected]> Date: Sun Aug 31 11:56:21 2025 +0200 📝 Remove links to site callbackhell.com that doesn't exist anymore (#14006) commit0817c955ecAuthor: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Sun Aug 31 09:16:03 2025 +0000 📝 Update release notes [skip ci] commitc55f7138a1Author: Motov Yurii <[email protected]> Date: Sun Aug 31 11:15:41 2025 +0200 📝 Add permalinks to headers in English docs (#13993) Co-authored-by: Sebastián Ramírez <[email protected]> commit7653de2715Author: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Sun Aug 31 09:11:36 2025 +0000 📝 Update release notes [skip ci] commit784f068abaAuthor: Sebastián Ramírez <[email protected]> Date: Sun Aug 31 11:11:15 2025 +0200 🛠️ Update `mkdocs_hooks` to handle headers with permalinks when building docs (#14025) Co-authored-by: Yurii Motov <[email protected]> commit6db05770f6Author: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Mon Aug 25 20:03:24 2025 +0000 📝 Update release notes [skip ci] commit6be02e3d52Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon Aug 25 22:03:02 2025 +0200 ⬆ [pre-commit.ci] pre-commit autoupdate (#14016) updates: - [github.com/astral-sh/ruff-pre-commit: v0.12.9 → v0.12.10](https://github.com/astral-sh/ruff-pre-commit/compare/v0.12.9...v0.12.10) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> commit9cf7b70d7bAuthor: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Wed Aug 20 09:11:20 2025 +0000 📝 Update release notes [skip ci] commitf75c1532f6Author: Motov Yurii <[email protected]> Date: Wed Aug 20 11:10:51 2025 +0200 ⬆ Bump `mkdocs-macros-plugin` from 1.3.7 to 1.3.9 (#14003) Bump mkdocs-macros-plugin from 1.3.7 to 1.3.9 commit5c3a70d5b6Author: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Mon Aug 18 21:07:25 2025 +0000 📝 Update release notes [skip ci] commit6a45249303Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon Aug 18 23:07:04 2025 +0200 ⬆ [pre-commit.ci] pre-commit autoupdate (#13999) updates: - [github.com/astral-sh/ruff-pre-commit: v0.12.8 → v0.12.9](https://github.com/astral-sh/ruff-pre-commit/compare/v0.12.8...v0.12.9) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> commitc23051682bAuthor: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Date: Mon Aug 18 06:35:06 2025 +0000 📝 Update release notes [skip ci] commitdf779885faAuthor: Mika <[email protected]> Date: Mon Aug 18 14:34:40 2025 +0800 📝 Fix code include for Pydantic models example in `docs/zh/docs/python-types.md` (#13997) Updated the Pydantic expiration example in the Chinese documentation
102 changed files with 321 additions and 272 deletions
@ -16,7 +16,7 @@ You can learn more about [FastAPI versions and how to pin and upgrade them](http |
|||
|
|||
If you think you found a vulnerability, and even if you are not sure about it, please report it right away by sending an email to: [email protected]. Please try to be as explicit as possible, describing all the steps and example code to reproduce the security issue. |
|||
|
|||
I (the author, [@tiangolo](https://twitter.com/tiangolo)) will review it thoroughly and get back to you. |
|||
I (the author, [@tiangolo](https://x.com/tiangolo)) will review it thoroughly and get back to you. |
|||
|
|||
## Public Discussions |
|||
|
|||
|
|||
@ -1,5 +1,12 @@ |
|||
# Testing Events: startup - shutdown { #testing-events-startup-shutdown } |
|||
# Testing Events: lifespan and startup - shutdown { #testing-events-lifespan-and-startup-shutdown } |
|||
|
|||
When you need your event handlers (`startup` and `shutdown`) to run in your tests, you can use the `TestClient` with a `with` statement: |
|||
When you need `lifespan` to run in your tests, you can use the `TestClient` with a `with` statement: |
|||
|
|||
{* ../../docs_src/app_testing/tutorial004.py hl[9:15,18,27:28,30:32,41:43] *} |
|||
|
|||
|
|||
You can read more details about the ["Running lifespan in tests in the official Starlette documentation site."](https://www.starlette.io/lifespan/#running-lifespan-in-tests) |
|||
|
|||
For the deprecated `startup` and `shutdown` events, you can use the `TestClient` as follows: |
|||
|
|||
{* ../../docs_src/app_testing/tutorial003.py hl[9:12,20:24] *} |
|||
|
|||
@ -0,0 +1,43 @@ |
|||
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 |
|||
# clean up items |
|||
items.clear() |
|||
|
|||
|
|||
app = FastAPI(lifespan=lifespan) |
|||
|
|||
|
|||
@app.get("/items/{item_id}") |
|||
async def read_items(item_id: str): |
|||
return items[item_id] |
|||
|
|||
|
|||
def test_read_items(): |
|||
# Before the lifespan starts, "items" is still empty |
|||
assert items == {} |
|||
|
|||
with TestClient(app) as client: |
|||
# Inside the "with TestClient" block, the lifespan starts and items added |
|||
assert items == {"foo": {"name": "Fighters"}, "bar": {"name": "Tenders"}} |
|||
|
|||
response = client.get("/items/foo") |
|||
assert response.status_code == 200 |
|||
assert response.json() == {"name": "Fighters"} |
|||
|
|||
# After the requests is done, the items are still there |
|||
assert items == {"foo": {"name": "Fighters"}, "bar": {"name": "Tenders"}} |
|||
|
|||
# The end of the "with TestClient" block simulates terminating the app, so |
|||
# the lifespan ends and items are cleaned up |
|||
assert items == {} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue