From 643c3f20e385341a37c851a1d12ff3c33762c300 Mon Sep 17 00:00:00 2001 From: Redowan Delowar Date: Sun, 10 Jul 2022 18:23:40 +0600 Subject: [PATCH] =?UTF-8?q?Explain=20'TestClient(app)=20as=20client'=20vs?= =?UTF-8?q?=20'client=20=3D=20TestClient(app)'=20(#1=E2=80=A6=20(#1747)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Explain 'TestClient(app) as client' vs 'client = TestClient(app)' (#1733) * Nit (#1733) * Update docs/testclient.md (#1733) Co-authored-by: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> * Update docs/testclient.md Co-authored-by: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> * Update docs/testclient.md Co-authored-by: Marcelo Trylesinski * Update docs/testclient.md Co-authored-by: Marcelo Trylesinski Co-authored-by: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Co-authored-by: Marcelo Trylesinski --- docs/testclient.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/testclient.md b/docs/testclient.md index 64c766df..f64c570b 100644 --- a/docs/testclient.md +++ b/docs/testclient.md @@ -62,6 +62,13 @@ application. Occasionally you might want to test the content of 500 error responses, rather than allowing client to raise the server exception. In this case you should use `client = TestClient(app, raise_server_exceptions=False)`. +!!! note + + If you want the `TestClient` to run `lifespan` events (`on_startup`, `on_shutdown`, or `lifespan`), + you will need to use the `TestClient` as a context manager. Otherwise, the events + will not be triggered when the `TestClient` is instantiated. You can learn more about it + [here](/events/#running-event-handlers-in-tests). + ### Selecting the Async backend `TestClient` takes arguments `backend` (a string) and `backend_options` (a dictionary).