🧪 本地测试环境设置

❌ 问题:pytest 未找到

错误信息:

pytest : 无法将"pytest"项识别为 cmdlet、函数、脚本文件或可运行程序的名称

这说明 pytest 没有安装或不在 PATH 中。

✅ 解决方案:安装测试依赖

步骤1: 检查Python环境

# 检查Python是否安装
python --version

# 检查pip是否可用
pip --version

步骤2: 安装测试依赖

cd d:\Mises\mbe-monorepo

# 安装共享依赖
pip install -r shared/requirements.txt

# 安装核心模块依赖
pip install -r private/core/requirements.txt

# 安装平台模块依赖
pip install -r private/platform/requirements.txt

# 安装测试依赖(包括pytest)
pip install -r tests/requirements.txt

步骤3: 验证pytest安装

# 检查pytest是否安装
pytest --version

# 如果仍然找不到,尝试使用python -m pytest
python -m pytest --version

步骤4: 运行单元测试

# 方法1: 直接使用pytest(如果已安装)
pytest tests/unit/ -v -m unit

# 方法2: 使用python -m pytest(推荐)
python -m pytest tests/unit/ -v -m unit

🔍 如果pip安装失败

问题1: 权限问题

# 使用管理员权限运行PowerShell
# 或使用 --user 参数
pip install --user -r tests/requirements.txt

问题2: 网络问题

# 使用国内镜像源
pip install -r tests/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

问题3: 虚拟环境

如果项目使用虚拟环境:

# 创建虚拟环境
python -m venv venv

# 激活虚拟环境(Windows PowerShell)
.\venv\Scripts\Activate.ps1

# 安装依赖
pip install -r tests/requirements.txt

# 运行测试
pytest tests/unit/ -v -m unit

📋 快速安装脚本

创建一个PowerShell脚本来自动安装:

# install-test-deps.ps1
Write-Host "安装测试依赖..." -ForegroundColor Cyan

# 检查Python
if (-not (Get-Command python -ErrorAction SilentlyContinue)) {
    Write-Host "错误: Python未安装或不在PATH中" -ForegroundColor Red
    exit 1
}

# 安装依赖
Write-Host "安装共享依赖..." -ForegroundColor Yellow
pip install -r shared/requirements.txt

Write-Host "安装核心模块依赖..." -ForegroundColor Yellow
pip install -r private/core/requirements.txt

Write-Host "安装平台模块依赖..." -ForegroundColor Yellow
pip install -r private/platform/requirements.txt

Write-Host "安装测试依赖..." -ForegroundColor Yellow
pip install -r tests/requirements.txt

Write-Host "`n验证pytest安装..." -ForegroundColor Cyan
python -m pytest --version

Write-Host "`n✅ 安装完成!" -ForegroundColor Green
Write-Host "运行测试: python -m pytest tests/unit/ -v -m unit" -ForegroundColor Gray

🚀 运行测试

安装完成后,运行测试:

# 设置环境变量(如果需要)
$env:PYTHONPATH = "shared/src;private/core/src;private/platform/src"

# 运行单元测试
python -m pytest tests/unit/ -v -m unit

# 运行集成测试(需要数据库)
python -m pytest tests/integration/ -v -m integration

# 运行所有测试
python -m pytest tests/ -v

🔍 查看GitHub Actions中的错误

如果本地环境设置复杂,也可以直接查看GitHub Actions中的错误:

  1. 访问工作流运行页面

    https://github.com/zenglx1978/mbe-monorepo/actions
    
  2. 点击失败的运行

    • 找到 "Run Tests (unit)" 失败的运行
    • 点击查看详细日志
  3. 查看错误信息

    • 展开测试输出
    • 找到失败的测试用例
    • 查看错误堆栈

📚 相关文档


请先安装测试依赖,然后运行测试查看错误信息!