From db42bce7452a3c41083891794947ce2574911d6e Mon Sep 17 00:00:00 2001 From: z0z0r4 Date: Sun, 26 Jan 2025 08:55:25 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=F0=9F=8C=90=20Update=20testing=20events?= =?UTF-8?q?=20documentation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `lifespan` should be included in th document, instead of only `startup` and `shutdown` --- docs/en/docs/advanced/testing-events.md | 6 ++++-- docs_src/app_testing/tutorial003.py | 20 ++++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/docs/en/docs/advanced/testing-events.md b/docs/en/docs/advanced/testing-events.md index 0c554c4ec..6c800f279 100644 --- a/docs/en/docs/advanced/testing-events.md +++ b/docs/en/docs/advanced/testing-events.md @@ -1,5 +1,7 @@ # Testing Events: 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 your event handlers (`lifespan`, `startup` and `shutdown`) to run in your tests, you can use the `TestClient` with a `with` statement: -{* ../../docs_src/app_testing/tutorial003.py hl[9:12,20:24] *} +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) + +{* ../../docs_src/app_testing/tutorial003.py hl[9:15,32:37] *} \ No newline at end of file diff --git a/docs_src/app_testing/tutorial003.py b/docs_src/app_testing/tutorial003.py index ca6b45ce0..3b952dd73 100644 --- a/docs_src/app_testing/tutorial003.py +++ b/docs_src/app_testing/tutorial003.py @@ -1,15 +1,27 @@ +from contextlib import asynccontextmanager + from fastapi import FastAPI from fastapi.testclient import TestClient -app = FastAPI() - items = {} -@app.on_event("startup") -async def startup_event(): +@asynccontextmanager +async def lifespan(app: FastAPI): items["foo"] = {"name": "Fighters"} items["bar"] = {"name": "Tenders"} + yield + # clean up items or other work + ... + + +app = FastAPI(lifespan=lifespan) + +# startup event and shutdown event are deprecated, you should use lifespan instead +# @app.on_event("startup") +# async def startup_event(): +# items["foo"] = {"name": "Fighters"} +# items["bar"] = {"name": "Tenders"} @app.get("/items/{item_id}") From fdf38af8ac093e7ffb3958959774f2787d90e4ac Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 26 Jan 2025 00:56:29 +0000 Subject: [PATCH 02/13] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20f?= =?UTF-8?q?ormat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/en/docs/advanced/testing-events.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/docs/advanced/testing-events.md b/docs/en/docs/advanced/testing-events.md index 6c800f279..4312e8692 100644 --- a/docs/en/docs/advanced/testing-events.md +++ b/docs/en/docs/advanced/testing-events.md @@ -4,4 +4,4 @@ When you need your event handlers (`lifespan`, `startup` and `shutdown`) to run 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) -{* ../../docs_src/app_testing/tutorial003.py hl[9:15,32:37] *} \ No newline at end of file +{* ../../docs_src/app_testing/tutorial003.py hl[9:15,32:37] *} From 70c61b3bdd7a175acf4591018ddf38b3f32c6722 Mon Sep 17 00:00:00 2001 From: z0z0r4 Date: Sun, 26 Jan 2025 08:55:25 +0800 Subject: [PATCH 03/13] =?UTF-8?q?=F0=9F=8C=90=20Update=20testing=20events?= =?UTF-8?q?=20documentation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `lifespan` should be included in th document, instead of only `startup` and `shutdown` --- docs/en/docs/advanced/testing-events.md | 6 ++++-- docs_src/app_testing/tutorial003.py | 20 +++++++++++++++---- .../test_testing/test_tutorial003.py | 4 +--- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/docs/en/docs/advanced/testing-events.md b/docs/en/docs/advanced/testing-events.md index 0c554c4ec..6c800f279 100644 --- a/docs/en/docs/advanced/testing-events.md +++ b/docs/en/docs/advanced/testing-events.md @@ -1,5 +1,7 @@ # Testing Events: 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 your event handlers (`lifespan`, `startup` and `shutdown`) to run in your tests, you can use the `TestClient` with a `with` statement: -{* ../../docs_src/app_testing/tutorial003.py hl[9:12,20:24] *} +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) + +{* ../../docs_src/app_testing/tutorial003.py hl[9:15,32:37] *} \ No newline at end of file diff --git a/docs_src/app_testing/tutorial003.py b/docs_src/app_testing/tutorial003.py index ca6b45ce0..3b952dd73 100644 --- a/docs_src/app_testing/tutorial003.py +++ b/docs_src/app_testing/tutorial003.py @@ -1,15 +1,27 @@ +from contextlib import asynccontextmanager + from fastapi import FastAPI from fastapi.testclient import TestClient -app = FastAPI() - items = {} -@app.on_event("startup") -async def startup_event(): +@asynccontextmanager +async def lifespan(app: FastAPI): items["foo"] = {"name": "Fighters"} items["bar"] = {"name": "Tenders"} + yield + # clean up items or other work + ... + + +app = FastAPI(lifespan=lifespan) + +# startup event and shutdown event are deprecated, you should use lifespan instead +# @app.on_event("startup") +# async def startup_event(): +# items["foo"] = {"name": "Fighters"} +# items["bar"] = {"name": "Tenders"} @app.get("/items/{item_id}") diff --git a/tests/test_tutorial/test_testing/test_tutorial003.py b/tests/test_tutorial/test_testing/test_tutorial003.py index 2a5d67071..d9e16390e 100644 --- a/tests/test_tutorial/test_testing/test_tutorial003.py +++ b/tests/test_tutorial/test_testing/test_tutorial003.py @@ -1,7 +1,5 @@ -import pytest +from docs_src.app_testing.tutorial003 import test_read_items def test_main(): - with pytest.warns(DeprecationWarning): - from docs_src.app_testing.tutorial003 import test_read_items test_read_items() From e4e7c3ea8e3d346ebe71192c8adad6390af7f578 Mon Sep 17 00:00:00 2001 From: z0z0r4 Date: Tue, 15 Jul 2025 18:35:32 +0800 Subject: [PATCH 04/13] fox: Add single testing docs for lifespan --- docs/en/docs/advanced/testing-events.md | 12 +++++++--- docs_src/app_testing/tutorial003.py | 22 +++++------------- docs_src/app_testing/tutorial004.py | 30 +++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 20 deletions(-) create mode 100644 docs_src/app_testing/tutorial004.py diff --git a/docs/en/docs/advanced/testing-events.md b/docs/en/docs/advanced/testing-events.md index 4312e8692..9a5aa4bae 100644 --- a/docs/en/docs/advanced/testing-events.md +++ b/docs/en/docs/advanced/testing-events.md @@ -1,7 +1,13 @@ -# Testing Events: startup - shutdown +# Testing Events: lifespan and startup - shutdown + +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,26:30] *} -When you need your event handlers (`lifespan`, `startup` and `shutdown`) to run in your tests, you can use the `TestClient` with a `with` statement: 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) -{* ../../docs_src/app_testing/tutorial003.py hl[9:15,32:37] *} +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] *} \ No newline at end of file diff --git a/docs_src/app_testing/tutorial003.py b/docs_src/app_testing/tutorial003.py index 3b952dd73..d1a943cce 100644 --- a/docs_src/app_testing/tutorial003.py +++ b/docs_src/app_testing/tutorial003.py @@ -1,27 +1,15 @@ -from contextlib import asynccontextmanager - from fastapi import FastAPI from fastapi.testclient import TestClient +app = FastAPI() + items = {} -@asynccontextmanager -async def lifespan(app: FastAPI): +@app.on_event("startup") +async def startup_event(): items["foo"] = {"name": "Fighters"} items["bar"] = {"name": "Tenders"} - yield - # clean up items or other work - ... - - -app = FastAPI(lifespan=lifespan) - -# startup event and shutdown event are deprecated, you should use lifespan instead -# @app.on_event("startup") -# async def startup_event(): -# items["foo"] = {"name": "Fighters"} -# items["bar"] = {"name": "Tenders"} @app.get("/items/{item_id}") @@ -33,4 +21,4 @@ def test_read_items(): with TestClient(app) as client: response = client.get("/items/foo") assert response.status_code == 200 - assert response.json() == {"name": "Fighters"} + assert response.json() == {"name": "Fighters"} \ No newline at end of file diff --git a/docs_src/app_testing/tutorial004.py b/docs_src/app_testing/tutorial004.py new file mode 100644 index 000000000..076c00313 --- /dev/null +++ b/docs_src/app_testing/tutorial004.py @@ -0,0 +1,30 @@ +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 or other work + ... + + +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"} From f31a1bebf27fddcc4be2377e356d2a6ab34b145c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Jul 2025 10:36:08 +0000 Subject: [PATCH 05/13] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20f?= =?UTF-8?q?ormat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/en/docs/advanced/testing-events.md | 2 +- docs_src/app_testing/tutorial003.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/docs/advanced/testing-events.md b/docs/en/docs/advanced/testing-events.md index 9a5aa4bae..d67583de8 100644 --- a/docs/en/docs/advanced/testing-events.md +++ b/docs/en/docs/advanced/testing-events.md @@ -10,4 +10,4 @@ You can read more details about the ["Running lifespan in tests in the official 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] *} \ No newline at end of file +{* ../../docs_src/app_testing/tutorial003.py hl[9:12,20:24] *} diff --git a/docs_src/app_testing/tutorial003.py b/docs_src/app_testing/tutorial003.py index d1a943cce..ca6b45ce0 100644 --- a/docs_src/app_testing/tutorial003.py +++ b/docs_src/app_testing/tutorial003.py @@ -21,4 +21,4 @@ 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 + assert response.json() == {"name": "Fighters"} From 1b4351e5e5dbf484a4f5d12caa1522d98ca1661d Mon Sep 17 00:00:00 2001 From: z0z0r4 Date: Tue, 15 Jul 2025 19:02:49 +0800 Subject: [PATCH 06/13] docs: catch startup deprecated warning --- docs_src/app_testing/tutorial003.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/docs_src/app_testing/tutorial003.py b/docs_src/app_testing/tutorial003.py index d1a943cce..782530a83 100644 --- a/docs_src/app_testing/tutorial003.py +++ b/docs_src/app_testing/tutorial003.py @@ -1,3 +1,4 @@ +import warnings from fastapi import FastAPI from fastapi.testclient import TestClient @@ -5,11 +6,14 @@ app = FastAPI() items = {} +# startup event and shutdown event are deprecated, you should use lifespan instead +with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) -@app.on_event("startup") -async def startup_event(): - items["foo"] = {"name": "Fighters"} - items["bar"] = {"name": "Tenders"} + @app.on_event("startup") + async def startup_event(): + items["foo"] = {"name": "Fighters"} + items["bar"] = {"name": "Tenders"} @app.get("/items/{item_id}") From 950d0a2fec13ce667536d1a805e87805f5715aa7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Jul 2025 11:03:35 +0000 Subject: [PATCH 07/13] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20f?= =?UTF-8?q?ormat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs_src/app_testing/tutorial003.py | 1 + 1 file changed, 1 insertion(+) diff --git a/docs_src/app_testing/tutorial003.py b/docs_src/app_testing/tutorial003.py index 90513740e..53d8c894b 100644 --- a/docs_src/app_testing/tutorial003.py +++ b/docs_src/app_testing/tutorial003.py @@ -1,4 +1,5 @@ import warnings + from fastapi import FastAPI from fastapi.testclient import TestClient From 95722a8824fd22dfcfa2cf6ab6dbd30ad0c6751a Mon Sep 17 00:00:00 2001 From: z0z0r4 Date: Tue, 15 Jul 2025 19:05:04 +0800 Subject: [PATCH 08/13] Revert "docs: catch startup deprecated warning" This reverts commit 1b4351e5e5dbf484a4f5d12caa1522d98ca1661d. --- docs_src/app_testing/tutorial003.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/docs_src/app_testing/tutorial003.py b/docs_src/app_testing/tutorial003.py index 90513740e..ca6b45ce0 100644 --- a/docs_src/app_testing/tutorial003.py +++ b/docs_src/app_testing/tutorial003.py @@ -1,4 +1,3 @@ -import warnings from fastapi import FastAPI from fastapi.testclient import TestClient @@ -6,14 +5,11 @@ app = FastAPI() items = {} -# startup event and shutdown event are deprecated, you should use lifespan instead -with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) - @app.on_event("startup") - async def startup_event(): - items["foo"] = {"name": "Fighters"} - items["bar"] = {"name": "Tenders"} +@app.on_event("startup") +async def startup_event(): + items["foo"] = {"name": "Fighters"} + items["bar"] = {"name": "Tenders"} @app.get("/items/{item_id}") From 04a32e897f9b76d8d8bc136ff9f9531a03c947e0 Mon Sep 17 00:00:00 2001 From: z0z0r4 Date: Tue, 15 Jul 2025 19:07:24 +0800 Subject: [PATCH 09/13] fix: catch startup deprecated warning --- tests/test_tutorial/test_testing/test_tutorial003.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_tutorial/test_testing/test_tutorial003.py b/tests/test_tutorial/test_testing/test_tutorial003.py index d9e16390e..d2d980644 100644 --- a/tests/test_tutorial/test_testing/test_tutorial003.py +++ b/tests/test_tutorial/test_testing/test_tutorial003.py @@ -1,5 +1,7 @@ -from docs_src.app_testing.tutorial003 import test_read_items +import pytest def test_main(): - test_read_items() + with pytest.warns(DeprecationWarning): + from docs_src.app_testing.tutorial003 import test_read_items + test_read_items() \ No newline at end of file From 1a352a97764569e776b9a8e33272d2f83a44c3c4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Jul 2025 11:08:17 +0000 Subject: [PATCH 10/13] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20f?= =?UTF-8?q?ormat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_tutorial/test_testing/test_tutorial003.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_tutorial/test_testing/test_tutorial003.py b/tests/test_tutorial/test_testing/test_tutorial003.py index d2d980644..2a5d67071 100644 --- a/tests/test_tutorial/test_testing/test_tutorial003.py +++ b/tests/test_tutorial/test_testing/test_tutorial003.py @@ -4,4 +4,4 @@ import pytest def test_main(): with pytest.warns(DeprecationWarning): from docs_src.app_testing.tutorial003 import test_read_items - test_read_items() \ No newline at end of file + test_read_items() From f14743764d72c0aa1104ab259bc750c96c19e673 Mon Sep 17 00:00:00 2001 From: z0z0r4 Date: Tue, 15 Jul 2025 19:13:30 +0800 Subject: [PATCH 11/13] fix: Add test for tutorial004 --- tests/test_tutorial/test_testing/test_tutorial004.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 tests/test_tutorial/test_testing/test_tutorial004.py diff --git a/tests/test_tutorial/test_testing/test_tutorial004.py b/tests/test_tutorial/test_testing/test_tutorial004.py new file mode 100644 index 000000000..15ff1daba --- /dev/null +++ b/tests/test_tutorial/test_testing/test_tutorial004.py @@ -0,0 +1,5 @@ +from docs_src.app_testing.tutorial004 import test_read_items + + +def test_main(): + test_read_items() \ No newline at end of file From 7a31d0fd974a8ac3c87e2350207e6c539ef93b93 Mon Sep 17 00:00:00 2001 From: z0z0r4 Date: Tue, 15 Jul 2025 19:15:13 +0800 Subject: [PATCH 12/13] fix: lint --- tests/test_tutorial/test_testing/test_tutorial004.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_tutorial/test_testing/test_tutorial004.py b/tests/test_tutorial/test_testing/test_tutorial004.py index 15ff1daba..812ee44c1 100644 --- a/tests/test_tutorial/test_testing/test_tutorial004.py +++ b/tests/test_tutorial/test_testing/test_tutorial004.py @@ -2,4 +2,4 @@ from docs_src.app_testing.tutorial004 import test_read_items def test_main(): - test_read_items() \ No newline at end of file + test_read_items() From 644cfc4e638ad404cfb30eae49ee3c609d554061 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Jul 2025 11:15:15 +0000 Subject: [PATCH 13/13] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20f?= =?UTF-8?q?ormat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_tutorial/test_testing/test_tutorial004.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_tutorial/test_testing/test_tutorial004.py b/tests/test_tutorial/test_testing/test_tutorial004.py index 15ff1daba..812ee44c1 100644 --- a/tests/test_tutorial/test_testing/test_tutorial004.py +++ b/tests/test_tutorial/test_testing/test_tutorial004.py @@ -2,4 +2,4 @@ from docs_src.app_testing.tutorial004 import test_read_items def test_main(): - test_read_items() \ No newline at end of file + test_read_items()