跳转至

测试框架

概述

项目测试代码位于 test/ 目录,使用标准 unittest。当前回归配置见 test/integration_test_config.json,包含 38 个测试组(含 fast 快速子集)。

v3.9.6 发布前于 2026-10-31 使用 Anaconda python39 在隔离副本执行 --all-with-fast:38 组、172 次模块执行、3867 次用例执行,3867 通过、0 失败、0 错误、0 跳过,成功率 100%,耗时 1177.21 秒。次数包含 fast 与专项组的重复覆盖。

测试统计速查

版本 日期 回归规模(含 fast) 通过用例 通过率
v3.9.6 2026-10-31 38 组, 172 次模块执行, 3867 次用例执行 3867 100%
v3.9.5 2026-10-15 37 组, 160 模块, 3496 用例 3496 100%
v3.9.4 2026-09-30 37 组, 159 模块, 3450 用例 3450 100%
v3.9.3 2026-09-19 37 组, 154 模块, 3248 用例 3248 100%
v3.9.2 2026-09-14 36 组, 148 模块, 3166 用例 3166 100%
v3.9.1 2026-09-12 36 组, 146 模块, 3126 用例 3126 100%
v3.9.0 2026-08-29 35 组, 130 模块, 2622 用例 2622 100%
v3.8.9 2026-08-19 33 组, 123 模块, 2548 用例 2548 100%
v3.8.8 2026-08-13 33 组, 123 模块, 2479 用例 2479 100%
v3.8.7 2026-08-10 33 组, 123 模块, 2453 用例 2453 100%
v3.8.6 2026-08-07 33 组, 123 模块, 2361 用例 2361 100%
v3.8.5 2026-08-01 31 组, 108 模块, 2014 用例 2014 100%
v3.8.4 2026-07-28 31 组, 108 模块, 2004 用例 2004 100%
v3.8.1 2026-07-17 31 组, 107 模块, 1933 用例 1933 100%
v3.8.0 2026-07-13 31 组, 1912 用例 1912 100%
v3.6.0 2026-07-10 28 组, 70 模块, 1039 用例 - 100%

测试基础设施

文件 说明
test/test_base.py TestBase 基类:测试 DB 创建、持仓 fixture、线程断言、条件等待
test/test_mocks.py MockQmtTrader:完整模拟 QMT API(连接、持仓、下单),无需真实 QMT
test/test_utils.py 通用测试辅助函数

运行测试

回归测试框架(推荐)

# 快速验证(关键模块子集)
python test/run_integration_regression_tests.py --fast

# 运行全部回归测试
python test/run_integration_regression_tests.py --all

# 运行全部回归测试,并包含 fast 组
python test/run_integration_regression_tests.py --all-with-fast

# 按组运行
python test/run_integration_regression_tests.py --group autobuy
python test/run_integration_regression_tests.py --group system_integration
python test/run_integration_regression_tests.py --group stop_profit
python test/run_integration_regression_tests.py --group grid_signal
python test/run_integration_regression_tests.py --group grid_session
python test/run_integration_regression_tests.py --group grid_trade
python test/run_integration_regression_tests.py --group grid_exit
python test/run_integration_regression_tests.py --group grid_validation
python test/run_integration_regression_tests.py --group grid_comprehensive
python test/run_integration_regression_tests.py --group grid_bug_regression
python test/run_integration_regression_tests.py --group grid_true_pnl
python test/run_integration_regression_tests.py --group qmt_ipc_fallback
python test/run_integration_regression_tests.py --group qmt_rpc
python test/run_integration_regression_tests.py --group simulation_trading_e2e
python test/run_integration_regression_tests.py --group multi_account_isolation
python test/run_integration_regression_tests.py --group launcher_deployment

数据与备份隔离

完整回归会生成、清理和恢复测试数据库,应在不含生产配置和运行数据的独立源码副本中运行,并设置 ENABLE_SIMULATION_MODE=true、MINIQMT_DISABLE_DOTENV=1。--skip-env-prep 只跳过运行器环境准备,不能保证各测试类不再操作数据库,也不等于生产数据隔离。

