跳转至

测试指南

测试概览

ZeroClaw 包含 1017+ 个测试,确保代码质量和稳定性。

运行测试

基本测试

# 运行所有测试
cargo test

# 运行特定测试
cargo test test_provider
cargo test tool_shell

# 显示输出
cargo test -- --nocapture

测试分类

类别 测试数量 命令
单元测试 800+ cargo test --lib
集成测试 150+ cargo test --test *
文档测试 50+ cargo test --doc
基准测试 10+ cargo bench

测试覆盖

核心模块测试

  • src/providers/ - AI 提供商测试
  • src/channels/ - 通讯渠道测试
  • src/tools/ - 工具执行测试
  • src/memory/ - 内存系统测试
  • src/security/ - 安全策略测试
  • src/gateway/ - 网关测试

特殊测试

内存对比测试

# 运行 SQLite vs Markdown 对比
cargo test memory_comparison -- --nocapture

Telegram 集成测试

# 需要 Telegram Bot Token
export TELEGRAM_BOT_TOKEN="your-token"
cargo test telegram_integration -- --nocapture

编写测试

单元测试示例

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_provider_creation() {
        let provider = create_provider("openai");
        assert!(provider.is_ok());
    }

    #[test]
    fn test_tool_execution() {
        let tool = ShellTool::new();
        let result = tool.execute(json!({"cmd": "echo hello"}));
        assert!(result.is_ok());
    }
}

集成测试示例

#[tokio::test]
async fn test_chat_flow() {
    let agent = create_test_agent().await;
    let response = agent.chat("hello").await;
    assert!(response.is_ok());
}

CI/CD

ZeroClaw 使用 GitHub Actions 进行持续集成:

  • ✅ 每个 PR 自动运行测试
  • ✅ 多平台测试(Linux, macOS, Windows)
  • ✅ 多架构测试(x86_64, ARM64)
  • ✅ 安全扫描和 Fuzz testing

测试最佳实践

  1. 保持测试独立 - 每个测试应该独立运行
  2. 使用适当的断言 - 明确的期望值
  3. 测试边界条件 - 空输入、错误处理
  4. 模拟外部依赖 - Provider, Channel 等可模拟
  5. 保持测试快速 - 避免不必要的 I/O

性能测试

# 运行基准测试
cargo bench

# 比较性能变化
cargo bench -- --save-baseline main
cargo bench -- --baseline main

调试测试

# 显示详细输出
cargo test -- --nocapture

# 只运行失败的测试
cargo test -- --failed

# 按名称过滤
cargo test test_provider -- test_provider_openai