Files
tokens-reef/Makefile.test

168 lines
5.1 KiB
Makefile
Raw Normal View History

# Sub2API 测试 Makefile
# 提供便捷的测试命令
.PHONY: help test test-unit test-contract test-security test-e2e test-performance test-all
.PHONY: test-env-up test-env-down test-env-logs test-env-seed
.PHONY: coverage coverage-html lint security-scan
# 默认目标
help:
@echo "Sub2API 测试命令"
@echo ""
@echo "环境管理:"
@echo " make test-env-up 启动测试环境 (Docker Compose)"
@echo " make test-env-down 停止测试环境"
@echo " make test-env-logs 查看测试环境日志"
@echo " make test-env-seed 填充测试数据"
@echo ""
@echo "测试执行:"
@echo " make test-unit 运行单元测试"
@echo " make test-contract 运行契约测试"
@echo " make test-security 运行安全测试"
@echo " make test-e2e 运行E2E测试"
@echo " make test-performance 运行性能测试"
@echo " make test-all 运行所有测试"
@echo ""
@echo "代码质量:"
@echo " make coverage 生成覆盖率报告"
@echo " make coverage-html 生成HTML覆盖率报告"
@echo " make lint 运行代码检查"
@echo " make security-scan 运行安全扫描"
# ============================================
# 环境管理
# ============================================
test-env-up:
@echo "🚀 启动测试环境..."
docker-compose -f docker-compose.test.yml up -d
@echo "⏳ 等待服务就绪..."
@sleep 10
@echo "✅ 测试环境已启动"
@echo ""
@echo "服务地址:"
@echo " - 后端API: http://localhost:8080"
@echo " - 前端: http://localhost:3000"
@echo " - PostgreSQL: localhost:5432"
@echo " - Redis: localhost:6379"
@echo " - Grafana: http://localhost:3001"
@echo " - MailHog: http://localhost:8025"
test-env-down:
@echo "🛑 停止测试环境..."
docker-compose -f docker-compose.test.yml down -v
@echo "✅ 测试环境已停止"
test-env-logs:
docker-compose -f docker-compose.test.yml logs -f
test-env-seed:
@echo "🌱 填充测试数据..."
docker-compose -f docker-compose.test.yml exec -T backend go run cmd/seed/main.go
@echo "✅ 测试数据已填充"
# ============================================
# 测试执行
# ============================================
test-unit:
@echo "🧪 运行单元测试..."
cd backend && go test -tags=unit -v -race -coverprofile=coverage.out ./...
@echo "✅ 单元测试完成"
test-contract:
@echo "📋 运行契约测试..."
cd backend && go test -tags=integration -v ./internal/server/... -run TestAPIContracts -timeout 10m
@echo "✅ 契约测试完成"
test-security:
@echo "🔒 运行安全测试..."
cd backend && go test -tags=security -v ./internal/server/... -run TestAPI_Security -timeout 10m
@echo "✅ 安全测试完成"
test-e2e:
@echo "🎭 运行E2E测试..."
npx playwright test tests/e2e/ --reporter=html
@echo "✅ E2E测试完成"
test-e2e-ui:
@echo "🎭 运行E2E测试 (UI模式)..."
npx playwright test tests/e2e/ --ui
test-performance:
@echo "⚡ 运行性能测试..."
@echo "确保测试环境已启动: make test-env-up"
@echo ""
@echo "运行负载测试..."
k6 run \
--out influxdb=http://localhost:8086/k6 \
--env BASE_URL=http://localhost:8080 \
--env API_KEY=sk-test-key \
tests/performance/gateway-load-test.js
@echo "✅ 性能测试完成"
test-performance-stress:
@echo "💥 运行压力测试..."
k6 run \
--out influxdb=http://localhost:8086/k6 \
--env BASE_URL=http://localhost:8080 \
--env API_KEY=sk-test-key \
tests/performance/gateway-stress-test.js
test-performance-soak:
@echo "⏱️ 运行稳定性测试 (2小时)..."
k6 run \
--out influxdb=http://localhost:8086/k6 \
--env BASE_URL=http://localhost:8080 \
--env API_KEY=sk-test-key \
tests/performance/gateway-soak-test.js
test-all: test-unit test-contract test-security
@echo "🎉 所有测试通过!"
# ============================================
# 代码质量
# ============================================
coverage:
@echo "📊 生成覆盖率报告..."
cd backend && go test -tags=unit -coverprofile=coverage.out ./...
cd backend && go tool cover -func=coverage.out
coverage-html:
@echo "📊 生成HTML覆盖率报告..."
cd backend && go test -tags=unit -coverprofile=coverage.out ./...
cd backend && go tool cover -html=coverage.out -o coverage.html
@echo "✅ 报告已生成: backend/coverage.html"
lint:
@echo "🔍 运行代码检查..."
cd backend && golangci-lint run ./...
cd frontend && npm run lint
@echo "✅ 代码检查完成"
security-scan:
@echo "🔐 运行安全扫描..."
@echo "1. 运行 gosec..."
cd backend && gosec ./...
@echo "2. 运行 nancy (依赖漏洞检查)..."
cd backend && nancy sleuth go.sum
@echo "3. 运行 trivy (容器扫描)..."
trivy image --severity HIGH,CRITICAL sub2api-backend:latest || true
@echo "✅ 安全扫描完成"
# ============================================
# 便捷命令
# ============================================
test-quick:
@echo "⚡ 快速测试 (单元+契约)..."
make test-unit
make test-contract
test-ci:
@echo "🔄 CI模式测试..."
make test-unit
make test-contract
make test-security