运行器默认对实际存在的默认库和账号库创建 SQLite 一致性备份,每个源库保留最新 10 份;备份失败会中止后续清理及测试。Windows 下重复导入运行器保留标准输出流的归属,完整组汇总不会因重复包装句柄而关闭输出流。运行产物的预览清理见无人值守运行。

其他选项

# 失败重试
python test/run_integration_regression_tests.py --all --retry-failed

# 详细输出
python test/run_integration_regression_tests.py --all --verbose

# 跳过环境准备(不备份生产 DB)
python test/run_integration_regression_tests.py --all --skip-env-prep

单个测试文件

python test/run_single_test.py test.test_unattended_operation
python -m unittest test.test_system_integration -v
python test/run_all_grid_tests.py

QMT order_id 回归

order_stock_async() 的 seq -> order_id 行为已经固化为单元/集成回归,不再保留可触发实盘下单的 Web 调试窄接口。相关实盘行为结论和匹配原则见 QMT order_id 匹配。


测试报告

自动输出到:

  • test/integration_test_report.json — JSON 格式
  • test/integration_test_report.md — Markdown 格式

测试分组

组名 优先级 内容
autobuy high 自动买入候选池筛选、条件检查、防重、HTTP 下单
system_integration critical 系统集成、无人值守、线程监控
stop_profit high 动态止盈止损策略(9 个模块)
grid_signal high 网格信号检测与价格追踪
grid_session high 网格会话生命周期管理
grid_trade high 网格买卖执行与资金管理
grid_mece_regression critical 网格状态机、并发预占、委托回调、真实账本、重启恢复等边界
grid_exit high 网格退出条件检测
grid_comprehensive high 网格综合端到端场景
grid_validation medium 参数校验与边界情况
grid_bugfix_c1 critical BUG-C1 修复和 DESIGN-4 约束验证
grid_bug_regression high 已修复 Bug 的回归验证
order_rejection critical QMT 拒单保护和自适应卖出冷却
grid_qa_fixes high MECE 审查修复验证
grid_max_investment_safety critical max_investment 三重防护验证
core_metrics high 网格利润计算与风险分级
trader_callback critical 卖出委托 Callback 兜底机制
web_api critical RESTful API 功能测试
multi_account_isolation critical 多账号配置、数据目录、日志和端口隔离
launcher_deployment high 总控制台环境检查与配置校验
db_thread_safety critical 数据库线程安全、历史保留、事件归档与回归备份安全验证
dual_layer_storage critical 内存 + SQLite 双层存储一致性
xtdata_data_source high xtdata 动态订阅、Mootdx fallback、Tushare 休市空区间、股票名称解析、行情源健康评分
indicator_calculator high 技术指标计算器全方法验证
grid_qa_gap_supplement critical 网格 QA 缺口补充
grid_full_range_coverage critical 全网格价格区间覆盖(114 个用例,A-K 11 个套件)
grid_true_pnl critical True P&L / LIFO 真实盈亏验证
grid_simulation high 价格模拟测试
qmt_ipc_fallback high 大QMT 文件 IPC 客户端、执行器、PositionManager 集成
qmt_rpc high 大QMT RPC 交易后端契约、只读门禁、回调和委托映射
simulation_trading_e2e critical 模拟交易模式端到端(核心链路 / Web 下单 / 策略四分支 / 模式切换)
p1_fixes high 重连缓存刷新 / QMT 自恢复探测 / 信号保活与时效兜底 / 超时泄漏可观测
sellability critical 可卖数量、持仓受限原因、自动补仓门控与网关兼容
fast critical 关键功能子集(当前 59 次模块执行、1487 次用例执行)

编写新测试

from test.test_base import TestBase
from test.test_mocks import MockQmtTrader

class TestMyFeature(TestBase):
    def setUp(self):
        super().setUp()
        self.mock_trader = MockQmtTrader()
        self.mock_trader.add_mock_position("000001.SZ", volume=1000, cost_price=10.0)

    def test_something(self):
        # 测试代码...
        self.wait_for_condition(lambda: condition_met, timeout=5)

测试运行时自动备份生产 DB,测试完成后恢复。