服务类深度测试指南

📋 概述

本文档介绍如何运行和管理服务类深度测试。这些测试覆盖核心业务逻辑,预计包含 270+ 个测试用例,覆盖 5-10% 的代码覆盖率提升

🎯 测试文件

已创建的深度测试

  1. test_user_service_deep.py (~80个测试用例)

    • 用户创建、查询、更新
    • 设备绑定、认证
    • 订阅管理
    • 使用统计、配额检查
    • API密钥管理
    • OAuth集成
  2. test_payment_service_deep.py (~50个测试用例)

    • 微信支付
    • 支付宝
    • 统一支付接口
    • 订单查询和回调
  3. test_sales_advisor_deep.py (~50个测试用例)

    • 客户分析
    • 异议解码
    • 下一步建议
    • 丢单复盘
    • 话术生成
  4. test_smart_router_deep.py (~50个测试用例)

    • 路由决策
    • 模型选择
    • 成本/延迟/质量优化
    • 缓存机制
  5. test_model_sync_registry_deep.py (~40个测试用例)

    • 服务注册
    • 服务获取
    • 同步管理

🚀 快速开始

Windows (PowerShell)

# 运行所有深度测试
.\scripts\run_deep_tests.ps1

# 运行特定测试文件
.\scripts\run_deep_tests.ps1 -TestFile "tests/unit/test_user_service_deep.py"

# 带覆盖率报告
.\scripts\run_deep_tests.ps1 -Coverage

# 跳过数据库测试
.\scripts\run_deep_tests.ps1 -SkipDb

Linux/Mac (Bash)

# 添加执行权限
chmod +x scripts/run_deep_tests.sh

# 运行所有深度测试
./scripts/run_deep_tests.sh

# 运行特定测试文件
./scripts/run_deep_tests.sh --file "tests/unit/test_user_service_deep.py"

# 带覆盖率报告
./scripts/run_deep_tests.sh --coverage

# 跳过数据库测试
./scripts/run_deep_tests.sh --skip-db

直接使用 pytest

# 设置环境变量
export DATABASE_URL="postgresql+asyncpg://mbe:test_password@localhost:5432/mbe_test"
export REDIS_URL="redis://localhost:6379/15"
export PYTHONPATH="shared/src:private/core/src:private/platform/src"

# 运行所有深度测试
pytest tests/unit/test_*_deep.py -v -m unit

# 运行特定测试
pytest tests/unit/test_user_service_deep.py -v

# 带覆盖率
pytest tests/unit/test_*_deep.py -v --cov=shared/src --cov=private/core/src --cov=private/platform/src --cov-report=html

🗄️ 数据库配置

使用 Docker (推荐)

# 启动测试数据库
docker-compose -f docker-compose.dev.yml up -d postgres redis

# 验证数据库运行
docker ps --filter "name=mbe-dev-postgres"

环境变量

测试脚本会自动检测Docker容器并设置环境变量。也可以手动设置:

Windows (PowerShell):

$env:DATABASE_URL = "postgresql+asyncpg://mbe:dev_password@localhost:5432/mbe_dev"
$env:REDIS_URL = "redis://localhost:6379/15"

Linux/Mac (Bash):

export DATABASE_URL="postgresql+asyncpg://mbe:dev_password@localhost:5432/mbe_dev"
export REDIS_URL="redis://localhost:6379/15"

📊 测试结果

查看覆盖率报告

运行带 --coverage 参数的测试后,会生成HTML覆盖率报告:

# 打开覆盖率报告
open htmlcov/index.html  # Mac
start htmlcov/index.html  # Windows
xdg-open htmlcov/index.html  # Linux

CI/CD 集成

深度测试已集成到 GitHub Actions CI 流程中:

  • 文件: .github/workflows/ci.yml
  • Job: test-monorepo
  • 步骤: Run deep service tests

🔧 故障排除

数据库连接错误

错误: password authentication failed for user "postgres"

解决方案:

  1. 检查Docker容器是否运行:

    docker ps --filter "name=mbe-dev-postgres"
    
  2. 检查环境变量:

    echo $DATABASE_URL  # Linux/Mac
    echo $env:DATABASE_URL  # Windows PowerShell
    
  3. 重启数据库容器:

    docker-compose -f docker-compose.dev.yml restart postgres
    

导入错误

错误: ModuleNotFoundError: No module named 'users'

解决方案: 确保设置了正确的 PYTHONPATH:

export PYTHONPATH="shared/src:private/core/src:private/platform/src"

Redis 连接错误

错误: Connection refused

解决方案:

  1. 检查Redis容器:

    docker ps --filter "name=mbe-dev-redis"
    
  2. 启动Redis:

    docker-compose -f docker-compose.dev.yml up -d redis
    

📈 测试统计

当前状态

  • 总测试用例: ~270个
  • 覆盖服务类: 5个核心服务
  • 代码量: ~5,000-8,000行测试代码
  • 预计覆盖率提升: 5-10%

测试分类

  • 单元测试: 测试独立功能
  • 集成测试: 测试服务间交互
  • 边界测试: 测试边界情况
  • 错误处理: 测试错误场景
  • 并发测试: 测试并发安全性
  • 性能测试: 测试性能基准

🔄 持续改进

添加新测试

  1. 创建新的测试文件:tests/unit/test_<service>_deep.py
  2. 遵循现有测试模式
  3. 添加适当的fixtures和mocks
  4. 更新CI配置

扩展现有测试

  1. 识别未覆盖的场景
  2. 添加边界情况测试
  3. 添加错误处理测试
  4. 添加并发测试

📝 最佳实践

  1. 使用fixtures: 复用测试数据和设置
  2. Mock外部依赖: 避免真实API调用
  3. 清理测试数据: 使用 clean_db fixture
  4. 独立测试: 每个测试应该独立运行
  5. 描述性命名: 测试名称应该清晰描述测试内容

🎓 示例

运行单个测试

pytest tests/unit/test_user_service_deep.py::TestUserServiceDeep::test_create_user_all_fields -v

运行特定类别的测试

# 只运行用户创建相关测试
pytest tests/unit/test_user_service_deep.py -k "create_user" -v

# 只运行订阅相关测试
pytest tests/unit/test_user_service_deep.py -k "subscription" -v

并行运行测试

# 安装pytest-xdist
pip install pytest-xdist

# 并行运行(4个worker)
pytest tests/unit/test_*_deep.py -n 4

📚 相关文档

🤝 贡献

添加新测试时,请确保:

  1. 测试通过
  2. 代码格式正确
  3. 添加适当的文档
  4. 更新本指南

最后更新: 2026-02-08