fastapi/docs/zh-hant/docs/advanced/testing-events.md

618 B
Raw Blame History

測試事件lifespan 與 startup - shutdown

當你需要在測試中執行 lifespan生命週期你可以使用 TestClient 並搭配 with 陳述式:

{* ../../docs_src/app_testing/tutorial004_py310.py hl[9:15,18,27:28,30:32,41:43] *}

你可以閱讀更多細節:在測試中執行 lifespanStarlette 官方文件)。

對於已棄用的 startupshutdown 事件,你可以這樣使用 TestClient

{* ../../docs_src/app_testing/tutorial003_py310.py hl[9:12,20:24] *}