Python Gateway 当前执行:
python -m coverage run -m unittest discover -s tests -p "test_*.py"
python -m coverage report -m
全量 297 个测试通过,但 .coveragerc 启用了语句和分支覆盖,当前 TOTAL 仅为
93.63%。报告包含 1153 条语句和 418 条分支,其中缺少 52 条语句、48 条分支,
共有 9 个生产文件未达到 100%。
.coveragerc 的 source、omit、branch=True 和报告口径不变。100.00%。100.00%,并通过 coverage report --fail-under=100。不通过 .coveragerc omit、exclude_lines 或 # pragma: no cover 隐藏当前生产代码
缺口。这会改变统计边界,无法证明现有代码路径经过测试。
当前函数级报告未发现必须重构才能触达的代码。默认只修改测试;若实施中证明某条 分支结构上不可达,应暂停并重新评审,而不是直接修改生产逻辑或排除该分支。
| 生产文件 | 测试文件 | 覆盖重点 |
|---|---|---|
tools/query_order_exact.py |
tests/test_query_order_exact_tool.py |
非法 scalar ID、逗号字符串 ID 列表、字符串列表去重回边 |
services/token_store.py |
tests/test_token_store_coverage.py |
无目录文件、延迟读取、已删除文件清理、bytes 请求、非法 Redis 行 |
services/gateway_session_store.py |
tests/test_gateway_session_store_unit.py |
不存在 session 的 touch |
config.py |
tests/test_config_compat.py |
timeout 环境变量优先级、无效 dotenv 行、带引号值和循环回边 |
这一批不改生产代码,只补可观察的输入、返回值、异常和依赖调用断言。
| 生产文件 | 测试文件 | 覆盖重点 |
|---|---|---|
app.py |
tests/test_app_coverage.py |
注册工具名称、动态列表错误、request ID、缺少刷新客户端、public 启动、CLI 必填/可选参数及最终防御分支 |
serve-public 使用 mock 替换 Redis client、session store、public app 和 server 入口;
CLI 防御分支通过模拟参数解析结果触发,不启动真实服务。
| 生产文件 | 测试文件 | 覆盖重点 |
|---|---|---|
mcp_protocol.py |
tests/test_mcp_protocol_coverage.py |
无 flush 输出、无 input schema、工具响应数据形态、错误 data/meta、空轨迹 tips |
public_gateway.py |
tests/test_public_gateway_unit.py |
动态列表无效响应、不支持 touch_session 的 store |
public_server.py |
tests/test_public_server_coverage.py |
input schema 归一化、启用限流、清理线程、KeyboardInterrupt 关闭 |
HTTP server、线程和无限清理循环都使用 mock 控制。清理循环测试捕获
threading.Thread 的 target,并让受控 time.sleep 抛出测试专用异常终止循环,不做
真实等待。
每批执行:
python -m unittest <本批目标测试模块>
python -m coverage run -m unittest discover -s tests -p "test_*.py"
python -m coverage report -m
最终验收:
python -m coverage run -m unittest discover -s tests -p "test_*.py"
python -m coverage report -m --fail-under=100
git diff --check
验收标准:
100.00%。100.00%,Missing 为空。.coveragerc 的统计范围和分支覆盖设置未被放宽。Thread.start
或捕获 target 后同步执行。coverage report -m 为准逐项
处理,不通过删除有效断言或放宽配置收尾。