集成测试

📎 引用文件

本文引用的文件 - agent/tests/conftest.py - agent/tests/test_api_infrastructure.py - agent/tests/test_channels_api.py - agent/tests/test_upload_api.py - agent/tests/fixtures/fake_mcp_server.py - agent/api_server.py - agent/src/api/security.py - agent/src/api/models.py - agent/src/api/helpers.py - agent/src/api/state.py - agent/src/channels/config.py - agent/src/channels/pairing/store.py

目录

  1. 简介
  2. 项目结构
  3. 核心组件
  4. 架构总览
  5. 详细组件分析
  6. 依赖关系分析
  7. 性能考量
  8. 故障排查指南
  9. 结论
  10. 附录

简介

本文件面向 Vibe-Trading 的集成测试,聚焦端到端测试架构、外部服务模拟与数据库/存储测试策略。内容覆盖 API 集成测试、消息通道(类消息队列)集成测试、文件上传与存储测试、第三方服务(MCP)集成测试;并给出测试环境配置、测试数据准备与清理策略、异步与并发测试要点、分布式系统测试注意事项,以及常见问题与解决方案。所有示例均基于仓库中的实际测试用例与基础设施代码进行说明。

项目结构

Vibe-Trading 的集成测试主要位于 agent/tests 目录,围绕 FastAPI 应用 api_server 构建: - 共享夹具与环境隔离:conftest.py 负责进程级环境变量与配置缓存隔离,避免测试间相互污染。 - API 基础设施回归测试:test_api_infrastructure.py 验证安全、模型、助手函数、状态模块的导出一致性与边界行为。 - 通道(IM/消息)API 测试:test_channels_api.py 通过 TestClient 驱动 /channels/* 接口,校验通道启动/停止、配对命令与运行时配置。 - 文件上传与存储测试:test_upload_api.py 针对 /upload 流式上传、大小限制、跨站请求防护、权限控制与错误清理路径。 - 第三方服务模拟:fixtures/fake_mcp_server.py 提供最小 MCP 服务器,用于端到端调用链路的集成测试。

graph TB subgraph "测试层" T1["test_api_infrastructure.py"] T2["test_channels_api.py"] T3["test_upload_api.py"] T4["fake_mcp_server.py"] C["conftest.py"] end subgraph "应用层" A["api_server.py"] S["src/api/security.py"] M["src/api/models.py"] H["src/api/helpers.py"] ST["src/api/state.py"] CHC["src/channels/config.py"] CHS["src/channels/pairing/store.py"] end C --> A T1 --> A T2 --> A T3 --> A T4 --> A A --> S A --> M A --> H A --> ST A --> CHC A --> CHS

图表来源 - agent/tests/test_api_infrastructure.py:1-356 - agent/tests/test_channels_api.py:1-102 - agent/tests/test_upload_api.py:1-200 - agent/tests/fixtures/fake_mcp_server.py:1-48 - agent/tests/conftest.py:1-46

章节来源 - agent/tests/conftest.py:1-46 - agent/tests/test_api_infrastructure.py:1-356 - agent/tests/test_channels_api.py:1-102 - agent/tests/test_upload_api.py:1-200 - agent/tests/fixtures/fake_mcp_server.py:1-48

核心组件

章节来源 - agent/tests/conftest.py:16-46 - agent/tests/test_api_infrastructure.py:17-71 - agent/tests/test_channels_api.py:17-46 - agent/tests/test_upload_api.py:19-24 - agent/tests/fixtures/fake_mcp_server.py:14-48

架构总览

下图展示集成测试如何驱动 FastAPI 应用,并通过 TestClient 与内部模块交互,形成端到端验证闭环。

sequenceDiagram participant Py as "pytest" participant TC as "TestClient" participant AS as "api_server.py" participant SEC as "security.py" participant MOD as "models.py" participant HEL as "helpers.py" participant STA as "state.py" participant CH as "channels/*" Py->>TC : 发起HTTP请求(如 /upload, /channels/*) TC->>AS : 路由分发 AS->>SEC : 鉴权/跨域检查 AS->>HEL : 路径/参数校验、.env读写 AS->>MOD : 序列化/反序列化 AS->>STA : 获取会话服务(可被替换为假实现) AS->>CH : 通道启动/停止/状态查询 AS-->>TC : 返回响应(含状态码与JSON体) TC-->>Py : 断言响应

图表来源 - agent/tests/test_api_infrastructure.py:17-71 - agent/tests/test_channels_api.py:17-46 - agent/tests/test_upload_api.py:19-24 - agent/src/api/security.py - agent/src/api/models.py - agent/src/api/helpers.py - agent/src/api/state.py

详细组件分析

API 基础设施集成测试

flowchart TD Start(["进入测试"]) --> ReExport["断言再导出一致性"] ReExport --> CORS["测试CORS解析边界"] CORS --> PathVal["测试路径参数安全校验"] PathVal --> EnvRW[".env 读写与权限断言"] EnvRW --> SessionWB["会话服务写回兼容断言"] SessionWB --> End(["完成"])

图表来源 - agent/tests/test_api_infrastructure.py:17-71 - agent/tests/test_api_infrastructure.py:101-125 - agent/tests/test_api_infrastructure.py:157-183 - agent/tests/test_api_infrastructure.py:217-339 - agent/tests/test_api_infrastructure.py:347-356

章节来源 - agent/tests/test_api_infrastructure.py:17-71 - agent/tests/test_api_infrastructure.py:101-125 - agent/tests/test_api_infrastructure.py:157-183 - agent/tests/test_api_infrastructure.py:217-339 - agent/tests/test_api_infrastructure.py:347-356

消息通道(类消息队列)集成测试

sequenceDiagram participant T as "测试用例" participant C as "TestClient" participant A as "api_server.py" participant CFG as "channels/config.py" participant PST as "pairing/store.py" T->>C : GET /channels/status C->>A : 路由到通道状态 A->>CFG : 加载通道配置 A-->>C : {running : false, channels : {...}} T->>C : POST /channels/start C->>A : 启动通道运行时 A-->>C : {status : "started", running : true} T->>C : POST /channels/stop C->>A : 停止通道运行时 A-->>C : {status : "stopped", running : false} T->>C : POST /channels/pairing/command C->>A : 转发至通道配对命令 A->>PST : 读写配对存储 A-->>C : {channel : "telegram", reply : "..."}

图表来源 - agent/tests/test_channels_api.py:17-46 - agent/tests/test_channels_api.py:49-102 - agent/src/channels/config.py - agent/src/channels/pairing/store.py

章节来源 - agent/tests/test_channels_api.py:17-46 - agent/tests/test_channels_api.py:49-102

文件存储与上传集成测试

flowchart TD S(["开始上传"]) --> CheckOrigin{"是否跨站?"} CheckOrigin --> |是| Deny["拒绝并返回403"] CheckOrigin --> |否| Auth{"是否配置API_KEY?"} Auth --> |是| Verify["校验Bearer令牌"] Auth --> |否| AllowLocal["允许本地上传"] Verify --> Size{"是否超过限制?"} AllowLocal --> Size Size --> |是| Reject["返回413并清理临时文件"] Size --> |否| Save["保存文件并返回路径"] Save --> Done(["结束"]) Reject --> Done Deny --> Done

图表来源 - agent/tests/test_upload_api.py:19-24 - agent/tests/test_upload_api.py:31-140 - agent/tests/test_upload_api.py:143-200

章节来源 - agent/tests/test_upload_api.py:19-24 - agent/tests/test_upload_api.py:31-140 - agent/tests/test_upload_api.py:143-200

第三方服务集成测试(MCP)

sequenceDiagram participant T as "测试用例" participant P as "子进程(fake_mcp_server)" participant C as "MCP客户端" T->>P : 启动stdio传输 T->>C : 初始化连接 C->>P : 注册/发现工具(echo, add) C->>P : 调用echo("hello") P-->>C : 返回"echo : hello" C->>P : 调用add(1,2) P-->>C : 返回3

图表来源 - agent/tests/fixtures/fake_mcp_server.py:14-48

章节来源 - agent/tests/fixtures/fake_mcp_server.py:14-48

依赖关系分析

graph LR TAPI["test_api_infrastructure.py"] --> AS["api_server.py"] TCH["test_channels_api.py"] --> AS TUP["test_upload_api.py"] --> AS TMCP["fake_mcp_server.py"] --> AS AS --> SEC["security.py"] AS --> MOD["models.py"] AS --> HEL["helpers.py"] AS --> STA["state.py"] AS --> CHC["channels/config.py"] AS --> CHS["channels/pairing/store.py"]

图表来源 - agent/tests/test_api_infrastructure.py:17-71 - agent/tests/test_channels_api.py:17-46 - agent/tests/test_upload_api.py:19-24 - agent/tests/fixtures/fake_mcp_server.py:14-48

章节来源 - agent/tests/test_api_infrastructure.py:55-71 - agent/tests/test_channels_api.py:17-46 - agent/tests/test_upload_api.py:19-24

性能考量

[本节为通用指导,不直接分析具体文件]

故障排查指南

章节来源 - agent/tests/conftest.py:16-46 - agent/tests/test_upload_api.py:143-186 - agent/tests/test_channels_api.py:49-102

结论

本项目的集成测试围绕 FastAPI 应用构建了稳定的端到端验证体系:通过 TestClient 驱动 API、通过夹具与 monkeypatch 隔离外部依赖、通过最小化 MCP 服务器验证第三方集成。测试覆盖了安全、配置、I/O、通道生命周期与第三方协议等关键路径,具备良好的可维护性与可扩展性。建议持续保持: - 严格的环境与配置隔离 - 对关键路径(鉴权、大小限制、跨站、清理)的完备断言 - 对第三方服务的轻量模拟与契约测试

[本节为总结性内容,不直接分析具体文件]

附录

[本节为通用指导,不直接分析具体文件]