- docs/testing_strategy_v1.md: comprehensive testing strategy - Test pyramid definition (Unit/Integration/E2E) - File naming conventions - Mock interface guidelines - Coverage requirements - Test execution commands - Common issues solutions - reports/test_coverage_report_2026-04-08.md: current coverage status - Module-by-module breakdown - Coverage达标情况 - Mock implementations inventory - Next action items - CLAUDE.md: update test specifications - Add audit store mock signature - Detailed coverage targets per module - Test naming conventions
4.1 KiB
4.1 KiB
测试覆盖率报告
生成时间: 2026-04-08 分支: upload/2026-03-26-sync-clean
摘要
| 指标 | 数值 |
|---|---|
| 总测试文件数 | 40+ |
| 单元测试覆盖达标模块 | 7/8 |
| 关键模块平均覆盖率 | 78.4% |
模块覆盖率详情
✅ 达标模块
| 模块 | 目标 | 实际 | 差距 |
|---|---|---|---|
| domain | 70% | 71.2% | +1.2% |
| middleware | 80% | 80.4% | +0.4% |
| audit/handler | 75% | 79.6% | +4.6% |
| audit/service | 80% | 83.0% | +3.0% |
| audit/model | 80% | 93.8% | +13.8% |
| audit/sanitizer | 80% | 84.3% | +4.3% |
| security | 80% | 88.8% | +8.8% |
| iam | 70% | 93.2% | +23.2% |
| pkg/error | 80% | 93.1% | +13.1% |
⚠️ 需改进模块
| 模块 | 目标 | 实际 | 待提升 |
|---|---|---|---|
| audit/repository | 50% | 0.0% | +50% |
| repository | 50% | 1.3% | +48.7% |
| httpapi | 50% | 6.0% | +44.0% |
| iam/handler | 50% | 23.2% | +26.8% |
| iam/service | 50% | 23.6% | +26.4% |
| iam/middleware | 50% | 24.6% | +25.4% |
| pkg/logging | 50% | 50.0% | 0% |
| middleware | 80% | 52.7% | -27.3% |
Domain 模块详细覆盖
覆盖率分布
| 文件 | 覆盖率 | 状态 |
|---|---|---|
| account.go | 高 | ✅ |
| package.go | 高 | ✅ |
| settlement.go | 中 | ⚠️ |
| outbox.go | 高 | ✅ |
| compensation.go | 高 | ✅ |
| invariants.go | 高 | ✅ |
未完全覆盖方法
settlement.go:
- Withdraw: 0%
- Cancel: 0%
- GetByID: 0%
- List: 0%
- GetBillingSummary: 0%
- generateSettlementNo: 0%
package.go:
- Clone: 0% (已有测试)
- BatchUpdatePrice: 0% (已有测试)
测试文件清单
Domain 模块
| 文件 | 行数 | 说明 |
|---|---|---|
| account_test.go | ~580 | AccountService 完整测试 |
| package_test.go | ~580 | PackageService 完整测试 |
| settlement_test.go | ~500 | SettlementService 完整测试 |
| invariants_test.go | ~500 | 业务不变量测试 |
| outbox_test.go | ~400 | Outbox 模式测试 |
| compensation_test.go | ~200 | 补偿机制测试 |
Audit 模块
| 文件 | 行数 | 说明 |
|---|---|---|
| audit_service_test.go | ~300 | 审计服务测试 |
| audit_service_db_test.go | ~200 | 数据库审计测试 |
| alert_service_test.go | ~200 | 告警服务测试 |
| batch_buffer_test.go | ~150 | 批处理测试 |
| audit_handler_test.go | ~400 | 处理器测试 |
Middleware 模块
| 文件 | 行数 | 说明 |
|---|---|---|
| auth_test.go | ~300 | 认证测试 |
| ratelimit_test.go | ~200 | 限流测试 |
| idempotency_test.go | ~200 | 幂等测试 |
| tracing_test.go | ~150 | 追踪测试 |
| db_token_backend_test.go | ~200 | Token 后端测试 |
Mock 实现清单
Domain Mocks
// account_test.go
mockAccountStore struct { ... }
mockAuditStore struct { ... }
// package_test.go
mockPackageStoreForPackageTest struct { ... }
mockAccountStoreForPackageTest struct { ... }
mockAuditStoreForPackageTest struct { ... }
// invariants_test.go
mockAccountStoreForInvariant struct { ... }
mockPackageStoreForInvariant struct { ... }
mockSettlementStoreForInvariant struct { ... }
// settlement_test.go
mockSettlementStore struct { ... }
mockEarningStore struct { ... }
mockAuditStoreForSettlement struct { ... }
测试运行指南
本地快速验证
# 验证关键模块
go test -cover ./internal/domain/... \
./internal/middleware/... \
./internal/audit/handler/... \
./internal/audit/service/...
生成覆盖率报告
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o coverage.html
CI 检查
# 检查所有测试通过
go test ./...
# 检查覆盖率达标
go test -cover ./... | grep -v "coverage: 0.0%"
下一步行动
短期(1周)
- 提升 repository 模块覆盖率(0% → 30%)
- 提升 httpapi 模块覆盖率(6% → 30%)
- 补充 middleware 缺失测试
中期(2周)
- 完善 settlement.go 所有方法测试
- 补充 IAM 模块 handler/service 测试
- 添加集成测试骨架
长期(1月)
- 建立 E2E 测试
- 性能测试基线
- 混沌测试