Coverage for tests / test_tutorial / test_custom_request_and_route / test_tutorial003.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import importlib 1abcd

2 

3import pytest 1abcd

4from fastapi.testclient import TestClient 1abcd

5 

6from tests.utils import needs_py310 1abcd

7 

8 

9@pytest.fixture( 1abcd

10 name="client", 

11 params=[ 

12 pytest.param("tutorial003_py310", marks=needs_py310), 

13 ], 

14) 

15def get_client(request: pytest.FixtureRequest): 1abcd

16 mod = importlib.import_module(f"docs_src.custom_request_and_route.{request.param}") 1ijklmnop

17 

18 client = TestClient(mod.app) 1ijklmnop

19 return client 1ijklmnop

20 

21 

22def test_get(client: TestClient): 1abcd

23 response = client.get("/") 1qrst

24 assert response.json() == {"message": "Not timed"} 1qrst

25 assert "X-Response-Time" not in response.headers 1qrst

26 

27 

28def test_get_timed(client: TestClient): 1abcd

29 response = client.get("/timed") 1efgh

30 assert response.json() == {"message": "It's the time of my life"} 1efgh

31 assert "X-Response-Time" in response.headers 1efgh

32 assert float(response.headers["X-Response-Time"]) >= 0 1efgh