TestClient is not triggering on_startup events or on_shutdown events
See original GitHub issueIs your feature related to a problem? Please describe.
TestClient provides interface to make requests to Starlette app. What I expect is that also on_startup events and on_shutdown events are triggered when invoking TestClient. I want to test incoming requests that depend on the on_startup functions being called by Starlette app.
Describe the solution you would like.
with TestClient(app) as client:
# on_startup functions called
client.get('/')
# on_shutdown functions called
or at least on_startup is called in this code snippet:
from starlette.applications import Starlette
from starlette.testclient import TestClient
app = Starlette(
debug=True,
on_startup=[lambda: print('Startup')],
on_shutdown=[lambda: print('Shutdown')],
)
client = TestClient(app)
Describe alternatives you considered
I require on_startup async functions to be called for database initialization (Postgres Engine) and in this scenario I can’t use TestClient anymore
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
[BUG] TestClient doesn't trigger startup event if app is ... - GitHub
When using starlette's TestClient to test an application and importing the app module from a separate file, the startup events are not ......
Read more >Testing Events: startup - shutdown - FastAPI
When you need your event handlers ( startup and shutdown ) to run in your tests, you can use the TestClient with a...
Read more >How to test @app.on_event("shutdown") in FastAPI?
According to the documentation, you need to wrap it in the context manager (a with statement) to trigger the events, something like this:...
Read more >Events - Starlette
Starlette will not start serving any incoming requests until all of the registered startup handlers have completed. The shutdown handlers will run once...
Read more >Test Client Events tab - IBM
By selecting Saved Messages, you ensure that you are always working with the same set of values regardless of how many Test Client...
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 am sorry after reading more closely the code of TestClient there is already implementation of what I need:
this will do the startup and shutdown events
I think it should mention the information about the lifespan events