测试策略

📎 引用文件

本文引用的文件 - pyproject.toml - conftest.py - test_base_engine.py - test_backtest_runner_security.py - test_akshare_loader.py - test_api_infrastructure.py - test_content_filter_e2e.py - test_engine_robustness.py

目录

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

简介

本测试策略面向 Vibe-Trading 的量化回测与交易智能体,覆盖单元测试、集成测试、端到端测试、数据源测试、性能与安全测试。文档基于仓库中的实际测试代码与配置,说明测试框架配置、用例组织、Mock 策略、覆盖率要求、与其他组件的集成方式,以及常见问题的处理方案。重点强调: - 测试架构设计:以 pytest 为核心,通过 conftest 统一环境隔离与路径设置。 - 测试数据管理:使用 fixtures、临时目录与最小化构造数据,避免网络与外部依赖。 - 自动化流程:通过 pyproject 中 pytest 与 coverage 的配置,支持快速本地运行与 CI 集成。 - 安全与健壮性:对动态加载的策略代码进行严格的安全扫描与白名单控制;对引擎异常进行隔离与容错。

项目结构

测试位于 agent/tests 下,按功能域划分(如 factors、memory、quantlib、openbb_bridge),并包含大量针对数据源、引擎、API、CLI 等的回归测试。根级 pyproject.toml 集中定义测试入口、标记、覆盖率与开发依赖。

graph TB A["pyproject.toml<br/>pytest/coverage/开发依赖"] --> B["agent/tests/conftest.py<br/>全局夹具与环境隔离"] B --> C["单元/集成/端到端测试<br/>test_*.py"] C --> D["回测引擎测试<br/>test_base_engine.py / test_engine_robustness.py"] C --> E["数据源路由测试<br/>test_akshare_loader.py"] C --> F["API 基础设施测试<br/>test_api_infrastructure.py"] C --> G["内容过滤 E2E<br/>test_content_filter_e2e.py"] C --> H["策略安全扫描<br/>test_backtest_runner_security.py"]

图表来源 - pyproject.toml:232-256 - conftest.py:1-46

章节来源 - pyproject.toml:224-256 - conftest.py:1-46

核心组件

章节来源 - pyproject.toml:232-256 - conftest.py:16-46

架构总览

下图展示测试在系统中的位置与交互:测试通过 pytest 启动,利用 conftest 提供的环境隔离,驱动回测引擎、数据加载器、API 层与内容过滤管线,并以断言验证行为与输出。

sequenceDiagram participant Py as "pytest" participant CF as "conftest 夹具" participant BE as "回测引擎(ChinaAEngine/BaseEngine)" participant DL as "数据加载器(AKShare等)" participant API as "API 基础设施" participant CFil as "内容过滤(E2E)" participant Sec as "策略安全扫描" Py->>CF : 初始化/环境快照 Py->>BE : 执行对齐/调仓/PnL 测试 BE-->>Py : 断言仓位、资金、交易记录 Py->>DL : 路由到 ETF/外汇/A股 DL-->>Py : 断言列名、填充、映射 Py->>API : 验证安全/模型/路径参数 API-->>Py : 断言重导出/异常 Py->>CFil : 模拟LLM流式响应 CFil-->>Py : 断言状态/警告/追踪 Py->>Sec : 加载策略代码并扫描 Sec-->>Py : 断言拒绝/允许规则

图表来源 - test_base_engine.py:1-120 - test_akshare_loader.py:119-177 - test_api_infrastructure.py:17-71 - test_content_filter_e2e.py:156-193 - test_backtest_runner_security.py:17-85

详细组件分析

回测引擎测试(对齐、调仓、PnL、生命周期)

flowchart TD Start(["进入 _align"]) --> Normalize["标准化信号与数据索引"] Normalize --> Shift["信号延迟一期生成下一根K线仓位"] Shift --> FillNaN["缺失收盘价 ffill/bfill"] FillNaN --> Clip["裁剪信号至[-1,1]"] Clip --> Optimize{"是否应用优化器?"} Optimize --> |是| ApplyOpt["应用优化器变换权重"] Optimize --> |否| SkipOpt["跳过优化"] ApplyOpt --> Output["输出 close_df/pos_df/ret_df"] SkipOpt --> Output

图表来源 - test_base_engine.py:442-520

章节来源 - test_base_engine.py:119-368 - test_base_engine.py:370-413 - test_base_engine.py:547-617

