Coverage for tests / test_tutorial / test_handling_errors / test_tutorial004.py: 100%
21 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1from fastapi.testclient import TestClient 1abcd
2from inline_snapshot import snapshot 1abcd
4from docs_src.handling_errors.tutorial004_py310 import app 1abcd
6client = TestClient(app) 1abcd
9def test_get_validation_error(): 1abcd
10 response = client.get("/items/foo") 1efg
11 assert response.status_code == 400, response.text 1efg
12 assert "Validation errors:" in response.text 1efg
13 assert "Field: ('path', 'item_id')" in response.text 1efg
16def test_get_http_error(): 1abcd
17 response = client.get("/items/3") 1hij
18 assert response.status_code == 418, response.text 1hij
19 assert response.content == b"Nope! I don't like 3." 1hij
22def test_get(): 1abcd
23 response = client.get("/items/2") 1klm
24 assert response.status_code == 200, response.text 1klm
25 assert response.json() == {"item_id": 2} 1klm
28def test_openapi_schema(): 1abcd
29 response = client.get("/openapi.json") 1nop
30 assert response.status_code == 200, response.text 1nop
31 assert response.json() == snapshot( 1nop
32 {
33 "openapi": "3.1.0",
34 "info": {"title": "FastAPI", "version": "0.1.0"},
35 "paths": {
36 "/items/{item_id}": {
37 "get": {
38 "responses": {
39 "200": {
40 "description": "Successful Response",
41 "content": {"application/json": {"schema": {}}},
42 },
43 "422": {
44 "description": "Validation Error",
45 "content": {
46 "application/json": {
47 "schema": {
48 "$ref": "#/components/schemas/HTTPValidationError"
49 }
50 }
51 },
52 },
53 },
54 "summary": "Read Item",
55 "operationId": "read_item_items__item_id__get",
56 "parameters": [
57 {
58 "required": True,
59 "schema": {"title": "Item Id", "type": "integer"},
60 "name": "item_id",
61 "in": "path",
62 }
63 ],
64 }
65 }
66 },
67 "components": {
68 "schemas": {
69 "ValidationError": {
70 "title": "ValidationError",
71 "required": ["loc", "msg", "type"],
72 "type": "object",
73 "properties": {
74 "loc": {
75 "title": "Location",
76 "type": "array",
77 "items": {
78 "anyOf": [{"type": "string"}, {"type": "integer"}]
79 },
80 },
81 "msg": {"title": "Message", "type": "string"},
82 "type": {"title": "Error Type", "type": "string"},
83 "input": {"title": "Input"},
84 "ctx": {"title": "Context", "type": "object"},
85 },
86 },
87 "HTTPValidationError": {
88 "title": "HTTPValidationError",
89 "type": "object",
90 "properties": {
91 "detail": {
92 "title": "Detail",
93 "type": "array",
94 "items": {
95 "$ref": "#/components/schemas/ValidationError"
96 },
97 }
98 },
99 },
100 }
101 },
102 }
103 )