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.

Mention the difference between `with TestClient(app) as client` and `client = TestClient(app)`

See original GitHub issue

Add 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:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
adriangbcommented, Jul 4, 2022
@contextlib.asynccontextmanager
async def lifespan(app):
    async with some_async_resource():
        yield

I thought this was the “recommended” way to do lifespans nowdays

0reactions
Kludexcommented, Jul 10, 2022

Closed by #1747

Read more comments on GitHub >

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

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