数据源测试(AKShare 路由与映射)

sequenceDiagram participant T as "测试用例" participant MP as "monkeypatch" participant AK as "akshare(模拟)" participant DL as "DataLoader" T->>MP : 注入 fake akshare T->>DL : _fetch_one(symbol, start, end, period) DL->>AK : 根据符号选择接口 AK-->>DL : 返回DataFrame DL-->>T : 断言列名/填充/调用次数

图表来源 - test_akshare_loader.py:119-177

章节来源 - test_akshare_loader.py:33-76 - test_akshare_loader.py:119-177

API 基础设施测试(安全、模型、路径参数)

章节来源 - test_api_infrastructure.py:17-71 - test_api_infrastructure.py:101-183

端到端内容过滤测试(LLM 流式调用与告警)

sequenceDiagram participant Test as "测试用例" participant LLM as "LLM Stub" participant Loop as "AgentLoop" participant Trace as "TraceWriter" Test->>Loop : run(user_message) Loop->>LLM : stream_chat(...) LLM-->>Loop : content_filter_triggered=True/False Loop->>Trace : 写入 content_filter_skipped Loop-->>Test : 返回结果(含 warnings) Test->>Trace : 读取 trace.jsonl 并断言

图表来源 - test_content_filter_e2e.py:29-74 - test_content_filter_e2e.py:120-148 - test_content_filter_e2e.py:156-193

章节来源 - test_content_filter_e2e.py:156-193

策略安全扫描测试(AST 扫描与白名单)

flowchart TD Load["加载策略模块"] --> Parse["AST 解析"] Parse --> CheckTop["检查顶层语句"] CheckTop --> |通过| Walk["遍历方法体与调用链"] CheckTop --> |拒绝| RejectTop["拒绝: 可执行顶层语句"] Walk --> Detect["检测危险调用/导入/属性访问"] Detect --> |发现| RejectBody["拒绝: 方法体内禁止操作"] Detect --> |未发现| Allow["允许: 合法策略"]

图表来源 - test_backtest_runner_security.py:17-85 - test_backtest_runner_security.py:97-169 - test_backtest_runner_security.py:302-364 - test_backtest_runner_security.py:428-456

章节来源 - test_backtest_runner_security.py:17-85 - test_backtest_runner_security.py:97-169 - test_backtest_runner_security.py:302-364 - test_backtest_runner_security.py:428-456

引擎健壮性测试(ffill 限制、全 NaN 符号、异常隔离)

章节来源 - test_engine_robustness.py:34-89 - test_engine_robustness.py:96-137

依赖关系分析

graph LR TBE["test_base_engine.py"] --> BE["backtest.engines.*"] TAL["test_akshare_loader.py"] --> AL["backtest.loaders.akshare_loader"] TAIP["test_api_infrastructure.py"] --> SA["src.api.security/models/helpers/state"] TCF["test_content_filter_e2e.py"] --> AG["src.agent.loop/memory/tools/providers"] TSEC["test_backtest_runner_security.py"] --> RUN["backtest.runner"]

图表来源 - test_base_engine.py:1-17 - test_akshare_loader.py:18-25 - test_api_infrastructure.py:8-10 - test_content_filter_e2e.py:21-22 - test_backtest_runner_security.py:9-10

章节来源 - test_base_engine.py:1-17 - test_akshare_loader.py:18-25 - test_api_infrastructure.py:8-10 - test_content_filter_e2e.py:21-22 - test_backtest_runner_security.py:9-10

性能考量

[本节为通用指导,无需具体文件分析]

故障排查指南

章节来源 - conftest.py:16-46 - test_akshare_loader.py:119-177 - test_content_filter_e2e.py:156-193 - test_backtest_runner_security.py:302-364

结论

Vibe-Trading 的测试体系以 pytest 为核心,结合严格的 fixture 隔离与广泛的 Mock 策略,覆盖了从底层引擎到上层 API 与内容过滤的关键路径。通过系统化的安全扫描与健壮性测试,确保了策略代码的动态加载安全与回测过程的容错能力。建议在后续迭代中持续完善: - 增加更多端到端场景(多市场、多数据源组合)。 - 引入性能基准测试与回归监控。 - 扩展安全白名单与黑名单,定期审计新风险面。 - 提高覆盖率门槛并在 CI 中强制执行。

[本节为总结性内容,无需具体文件分析]

附录

章节来源 - pyproject.toml:232-256