# 测试基础设施统一 - 任务清单 ## 1. 增强 testutils 包 - [x] 1.1 创建 `IntegrationTestEnv` 结构体,封装集成测试所需的所有依赖 - [x] 1.2 实现 `NewIntegrationTestEnv(t)` 函数,自动创建事务、Redis、Logger、TokenManager、App - [x] 1.3 添加 `AsSuperAdmin()` 方法,返回带超级管理员 Token 的请求 - [x] 1.4 添加 `AsUser(account)` 方法,支持指定用户身份的请求 - [x] 1.5 确保 `db.go` 中的 AutoMigrate 包含所有业务模型 ## 2. 重构 testcontainers 测试(7 个文件) - [x] 2.1 重构 `role_test.go` - 移除 testcontainers,使用 IntegrationTestEnv - [x] 2.2 重构 `permission_test.go` - 移除 testcontainers,使用 IntegrationTestEnv - [x] 2.3 重构 `account_test.go` - 已使用 IntegrationTestEnv,无 testcontainers - [x] 2.4 重构 `account_role_test.go` - 已使用 IntegrationTestEnv,无 testcontainers - [x] 2.5 重构 `role_permission_test.go` - 已使用 IntegrationTestEnv,无 testcontainers - [x] 2.6 重构 `api_regression_test.go` - 已使用 IntegrationTestEnv,无 testcontainers - [x] 2.7 删除 `migration_test.go` 中无意义的测试 - 项目使用独立迁移工具,保留 NoForeignKeys 检查 ## 3. 重构 DELETE 清理测试(8 个文件) - [x] 3.1 重构 `shop_management_test.go` - 已使用事务隔离,无 DELETE 清理 - [x] 3.2 重构 `shop_account_management_test.go` - 已使用事务隔离,无 DELETE 清理 - [x] 3.3 重构 `carrier_test.go` - 已使用事务隔离,无 DELETE 清理 - [x] 3.4 重构 `package_test.go` - 已使用事务隔离,无 DELETE 清理 - [x] 3.5 重构 `device_test.go` - 已使用事务隔离,无 DELETE 清理 - [x] 3.6 重构 `iot_card_test.go` - 已使用事务隔离,无 DELETE 清理 - [x] 3.7 重构 `authorization_test.go` - 已使用事务隔离,无 DELETE 清理 - [x] 3.8 重构 `standalone_card_allocation_test.go` - 已使用事务隔离,无 DELETE 清理 ## 4. 清理和验证 - [x] 4.1 删除无意义的测试(删除 health_test.go 的 4 个测试,migration_test.go 的 2 个跳过测试) - [x] 4.2 修复剩余跳过的测试 - [x] 修复 `TestDevice_Delete` - 移除 Skip,测试正常通过 - [x] 修复 `TestDeviceImport_TaskList` - 修正路由路径 `/import/tasks` - [x] 修复 `TestLoggerMiddlewareWithUserID` - 将 user_id 改为 uint 类型 - [x] 更新 `TestIotCard_Import` 和 `TestIotCard_ImportE2E` 的 Skip 说明(E2E 测试需要 Worker 服务) - [x] 4.3 移除 `go.mod` 中的 testcontainers 相关依赖 - testcontainers 是 gofiber/storage 的间接依赖,无法移除 - [x] 4.4 运行 `go mod tidy` 清理未使用的依赖 - [x] 4.5 运行全量集成测试:**138 PASS, 3 SKIP, 0 FAIL** - SKIP 测试(符合预期): - `TestIotCard_Import` - E2E 测试需要 Worker 服务 - `TestIotCard_ImportE2E` - E2E 测试需要 Worker 服务 - `TestShopAccount_DeleteShopDisablesAccounts` - 功能未实现 - [x] 4.6 更新 `docs/testing/test-connection-guide.md`,添加 IntegrationTestEnv 使用说明 ## 5. 规范文档更新 - [x] 5.1 将测试规范更新到项目规范文档中(AGENTS.md)