Mention the difference between `with TestClient(app) as client` and `client = TestClient(app)`
See original GitHub issueAdd a note on the TestClient
section about the behavior difference between those two ways, i.e. lifespan events.
Considering the follow application:
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import Route
from starlette.testclient import TestClient
async def startup() -> None:
print("Startup event!")
async def endpoint(request: Request) -> Response:
return Response(status_code=204)
app = Starlette(routes=[Route("/", endpoint)], on_startup=[startup])
The following TestClient
prints “Startup event!”:
with TestClient(app) as client:
...
But the following, doesn’t:
client = TestClient(app)
Issue Analytics
- State:
- Created a year ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Test Client - Starlette
The test client allows you to make requests against your ASGI application, ... client = TestClient(app) # Set headers on the client for...
Read more >Testing - FastAPI
Create a TestClient by passing your FastAPI application to it. Create functions with a name that starts with test_ (this is standard pytest...
Read more >Testing FastAPI endpoints using fastapi.testclient - Qxf2 BLOG
This blog will help to write quick endpoint tests using fastapi testclient, when the application is built using FastAPI.
Read more >Can FastAPI test client be called by something external?
If you're going to do load testing of an application, use the same http stack as you'd do in production (so uvicorn, gunicorn,...
Read more >Testing Flask Applications — Flask Documentation (2.2.x)
Fixtures for the application, test client, and CLI runner are shown below, they can be placed in tests/conftest.py . If you're using an...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I thought this was the “recommended” way to do lifespans nowdays
Closed by #1747