This skill should be used when the user asks to "debug backend tests", "fix pytest failures", "analyze Python errors", "fix FastAPI bugs", or mentions keywords like "pytest", "IntegrityError", "ValidationError", "SQLAlchemy", "FastAPI". It provides the complete bugfix workflow knowledge including error classification, confidence scoring, and TDD best practices for Python/FastAPI backends.
Provides TDD workflows for debugging Python/FastAPI backend tests. Triggers on "debug backend tests", "pytest failures", or keywords like IntegrityError, ValidationError, SQLAlchemy, FastAPI.
/plugin marketplace add penkzhou/swiss-army-knife-plugin/plugin install swiss-army-knife@swiss-army-knife-pluginThis skill inherits all available tools. When active, it can use any tool Claude has access to.
本 skill 提供后端测试 bugfix 的完整工作流知识,包括错误分类体系、置信度评分系统和 TDD 最佳实践。
后端测试失败主要分为以下类型(按频率排序):
症状:数据库连接失败、查询错误、事务问题
识别特征:
IntegrityError、OperationalErrorsqlalchemy.exc.* 异常UNIQUE constraint failed解决策略:正确处理事务边界
# Before - 事务未正确处理
def create_user(db: Session, user: UserCreate):
db_user = User(**user.dict())
db.add(db_user)
db.commit() # 失败时无回滚
return db_user
# After - 使用 try/except 确保事务安全
def create_user(db: Session, user: UserCreate):
try:
db_user = User(**user.dict())
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
except IntegrityError:
db.rollback()
raise HTTPException(status_code=409, detail="User already exists")
症状:输入验证失败、Schema 不匹配
识别特征:
ValidationErrorpydantic.error_wrappers422 Unprocessable Entityfield required 错误解决策略:完善 Pydantic Schema
# Before - 缺少验证
class UserCreate(BaseModel):
email: str # 没有格式验证
# After - 使用 Pydantic 验证器
class UserCreate(BaseModel):
email: EmailStr
@field_validator('email')
@classmethod
def email_must_be_valid(cls, v):
if not v or '@' not in v:
raise ValueError('Invalid email format')
return v.lower()
症状:端点返回错误状态码、路由不匹配
识别特征:
HTTPException404 Not Found、405 Method Not Allowed解决策略:检查路由定义和请求方法
# 确保端点定义正确
@router.get("/users/{user_id}", response_model=UserResponse)
async def get_user(user_id: int, db: Session = Depends(get_db)):
user = db.query(User).filter(User.id == user_id).first()
if not user:
raise HTTPException(status_code=404, detail="User not found")
return user
症状:认证失败、权限不足
识别特征:
401 Unauthorized403 Forbiddencredentials 验证失败解决策略:检查认证流程和 Token 处理
# 确保 Token 验证正确
async def get_current_user(token: str = Depends(oauth2_scheme)):
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
user_id: str = payload.get("sub")
if user_id is None:
raise credentials_exception
except JWTError:
raise credentials_exception
return user_id
症状:异步操作超时、并发问题
识别特征:
TimeoutErrorCancelledErrorasyncio 相关异常await 关键字解决策略:正确使用 async/await
# Before - 忘记 await
async def get_data():
result = fetch_from_external_api() # 缺少 await
return result
# After - 正确等待异步操作
async def get_data():
result = await fetch_from_external_api()
return result
症状:配置加载失败、环境变量缺失
识别特征:
KeyErrorenvironment 相关错误settings 加载失败解决策略:使用 Pydantic Settings 管理配置
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
database_url: str
secret_key: str
class Config:
env_file = ".env"
settings = Settings()
| 分数 | 级别 | 行为 |
|---|---|---|
| 80+ | 高 | 自动执行 |
| 60-79 | 中 | 标记验证后继续 |
| 40-59 | 低 | 暂停询问用户 |
| <40 | 不确定 | 停止收集信息 |
置信度 = 证据质量(40%) + 模式匹配(30%) + 上下文完整性(20%) + 可复现性(10%)
证据质量:
模式匹配:
上下文完整性:
可复现性:
import pytest
from fastapi.testclient import TestClient
def test_create_user_duplicate_email(client: TestClient, db_session):
"""测试重复邮箱应返回 409"""
# 1. 设置前置条件
client.post("/api/users", json={"email": "test@example.com", "name": "User 1"})
# 2. 执行被测操作
response = client.post("/api/users", json={"email": "test@example.com", "name": "User 2"})
# 3. 断言期望结果
assert response.status_code == 409
assert "already exists" in response.json()["detail"]
# 只写让测试通过的最小代码
# 不要优化,不要添加额外功能
def create_user(db: Session, user: UserCreate):
existing = db.query(User).filter(User.email == user.email).first()
if existing:
raise HTTPException(status_code=409, detail="User already exists")
# ... 创建用户逻辑
# 改善代码结构
# 保持测试通过
# 消除重复
# 提取公共逻辑到服务层
| 检查项 | 标准 |
|---|---|
| 测试通过率 | 100% |
| 代码覆盖率 | >= 90% |
| 新代码覆盖率 | 100% |
| Lint (flake8) | 无错误 |
| TypeCheck (mypy) | 无错误 |
@pytest.fixture
def db_session():
"""创建测试数据库会话"""
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
yield session
session.close()
@pytest.fixture
def client(db_session):
"""创建测试客户端"""
def override_get_db():
yield db_session
app.dependency_overrides[get_db] = override_get_db
return TestClient(app)
import pytest
@pytest.mark.asyncio
async def test_async_operation():
result = await some_async_function()
assert result is not None
@pytest.mark.parametrize("status_code,detail", [
(400, "Invalid input"),
(404, "Not found"),
(409, "Already exists"),
])
def test_error_responses(client, status_code, detail):
# 测试多种错误场景
pass
# 运行后端测试
make test TARGET=backend
# 运行特定测试
make test TARGET=backend FILTER=test_create_user
# 或使用 pytest 直接运行
pytest tests/ -k "test_create_user" -v
# 覆盖率检查
pytest --cov=app --cov-report=term-missing --cov-fail-under=90
# Lint 检查
flake8 app/ tests/
# 类型检查
mypy app/
# 完整 QA
make qa
文档路径由配置指定(best_practices_dir),使用以下关键词搜索:
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code, generative art, algorithmic art, flow fields, or particle systems. Create original algorithmic art rather than copying existing artists' work to avoid copyright violations.
Applies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.
Create beautiful visual art in .png and .pdf documents using design philosophy. You should use this skill when the user asks to create a poster, piece of art, design, or other static piece. Create original visual designs, never copying existing artists' work to avoid copyright violations.