fastapi
1from fastapi import APIRouter, FastAPI
2from fastapi.testclient import TestClient
3
4router = APIRouter()
5
6sub_router = APIRouter()
7
8app = FastAPI()
9
10
11@sub_router.get("/")
12def read_item():
13return {"id": "foo"}
14
15
16router.include_router(sub_router, prefix="/items")
17
18app.include_router(router)
19
20client = TestClient(app)
21
22
23def test_path_operation():
24response = client.get("/items/")
25assert response.status_code == 200, response.text
26assert response.json() == {"id": "foo"}
27
28
29def test_openapi_schema():
30response = client.get("/openapi.json")
31assert response.status_code == 200, response.text
32assert response.json() == {
33"openapi": "3.1.0",
34"info": {"title": "FastAPI", "version": "0.1.0"},
35"paths": {
36"/items/": {
37"get": {
38"responses": {
39"200": {
40"description": "Successful Response",
41"content": {"application/json": {"schema": {}}},
42}
43},
44"summary": "Read Item",
45"operationId": "read_item_items__get",
46}
47}
48},
49}
50