MCPcopy Create free account
hub / github.com/PacktPublishing/Building-Data-Science-Applications-with-FastAPI / client

Function client

tests/conftest.py:22–56  ·  view source on GitHub ↗
(
    request: pytest.FixtureRequest,
)

Source from the content-addressed store, hash-verified

20
21@pytest_asyncio.fixture
22async def client(
23 request: pytest.FixtureRequest,
24) -> AsyncGenerator[httpx.AsyncClient, None]:
25 marker = request.node.get_closest_marker("fastapi")
26 if marker is None:
27 raise ValueError("client fixture: the marker fastapi must be provided")
28 try:
29 app = marker.kwargs["app"]
30 except KeyError:
31 raise ValueError(
32 "client fixture: keyword argument app must be provided in the marker"
33 )
34 if not isinstance(app, FastAPI):
35 raise ValueError("client fixture: app must be a FastAPI instance")
36
37 dependency_overrides = marker.kwargs.get("dependency_overrides")
38 if dependency_overrides:
39 if not isinstance(dependency_overrides, dict):
40 raise ValueError(
41 "client fixture: dependency_overrides must be a dictionary"
42 )
43 app.dependency_overrides = dependency_overrides
44
45 run_lifespan_events = marker.kwargs.get("run_lifespan_events", True)
46 if not isinstance(run_lifespan_events, bool):
47 raise ValueError("client fixture: run_lifespan_events must be a bool")
48
49 test_client_generator = httpx.AsyncClient(app=app, base_url="http://app.io")
50 if run_lifespan_events:
51 async with LifespanManager(app):
52 async with test_client_generator as test_client:
53 yield test_client
54 else:
55 async with test_client_generator as test_client:
56 yield test_client
57
58
59@pytest.fixture

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected