Coverage for tests / test_tutorial / test_static_files / test_tutorial001.py: 100%

27 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import os 1abdc

2from pathlib import Path 1abdc

3 

4import pytest 1abdc

5from fastapi.testclient import TestClient 1abdc

6from inline_snapshot import snapshot 1abdc

7 

8 

9@pytest.fixture(scope="module") 1abdc

10def client(): 1abdc

11 static_dir: Path = Path(os.getcwd()) / "static" 1abc

12 static_dir.mkdir(exist_ok=True) 1abc

13 sample_file = static_dir / "sample.txt" 1abc

14 sample_file.write_text("This is a sample static file.") 1abc

15 from docs_src.static_files.tutorial001_py310 import app 1abc

16 

17 with TestClient(app) as client: 1abc

18 yield client 1abc

19 sample_file.unlink() 1abc

20 static_dir.rmdir() 1abc

21 

22 

23def test_static_files(client: TestClient): 1abdc

24 response = client.get("/static/sample.txt") 1efg

25 assert response.status_code == 200, response.text 1efg

26 assert response.text == "This is a sample static file." 1efg

27 

28 

29def test_static_files_not_found(client: TestClient): 1abdc

30 response = client.get("/static/non_existent_file.txt") 1klm

31 assert response.status_code == 404, response.text 1klm

32 

33 

34def test_openapi_schema(client: TestClient): 1abdc

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

36 assert response.status_code == 200, response.text 1hij

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

38 { 

39 "openapi": "3.1.0", 

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

41 "paths": {}, 

42 } 

43 )