Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
직접 명령은 검토 Prompt를 거치지 않습니다. 실행하기 전에 소스를 확인하세요.
npx skills add https://github.com/tomevault-io/skills-registry --skill fastapi-testing명령은 한 줄로 유지됩니다. 복사하기 전에 가로로 스크롤해 전체 내용을 확인하세요.
로컬 사본을 원하시나요? SkillsMP에서 현재 제공할 수 있는 파일을 다운로드하세요.
SOC 직업 분류 기준
SKILL.md 표시 중
| name | fastapi-testing |
| description | | Use when this capability is needed. |
Deep Knowledge: Use
mcp__documentation__fetch_docswith technology:fastapi-testingfor comprehensive documentation on TestClient, async testing, auth, and HTTP mocking.
from fastapi.testclient import TestClient
from myapp.main import app
client = TestClient(app)
def test_read_root():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Hello World"}
def test_post_item():
response = client.post(
"/items/",
json={"name": "Widget", "price": 9.99},
)
assert response.status_code == 201
def test_with_lifespan():
with TestClient(app) as client: # triggers startup/shutdown
response = client.get("/items/")
assert response.status_code == 200
import pytest
from httpx import ASGITransport, AsyncClient
from myapp.main import app
@pytest.mark.anyio
async def test_async():
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as ac:
response = await ac.get("/")
assert response.status_code == 200
# Shared fixture
@pytest.fixture(scope="module")
async def async_client():
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
yield client
# Production dependency
async def get_db() -> AsyncSession:
async with AsyncSessionLocal() as session:
yield session
# Test: override with test session
@pytest.fixture
def client(db_session):
def override_get_db():
yield db_session
app.dependency_overrides[get_db] = override_get_db
with TestClient(app) as c:
yield c
app.dependency_overrides.clear()
# Override user authentication
async def override_current_user():
return User(id=1, username="testuser", is_active=True)
app.dependency_overrides[get_current_user] = override_current_user
from datetime import timedelta
from myapp.auth import create_access_token
def get_test_token(username="testuser") -> str:
return create_access_token(
data={"sub": username},
expires_delta=timedelta(minutes=30),
)
def test_protected_endpoint():
token = get_test_token()
response = client.get(
"/users/me/",
headers={"Authorization": f"Bearer {token}"},
)
assert response.status_code == 200
def test_no_token():
response = client.get("/users/me/")
assert response.status_code == 401
def test_websocket():
with client.websocket_connect("/ws") as ws:
ws.send_text("hello")
data = ws.receive_text()
assert data == "Echo: hello"
def test_upload_file():
response = client.post(
"/uploadfile/",
files={"file": ("test.txt", b"hello world", "text/plain")},
)
assert response.status_code == 200
assert response.json() == {"filename": "test.txt", "size": 11}
from unittest.mock import patch
def test_task_called():
with patch("fastapi.BackgroundTasks.add_task") as mock:
response = client.post("/send-notification/user@example.com")
assert response.status_code == 200
mock.assert_called_once()
import httpx
import respx
@respx.mock
def test_external_api():
respx.get("https://api.example.com/users").mock(
return_value=httpx.Response(200, json=[{"id": 1, "name": "Alice"}])
)
response = client.get("/proxy/users")
assert response.status_code == 200
# With side effects
@respx.mock
async def test_async_mock():
respx.post("https://api.example.com/data").mock(
side_effect=httpx.ConnectError
)
with pytest.raises(httpx.ConnectError):
async with httpx.AsyncClient() as ac:
await ac.post("https://api.example.com/data")
import responses
import requests
@responses.activate
def test_with_responses():
responses.get(
"https://api.example.com/users",
json=[{"id": 1}],
status=200,
)
result = my_service.get_users()
assert len(result) == 1
def test_real_server(httpserver):
httpserver.expect_request("/data").respond_with_json({"key": "value"})
response = requests.get(httpserver.url_for("/data"))
assert response.json() == {"key": "value"}
| Anti-Pattern | Solution |
|---|---|
app.dependency_overrides not cleared | Use yield + app.dependency_overrides.clear() in fixture |
Creating TestClient per test | Module or session scope TestClient |
| Testing with real external APIs | Use respx/responses to mock |
Not using ASGITransport for async | Required for httpx with ASGI apps |
Official docs: https://fastapi.tiangolo.com/tutorial/testing/
Source: claude-dev-suite/claude-dev-suite — distributed by TomeVault.