| 1 |
from fastapi import FastAPI |
| 2 |
from fastapi.testclient import TestClient |
| 3 |
|
| 4 |
app = FastAPI() |
| 5 |
|
| 6 |
items = {} |
| 7 |
|
| 8 |
|
| 9 |
@app.on_event("startup") |
| 10 |
async def startup_event(): |
| 11 |
items["foo"] = {"name": "Fighters"} |
| 12 |
items["bar"] = {"name": "Tenders"} |
| 13 |
|
| 14 |
|
| 15 |
@app.get("/items/{item_id}") |
| 16 |
async def read_items(item_id: str): |
| 17 |
return items[item_id] |
| 18 |
|
| 19 |
|
| 20 |
def test_read_items(): |
| 21 |
with TestClient(app) as client: |
| 22 |
response = client.get("/items/foo") |
| 23 |
assert response.status_code == 200 |
| 24 |
assert response.json() == {"name": "Fighters"} |