Skip to content

Commit 643c3f2

Browse files
rednafiadriangbKludex
authored
Explain 'TestClient(app) as client' vs 'client = TestClient(app)' (#1… (#1747)
* Explain 'TestClient(app) as client' vs 'client = TestClient(app)' (#1733) * Nit (#1733) * Update docs/testclient.md (#1733) Co-authored-by: Adrian Garcia Badaracco <[email protected]> * Update docs/testclient.md Co-authored-by: Adrian Garcia Badaracco <[email protected]> * Update docs/testclient.md Co-authored-by: Marcelo Trylesinski <[email protected]> * Update docs/testclient.md Co-authored-by: Marcelo Trylesinski <[email protected]> Co-authored-by: Adrian Garcia Badaracco <[email protected]> Co-authored-by: Marcelo Trylesinski <[email protected]>
1 parent e7717af commit 643c3f2

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

docs/testclient.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ application. Occasionally you might want to test the content of 500 error
6262
responses, rather than allowing client to raise the server exception. In this
6363
case you should use `client = TestClient(app, raise_server_exceptions=False)`.
6464

65+
!!! note
66+
67+
If you want the `TestClient` to run `lifespan` events (`on_startup`, `on_shutdown`, or `lifespan`),
68+
you will need to use the `TestClient` as a context manager. Otherwise, the events
69+
will not be triggered when the `TestClient` is instantiated. You can learn more about it
70+
[here](/events/#running-event-handlers-in-tests).
71+
6572
### Selecting the Async backend
6673

6774
`TestClient` takes arguments `backend` (a string) and `backend_options` (a dictionary).

0 commit comments

Comments
 (0)