Coverage for tests / test_tutorial / test_first_steps / test_tutorial001_tutorial002_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

5from inline_snapshot import snapshot 1abcd

6 

7 

8@pytest.fixture( 1abcd

9 name="client", 

10 params=[ 

11 "tutorial001_py310", 

12 "tutorial003_py310", 

13 ], 

14) 

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

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

17 client = TestClient(mod.app) 1efghijklmnopqrstuvwx

18 return client 1efghijklmnopqrstuvwx

19 

20 

21@pytest.mark.parametrize( 1abcd

22 "path,expected_status,expected_response", 

23 [ 

24 ("/", 200, {"message": "Hello World"}), 

25 ("/nonexistent", 404, {"detail": "Not Found"}), 

26 ], 

27) 

28def test_get_path(client: TestClient, path, expected_status, expected_response): 1abcd

29 response = client.get(path) 1yzABCDEFGHIJKL

30 assert response.status_code == expected_status 1yzABCDEFGHIJKL

31 assert response.json() == expected_response 1yzABCDEFGHIJKL

32 

33 

34def test_openapi_schema(client: TestClient): 1abcd

35 response = client.get("/openapi.json") 1MNOPQRST

36 assert response.status_code == 200 1MNOPQRST

37 assert response.json() == snapshot( 1MNOPQRST

38 { 

39 "openapi": "3.1.0", 

40 "info": {"title": "FastAPI", "version": "0.1.0"}, 

41 "paths": { 

42 "/": { 

43 "get": { 

44 "responses": { 

45 "200": { 

46 "description": "Successful Response", 

47 "content": {"application/json": {"schema": {}}}, 

48 } 

49 }, 

50 "summary": "Root", 

51 "operationId": "root__get", 

52 } 

53 } 

54 }, 

55 } 

56 )