TestClient - sync tests
Testing# pip install httpx pytest
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_read_root():
res = client.get("/")
assert res.status_code == 200
assert res.json() == {"message": "Hello!"}
def test_create_item():
res = client.post("/items", json={"name": "Foo", "price": 9.99})
assert res.status_code == 201
assert res.json()["name"] == "Foo"
def test_not_found():
res = client.get("/items/99999")
assert res.status_code == 404
def test_auth_header():
res = client.get("/me", headers={"Authorization": "Bearer fake-token"})
assert res.status_code == 401