question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TestClient is not triggering on_startup events or on_shutdown events

See original GitHub issue

Is 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:closed
  • Created 2 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Kolumbscommented, Oct 5, 2021

I am sorry after reading more closely the code of TestClient there is already implementation of what I need:

with TestClient(app) as client:
    client.request("GET", "/")

this will do the startup and shutdown events

0reactions
Kludexcommented, Dec 18, 2021

I think it should mention the information about the lifespan events

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found