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

31 statements  

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

1import os 1abcd

2from pathlib import Path 1abcd

3 

4import pytest 1abcd

5from fastapi.testclient import TestClient 1abcd

6from inline_snapshot import snapshot 1abcd

7 

8from tests.utils import workdir_lock 1abcd

9 

10 

11@pytest.fixture(scope="module") 1abcd

12def client(): 1abcd

13 static_dir: Path = Path(os.getcwd()) / "static" 1efgh

14 static_dir.mkdir(exist_ok=True) 1efgh

15 sample_file = static_dir / "sample.txt" 1efgh

16 sample_file.write_text("This is a sample static file.") 1efgh

17 from docs_src.static_files.tutorial001_py310 import app 1efgh

18 

19 with TestClient(app) as client: 1iejfkglh

20 yield client 1efgh

21 sample_file.unlink() 1ijkl

22 static_dir.rmdir() 1ijkl

23 

24 

25@workdir_lock 1abcd

26def test_static_files(client: TestClient): 1abcd

27 response = client.get("/static/sample.txt") 1mnop

28 assert response.status_code == 200, response.text 1mnop

29 assert response.text == "This is a sample static file." 1mnop

30 

31 

32@workdir_lock 1abcd

33def test_static_files_not_found(client: TestClient): 1abcd

34 response = client.get("/static/non_existent_file.txt") 1uvwx

35 assert response.status_code == 404, response.text 1uvwx

36 

37 

38@workdir_lock 1abcd

39def test_openapi_schema(client: TestClient): 1abcd

40 response = client.get("/openapi.json") 1qrst

41 assert response.status_code == 200, response.text 1qrst

42 assert response.json() == snapshot( 1qrst

43 { 

44 "openapi": "3.1.0", 

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

46 "paths": {}, 

47 } 

48 )