Coverage for tests / test_tutorial / test_custom_response / test_tutorial002_tutorial003_tutorial004.py: 100%

23 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 Is, snapshot 1abcd

6 

7 

8@pytest.fixture( 1abcd

9 name="mod_name", 

10 params=[ 

11 pytest.param("tutorial002_py310"), 

12 pytest.param("tutorial003_py310"), 

13 pytest.param("tutorial004_py310"), 

14 ], 

15) 

16def get_mod_name(request: pytest.FixtureRequest) -> str: 1abcd

17 return request.param 1pqrstuvwxyzABCDEFGHI

18 

19 

20@pytest.fixture(name="client") 1abcd

21def get_client(mod_name: str) -> TestClient: 1abcd

22 mod = importlib.import_module(f"docs_src.custom_response.{mod_name}") 1pqrstuvwxyzABCDEFGHI

23 return TestClient(mod.app) 1pqrstuvwxyzABCDEFGHI

24 

25 

26html_contents = """ 1abcd

27 <html> 

28 <head> 

29 <title>Some HTML in here</title> 

30 </head> 

31 <body> 

32 <h1>Look ma! HTML!</h1> 

33 </body> 

34 </html> 

35 """ 

36 

37 

38def test_get_custom_response(client: TestClient): 1abcd

39 response = client.get("/items/") 1JKLMNOPQRST

40 assert response.status_code == 200, response.text 1JKLMNOPQRST

41 assert response.text == html_contents 1JKLMNOPQRST

42 

43 

44def test_openapi_schema(client: TestClient, mod_name: str): 1abcd

45 if mod_name.startswith("tutorial003"): 1efghijklmno

46 response_content = {"application/json": {"schema": {}}} 1fikn

47 else: 

48 response_content = {"text/html": {"schema": {"type": "string"}}} 1eghjlmo

49 

50 response = client.get("/openapi.json") 1efghijklmno

51 assert response.status_code == 200, response.text 1efghijklmno

52 assert response.json() == snapshot( 1efghijklmno

53 { 

54 "openapi": "3.1.0", 

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

56 "paths": { 

57 "/items/": { 

58 "get": { 

59 "responses": { 

60 "200": { 

61 "description": "Successful Response", 

62 "content": Is(response_content), 

63 } 

64 }, 

65 "summary": "Read Items", 

66 "operationId": "read_items_items__get", 

67 } 

68 } 

69 }, 

70 } 

71 )