lifespan handling for test client #2084
Answered
by
Kludex
PaulRudin
asked this question in
Potential Issue
-
import contextlib
from typing import TypedDict
from starlette.applications import Starlette
from starlette.testclient import TestClient
from starlette.routing import Route
from starlette.responses import PlainTextResponse
class State(TypedDict):
foo: int
@contextlib.asynccontextmanager
async def lifespan(app: Starlette) -> State:
yield {"foo": "42"}
async def hello(request):
return PlainTextResponse(request.app.state.foo)
app = Starlette(
lifespan=lifespan,
routes=[Route("/", hello)]
)
def test_client():
with TestClient(app) as client:
client.get("/")
if __name__ == "__main__":
test_client() running this gives: Shouldn't state have a foo attribute here? |
Beta Was this translation helpful? Give feedback.
Answered by
Kludex
Mar 15, 2023
Replies: 2 comments 2 replies
-
It should be request.state.foo, and not request.app.state.foo. They are different. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Kludex
-
So... if I want to patch state.foo in a test where can I patch it? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It should be request.state.foo, and not request.app.state.foo. They are different.