Api Test Suite Builder

alirezarezvani/claude-skills/engineering/skills/api-test-suite-builder

作者 alirezarezvani19392f7a0826无许可证27K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5周前更新

Use when the user asks to generate API tests, create integration test suites, test REST endpoints, or build contract tests.

AI 生成的概览

扫描 API 路由定义,为 Node 或 Python 框架生成可直接运行的集成测试套件。

功能
该技能扫描 Next.js App Router、Express、FastAPI 和 Django REST 代码库中的 API 路由定义,构建路由映射。随后生成测试文件,覆盖身份验证、输入校验、错误码、分页、文件上传和速率限制,Node 使用 Vitest 加 Supertest,Python 使用 Pytest 加 httpx。它还提供测试矩阵、命名指导、常见陷阱和最佳实践。
适用场景
适用于新增 API 并希望在实现前生成测试脚手架、旧 API 没有测试需要建立基线覆盖,或准备进行契约审查、发布前回归检查和安全审计时。
运行要求
仅包含说明,不附带脚本。它依赖 find、grep 等 shell 工具扫描源文件,生成的测试面向 Vitest 加 Supertest 或 Pytest 加 httpx。

API Test Suite Builder

Tier: POWERFUL Category: Engineering Domain: Testing / API Quality


Overview

Scans API route definitions across frameworks (Next.js App Router, Express, FastAPI, Django REST) and auto-generates comprehensive test suites covering auth, input validation, error codes, pagination, file uploads, and rate limiting. Outputs ready-to-run test files for Vitest+Supertest (Node) or Pytest+httpx (Python).


Core Capabilities

  • Route detection — scan source files to extract all API endpoints
  • Auth coverage — valid/invalid/expired tokens, missing auth header
  • Input validation — missing fields, wrong types, boundary values, injection attempts
  • Error code matrix — 400/401/403/404/422/500 for each route
  • Pagination — first/last/empty/oversized pages
  • File uploads — valid, oversized, wrong MIME type, empty
  • Rate limiting — burst detection, per-user vs global limits

When to Use

  • New API added — generate test scaffold before writing implementation (TDD)
  • Legacy API with no tests — scan and generate baseline coverage
  • API contract review — verify existing tests match current route definitions
  • Pre-release regression check — ensure all routes have at least smoke tests
  • Security audit prep — generate adversarial input tests

Route Detection

Next.js App Router

bash
# Find all route handlersfind ./app/api -name "route.ts" -o -name "route.js" | sort
# Extract HTTP methods from each route filegrep -rn "export async function\|export function" app/api/**/route.ts | \  grep -oE "(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)" | sort -u
# Full route mapfind ./app/api -name "route.ts" | while read f; do  route=$(echo $f | sed 's|./app||' | sed 's|/route.ts||')  methods=$(grep -oE "export (async )?function (GET|POST|PUT|PATCH|DELETE)" "$f" | \    grep -oE "(GET|POST|PUT|PATCH|DELETE)")  echo "$methods $route"done

Express

bash
# Find all router filesfind ./src -name "*.ts" -o -name "*.js" | xargs grep -l "router\.\(get\|post\|put\|delete\|patch\)" 2>/dev/null
# Extract routes with line numbersgrep -rn "router\.\(get\|post\|put\|delete\|patch\)\|app\.\(get\|post\|put\|delete\|patch\)" \  src/ --include="*.ts" | grep -oE "(get|post|put|delete|patch)\(['\"][^'\"]*['\"]"
# Generate route mapgrep -rn "router\.\|app\." src/ --include="*.ts" | \  grep -oE "\.(get|post|put|delete|patch)\(['\"][^'\"]+['\"]" | \  sed "s/\.\(.*\)('\(.*\)'/\U\1 \2/"

FastAPI

bash
# Find all route decoratorsgrep -rn "@app\.\|@router\." . --include="*.py" | \  grep -E "@(app|router)\.(get|post|put|delete|patch)"
# Extract with path and function namegrep -rn "@\(app\|router\)\.\(get\|post\|put\|delete\|patch\)" . --include="*.py" | \  grep -oE "@(app|router)\.(get|post|put|delete|patch)\(['\"][^'\"]*['\"]"

Django REST Framework

bash
# urlpatterns extractiongrep -rn "path\|re_path\|url(" . --include="*.py" | grep "urlpatterns" -A 50 | \  grep -E "path\(['\"]" | grep -oE "['\"][^'\"]+['\"]" | head -40
# ViewSet router registrationgrep -rn "router\.register\|DefaultRouter\|SimpleRouter" . --include="*.py"

Test Generation Patterns

Auth Test Matrix

For every authenticated endpoint, generate:

Test CaseExpected Status
No Authorization header401
Invalid token format401
Valid token, wrong user role403
Expired JWT token401
Valid token, correct role2xx
Token from deleted user401

Input Validation Matrix

For every POST/PUT/PATCH endpoint with a request body:

Test CaseExpected Status
Empty body {}400 or 422
Missing required fields (one at a time)400 or 422
Wrong type (string where int expected)400 or 422
Boundary: value at min-1400 or 422
Boundary: value at min2xx
Boundary: value at max2xx
Boundary: value at max+1400 or 422
SQL injection in string field400 or 200 (sanitized)
XSS payload in string field400 or 200 (sanitized)
Null values for required fields400 or 422

Example Test Files

→ See references/example-test-files.md for details

Generating Tests from Route Scan

When given a codebase, follow this process:

  1. Scan routes using the detection commands above
  2. Read each route handler to understand:
    • Expected request body schema
    • Auth requirements (middleware, decorators)
    • Return types and status codes
    • Business rules (ownership, role checks)
  3. Generate test file per route group using the patterns above
  4. Name tests descriptively: "returns 401 when token is expired" not "auth test 3"
  5. Use factories/fixtures for test data — never hardcode IDs
  6. Assert response shape, not just status code

Common Pitfalls

  • Testing only happy paths — 80% of bugs live in error paths; test those first
  • Hardcoded test data IDs — use factories/fixtures; IDs change between environments
  • Shared state between tests — always clean up in afterEach/afterAll
  • Testing implementation, not behavior — test what the API returns, not how it does it
  • Missing boundary tests — off-by-one errors are extremely common in pagination and limits
  • Not testing token expiry — expired tokens behave differently from invalid ones
  • Ignoring Content-Type — test that API rejects wrong content types (xml when json expected)

Best Practices

  1. One describe block per endpoint — keeps failures isolated and readable
  2. Seed minimal data — don't load the entire DB; create only what the test needs
  3. Use beforeAll for shared setup, afterAll for cleanup — not beforeEach for expensive ops
  4. Assert specific error messages/fields, not just status codes
  5. Test that sensitive fields (password, secret) are never in responses
  6. For auth tests, always test the "missing header" case separately from "invalid token"
  7. Add rate limit tests last — they can interfere with other test suites if run in parallel

来源与署名

来源:alirezarezvani/claude-skills位于engineering/skills/api-test-suite-builder提交19392f7

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架