testing.md 1.7 KB

测试指南

项目测试策略和运行方法


测试分层

E2E 测试 - 真实 LLM 调用(需要 API Key)
    ↑
集成测试 - 多模块协作(Mock LLM)
    ↑
单元测试 - 单个函数/类

运行测试

本地运行

# 单元测试 + 集成测试
pytest tests/ -v -m "not e2e"

# 指定模块
pytest tests/test_agent_definition.py -v

# 生成覆盖率报告
pytest --cov=agent --cov-report=html tests/ -m "not e2e"

# E2E 测试(可选,需要 API Key)
GEMINI_API_KEY=xxx pytest tests/e2e/ -v -m e2e

CI 运行

# 单元测试 + 集成测试(PR 时运行)
pytest tests/ -v -m "not e2e" --cov=agent

# E2E 测试(仅 main 分支)
pytest tests/e2e/ -v -m e2e

覆盖率要求

模块 目标覆盖率
agent/core/ 90%+
agent/execution/ 90%+
agent/memory/ 80%+
agent/tools/ 85%+
agent/llm/ 70%+

测试标记

# 单元测试(默认)
def test_something():
    pass

# E2E 测试(需要 API Key)
@pytest.mark.e2e
def test_real_llm():
    pass

CI 配置

# .github/workflows/test.yml

jobs:
  test:
    - name: Run tests
      run: pytest tests/ -v -m "not e2e" --cov=agent

  e2e:
    if: github.ref == 'refs/heads/main'
    - name: Run E2E tests
      env:
        GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
      run: pytest tests/e2e/ -v -m e2e

测试文件结构

tests/
├── conftest.py              # Fixtures
├── test_agent_definition.py
├── test_trace.py
├── test_tools.py
├── integration/
│   ├── test_subagent.py
│   └── test_trace_store.py
└── e2e/
    └── test_subagent_e2e.py