Coverage for tests / test_tutorial / test_dependencies / test_tutorial008c.py: 100%
29 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1import importlib 1abcd
2from types import ModuleType 1abcd
4import pytest 1abcd
5from fastapi.exceptions import FastAPIError 1abcd
6from fastapi.testclient import TestClient 1abcd
9@pytest.fixture( 1abcd
10 name="mod",
11 params=[
12 pytest.param("tutorial008c_py310"),
13 pytest.param("tutorial008c_an_py310"),
14 ],
15)
16def get_mod(request: pytest.FixtureRequest): 1abcd
17 mod = importlib.import_module(f"docs_src.dependencies.{request.param}") 1KLMNOPQRSTUVWXYZ0123456789
19 return mod 1KLMNOPQRSTUVWXYZ0123456789
22def test_get_no_item(mod: ModuleType): 1abcd
23 client = TestClient(mod.app) 1efghijkl
24 response = client.get("/items/foo") 1efghijkl
25 assert response.status_code == 404, response.text 1efghijkl
26 assert response.json() == {"detail": "Item not found, there's only a plumbus here"} 1efghijkl
29def test_get(mod: ModuleType): 1abcd
30 client = TestClient(mod.app) 1mnopqrst
31 response = client.get("/items/plumbus") 1mnopqrst
32 assert response.status_code == 200, response.text 1mnopqrst
33 assert response.json() == "plumbus" 1mnopqrst
36def test_fastapi_error(mod: ModuleType): 1abcd
37 client = TestClient(mod.app) 1uvwxyzAB
38 with pytest.raises(FastAPIError) as exc_info: 1uvwxyzAB
39 client.get("/items/portal-gun") 1uvwxyzAB
40 assert "raising an exception and a dependency with yield" in exc_info.value.args[0] 1uvwxyzAB
43def test_internal_server_error(mod: ModuleType): 1abcd
44 client = TestClient(mod.app, raise_server_exceptions=False) 1CDEFGHIJ
45 response = client.get("/items/portal-gun") 1CDEFGHIJ
46 assert response.status_code == 500, response.text 1CDEFGHIJ
47 assert response.text == "Internal Server Error" 1CDEFGHIJ