]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
docs: add section about `TestClient(client=...)` (#2817)
authorMarcelo Trylesinski <marcelotryle@gmail.com>
Sat, 28 Dec 2024 07:23:40 +0000 (08:23 +0100)
committerGitHub <noreply@github.com>
Sat, 28 Dec 2024 07:23:40 +0000 (08:23 +0100)
docs/testclient.md

index 99346af8bfa543fd0919b1fb042f4c7c971f9a94..258186b0456825d49ce9195a4273dfb6c5494702 100644 (file)
@@ -1,4 +1,13 @@
 
+??? abstract "API Reference"
+    ::: starlette.testclient.TestClient
+        options:
+            parameter_headings: false
+            show_root_heading: true
+            heading_level: 3
+            filters:
+                - "__init__"
+
 The test client allows you to make requests against your ASGI application,
 using the `httpx` library.
 
@@ -69,10 +78,21 @@ case you should use `client = TestClient(app, raise_server_exceptions=False)`.
     not be triggered when the `TestClient` is instantiated. You can learn more about it
     [here](lifespan.md#running-lifespan-in-tests).
 
+### Change client address
+
+By default, the TestClient will set the client host to `"testserver"` and the port to `50000`.
+
+You can change the client address by setting the `client` attribute of the `TestClient` instance:
+
+```python
+client = TestClient(app, client=('http://localhost', 8000))
+```
+
 ### Selecting the Async backend
 
 `TestClient` takes arguments `backend` (a string) and `backend_options` (a dictionary).
-These options are passed to `anyio.start_blocking_portal()`. See the [anyio documentation](https://anyio.readthedocs.io/en/stable/basics.html#backend-options)
+These options are passed to `anyio.start_blocking_portal()`.
+See the [anyio documentation](https://anyio.readthedocs.io/en/stable/basics.html#backend-options)
 for more information about the accepted backend options.
 By default, `asyncio` is used with default options.