Coverage for tests / test_tutorial / test_dependencies / test_tutorial008c.py: 100%

29 statements  

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

1import importlib 1abdc

2from types import ModuleType 1abdc

3 

4import pytest 1abdc

5from fastapi.exceptions import FastAPIError 1abdc

6from fastapi.testclient import TestClient 1abdc

7 

8 

9@pytest.fixture( 1abdc

10 name="mod", 

11 params=[ 

12 pytest.param("tutorial008c_py310"), 

13 pytest.param("tutorial008c_an_py310"), 

14 ], 

15) 

16def get_mod(request: pytest.FixtureRequest): 1abdc

17 mod = importlib.import_module(f"docs_src.dependencies.{request.param}") 1abc

18 

19 return mod 1abc

20 

21 

22def test_get_no_item(mod: ModuleType): 1abdc

23 client = TestClient(mod.app) 1efg

24 response = client.get("/items/foo") 1efg

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

26 assert response.json() == {"detail": "Item not found, there's only a plumbus here"} 1efg

27 

28 

29def test_get(mod: ModuleType): 1abdc

30 client = TestClient(mod.app) 1hij

31 response = client.get("/items/plumbus") 1hij

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

33 assert response.json() == "plumbus" 1hij

34 

35 

36def test_fastapi_error(mod: ModuleType): 1abdc

37 client = TestClient(mod.app) 1klm

38 with pytest.raises(FastAPIError) as exc_info: 1klm

39 client.get("/items/portal-gun") 1klm

40 assert "raising an exception and a dependency with yield" in exc_info.value.args[0] 1klm

41 

42 

43def test_internal_server_error(mod: ModuleType): 1abdc

44 client = TestClient(mod.app, raise_server_exceptions=False) 1nop

45 response = client.get("/items/portal-gun") 1nop

46 assert response.status_code == 500, response.text 1nop

47 assert response.text == "Internal Server Error" 1nop