Browse Source

🎨 [pre-commit.ci] Auto format from pre-commit.com hooks

pull/13163/head
pre-commit-ci[bot] 4 months ago
parent
commit
3f8448fc17
  1. 7
      docs/en/docs/advanced/async-tests.md
  2. 4
      docs_src/async_tests/test_main.py

7
docs/en/docs/advanced/async-tests.md

@ -59,13 +59,13 @@ $ pytest
## In Detail ## In Detail
The lifespan function demonstrates how to manage the lifecycle of application-wide resources. During the app's lifespan, we open a resource (`some_state_open`) at startup and clean it up (`some_state_close`) during shutdown. The lifespan function demonstrates how to manage the lifecycle of application-wide resources. During the app's lifespan, we open a resource (`some_state_open`) at startup and clean it up (`some_state_close`) during shutdown.
We use **ASGITransport** from **HTTPX** to interact directly with the **FastAPI** app in an async test environment. We use **ASGITransport** from **HTTPX** to interact directly with the **FastAPI** app in an async test environment.
When testing **FastAPI** apps with a custom lifespan, it's critical to manually trigger it in the test context to ensure proper setup and teardown of resources. When testing **FastAPI** apps with a custom lifespan, it's critical to manually trigger it in the test context to ensure proper setup and teardown of resources.
If you observe issues with state initialization or teardown in your tests, ensure that the lifespan is correctly invoked, and verify the app's state before and after requests. If you observe issues with state initialization or teardown in your tests, ensure that the lifespan is correctly invoked, and verify the app's state before and after requests.
## Other Asynchronous Function Calls ## Other Asynchronous Function Calls
@ -87,4 +87,3 @@ def event_loop() -> Generator[AbstractEventLoop, None, None]:
``` ```
/// ///

4
docs_src/async_tests/test_main.py

@ -6,7 +6,9 @@ from .main import app, lifespan
@pytest.mark.anyio @pytest.mark.anyio
async def test_root(): async def test_root():
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
async with lifespan(app=app): async with lifespan(app=app):
# Send a GET request to the root endpoint # Send a GET request to the root endpoint
response = await client.get("/") response = await client.get("/")

Loading…
Cancel
Save