革新性CD数字归档:foobox-cn让音乐收藏重获新生的技术探索

核心内容摘要

PowerPaint-V1开源镜像实战:直播电商实时背景净化边缘推理方案
Java实习模拟面试:集合、并发、JVM与Spring核心高频题深度解析(附连环追问)

Mirage Flow在VMware虚拟机中的部署方案

第 18 篇按依赖顺序 复杂度由低到高逐步推进pytest单元测试**我们采用“依赖顺序 复杂度由低到高”的策略组织单元测试确保底层模块先通过测试上层模块再依赖可靠的基础。

测试优先覆盖纯函数、无外部依赖的模块再逐步扩展到涉及线程、信号、文件 I/O、模拟器等复杂模块。

测试模块顺序依赖与复杂度递增core/protocol/pelco_protocol.py—— 最基础纯函数无外部依赖core/macro/standard.py—— CommandValidator、范围校验core/alarm/rules.py—— load/save/executecore/macro/parser.py—— lexer/parsercore/template/renderer.py—— 模板渲染core/utils/log_emitter.py—— 信号、缓冲区core/macro/commands.py—— 命令执行核心core/macro/engine.py—— 宏执行引擎core/serial/protocol.py—— 帧提取与解析core/serial/manager.py worker.py—— 串口管理与线程core/template/library.py—— 模板文件管理core/simulator/virtual_device.py—— 模拟器状态机

1

1 core/protocol/pelco_protocol.py 单元测试测试文件tests/unit/test_pelco_protocol.py覆盖要点build_pelco_d / build_pelco_p不同参数组合parse_pelco_packetD/P 协议自动识别、PTZ、预置位、AUX、校验和错误、未知命令9 字节变体兼容性若启用边界值与非法帧处理运行命令pytest tests/unit/test_pelco_protocol.py -v --tbshort预期所有测试用例通过显示具体参数化测试名称及 PASS。

1

2 core/macro/standard.py 单元测试测试文件tests/unit/test_standard.py覆盖要点常量、枚举完整性COMMAND_SPECS 结构校验validate_command正常、未知命令、参数数量/类型/范围错误E001~E004可选参数处理cam_id 默认值静态方法get_command_spec、list_all_commands、get_commands_by_category边界值speed ±

preset 1~

aux 1~8运行命令pytest tests/unit/test_standard.py -v --tbshort预期约 20 个测试全部通过。

1

3 core/alarm/rules.py 单元测试测试文件tests/unit/test_alarm_rules.py覆盖要点load_alarm_rules / save_alarm_rules文件读写、空文件、异常处理execute_alarm_action不同 action 类型run_macro、load_template、notify、ack 等规则过滤enabled、alarm_code 匹配最后触发时间更新运行命令pytest tests/unit/test_alarm_rules.py -v --tbshort预期全部通过含 mock 文件 I/O 和动作执行。

1

4 core/macro/parser.py 单元测试测试文件tests/unit/test_parser.py覆盖要点Lexertoken 识别数字、ID、字符串、注释、符号Parser简单命令、字符串、变量、loop、for、if/else、嵌套结构语法错误恢复不崩溃返回部分 AST位置信息line/column准确性运行命令pytest tests/unit/test_parser.py -v --tbshort

1

5 core/template/renderer.py 单元测试测试文件tests/unit/test_renderer.py覆盖要点} → 原始值替换 → 类型转换int/float → 数字字符串、bool → “true”/“false”、str → 加引号缺少参数 → ValueError 日志空参数字典 → 返回原始脚本运行命令pytest tests/unit/test_renderer.py -v --tbshort

1

6 core/utils/log_emitter.py 单元测试测试文件tests/unit/test_log_emitter.py覆盖要点单例模式get_log_emitter 返回同一实例各日志方法info、warning、error、send、receive、simulator、alarm信号发射缓冲区机制use_bufferTrue 时进入缓冲flush 后触发 logs_batcherror 触发 error_occurred 信号非法日志类型自动转为 info运行命令pytest tests/unit/test_log_emitter.py -v --tbshort

1

7 core/macro/commands.py 单元测试测试文件tests/unit/test_commands.py覆盖要点各 cmd_xxx 方法ptz_control、zoom、focus、iris、preset、aux、pattern 等参数验证CommandValidator默认 cam_id 处理执行日志记录异常情况未知命令、无效参数运行命令pytest tests/unit/test_commands.py -v --covcore/macro/commands --cov-reportterm-missing

1

8 core/macro/engine.py 单元测试测试文件tests/unit/test_macro_engine.py覆盖要点生命周期init、set_script、run、stop、pause/resumeAST 执行命令、loop、for、if信号started、stopped、error、progress、command_executed进度计算、检查点、缓存异常安全与日志运行命令pytest tests/unit/test_macro_engine.py -v --tbshort

1

9 core/serial/protocol.py 单元测试测试文件tests/unit/test_serial_protocol.py覆盖要点detect_protocolD/P/未知extract_frame完整帧、不完整、校验失败parse_framePTZ、zoom、preset、query、错误帧、Pelco-P 未知命令校验和验证运行命令pytest tests/unit/test_serial_protocol.py -v --tbshort

1

10 core/serial/manager.py worker.py 单元测试测试文件tests/unit/test_serial_manager_worker.py覆盖要点open/close/write/is_open信号转发opened、closed、error、data_received、parsed_received默认 cam_id 同步到控制方法运行命令pytest tests/unit/test_serial_manager_worker.py -v --tbshort

1

11 core/template/library.py 单元测试测试文件tests/unit/test_template_library.py覆盖要点load/saveJSON 读写、异常处理find存在/不存在_validate_template合法/非法结构_create_default_templates缓存机制首次加载后复用运行命令pytest tests/unit/test_template_library.py -v --tbshort

1

12 core/simulator/virtual_device.py 单元测试测试文件tests/unit/test_virtual_device.py覆盖要点process_commandACK、查询响应、错误响应update_from_commandPTZ、zoom、focus、iris、auxgenerate_response各查询类型状态范围限制pan/tilt/zoom运行命令pytest tests/unit/test_virtual_device.py -v --tbshort测试执行建议按顺序运行上述测试文件确保底层模块先通过。

使用--cov生成覆盖率报告关注未覆盖分支。

所有测试均使用 mock 避免真实串口/文件依赖保证快速稳定。

通过后可继续集成测试serial protocol macro。

下一阶段完成以上单元测试后我们进入集成测试阶段。

上一篇 按照pytest自动化测试方案规划建立测试基础框架总目录Python开发软键盘全程总览下一篇

欢乐谷https//5.hlg2931dcc8888-欢乐谷应用

百度百家号客服电话人工服务

123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123