# ybbguotest **Repository Path**: dontworryNeedMoney/ybbguotest ## Basic Information - **Project Name**: ybbguotest - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-20 - **Last Updated**: 2026-05-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ybbguotest - HSTubeNest 自动化测试框架 基于 pytest + pywinauto 的 HSTubeNest 应用自动化测试框架,支持 GUI 集成测试和 C++ 函数单元测试。 ## 目录结构 ``` ybbguotest/ ├── conftest.py # pytest 全局 fixture 定义 ├── pytest.ini # pytest 配置(markers、testpaths 等) ├── requirements.txt # Python 依赖 ├── run_tests.py # 单元测试启动入口 ├── run_integration_test.py # 集成测试独立运行脚本 ├── testcases/ # 测试用例 │ ├── test_basic_flow.py # GUI 集成测试(启动/打开/排样/NC/保存/完整流程) │ ├── test_connect_running.py # 连接已运行应用的探索与交互测试 │ └── test_group_consecutive_ids.py # C++ groupConsecutiveIds 单元测试 ├── cpp_drivers/ # C++ 测试驱动程序 │ ├── build.bat # 编译脚本 │ ├── test_driver_group_consecutive_ids.cpp # 源码 │ └── test_driver_group_consecutive_ids.exe # 编译产物 ├── tools/ # 辅助工具 │ └── control_explorer.py # 控件树探索工具(导出控件树/按钮/Ribbon控件) ├── test_data/ # 测试数据(需手动放置 .hprj / .step 文件) ├── screenshots/ # 测试失败截图(自动生成) ├── explorer_output/ # 控件树探索输出(自动生成) └── utils/ # 工具模块 ├── app_runner.py # 应用启动/停止管理 ├── checkpoint.py # 测试断言检查点 ├── config.py # 全局配置(含工具栏action映射、快捷键映射) ├── console_reader.py # 控制台输出读取 ├── gui_controller.py # GUI 自动化控制(Ribbon/快捷键/弹窗菜单/截图) └── log_parser.py # 日志文件解析器(错误扫描/事件提取/模式等待) ``` ## 环境要求 - Python >= 3.10 - Visual Studio 2022(编译 C++ 测试驱动程序) - HSTubeNest 应用程序(GUI 集成测试需要) ## 安装 ```powershell pip install -r requirements.txt ``` 依赖列表: - pytest >= 7.0 - pywinauto >= 0.6.8 ## 运行测试 ### 1. 控件树探索(推荐先执行) 连接运行中的 HSTubeNest,导出控件树结构,确认按钮名称和控件类型: ```powershell python run_integration_test.py explore ``` 输出文件在 `explorer_output/` 目录下: - `control_tree.txt` — 完整控件树 - `buttons.json` — 所有按钮列表 - `ribbon_controls.json` — SARibbon 相关控件 ### 2. 连接运行中的应用做基本测试 ```powershell python run_integration_test.py connect ``` ### 3. 解析应用日志 ```powershell python run_integration_test.py logs ``` ### 4. 运行全部集成测试命令 ```powershell python run_integration_test.py all ``` ### 5. 运行 pytest 集成测试 ```powershell # 运行所有集成测试(需要先启动应用、放置测试数据) python -m pytest -m integration # 运行连接测试(连接已运行实例) python -m pytest -m connect # 运行完整流程测试 python -m pytest testcases\test_basic_flow.py::TestFullWorkflow -v ``` ### 6. 运行 C++ 函数单元测试 ```powershell python testcases\test_group_consecutive_ids.py ``` 或使用 pytest: ```powershell python -m pytest testcases\test_group_consecutive_ids.py -v --noconftest ``` > `--noconftest` 用于跳过 conftest.py 中的 GUI fixture,因为 C++ 函数测试不需要启动应用。 ## pytest Markers | Marker | 说明 | |--------|------| | `@pytest.mark.integration` | 集成测试,需要运行 HSTubeNest 应用 | | `@pytest.mark.connect` | 连接已运行实例的测试 | | `@pytest.mark.unit` | 单元测试,不需要应用 | ## GUI 集成测试说明 ### 测试用例覆盖 | 测试类 | 覆盖场景 | |--------|---------| | `TestAppLaunch` | 应用启动、窗口可见性、启动错误检查 | | `TestOpenProject` | 快捷键打开项目、Ribbon按钮打开项目 | | `TestImportPart` | 快捷键导入零件 | | `TestAutoNest` | Ribbon触发自动排样 | | `TestGenerateNC` | Ribbon触发生成NC、NC文件验证 | | `TestSaveProject` | 快捷键保存项目 | | `TestFullWorkflow` | 完整流程(打开→排样→保存→日志校验) | | `TestConnectToRunningApp` | 连接已运行实例、控件树探索、按钮点击 | ### Fixture 说明 | Fixture | 作用域 | 说明 | |---------|--------|------| | `config` | session | 全局配置(exe路径、超时、工具栏映射) | | `app_runner` | session | 应用进程管理(启动/停止) | | `gui_controller` | session | GUI 自动化操作 | | `log_parser` | session | 日志文件解析 | | `checkpoint` | session | 测试断言检查点 | ### 测试数据准备 在 `test_data/` 目录下放置以下文件: - `demo.hprj` — HSTubeNest 项目文件(用于打开/保存测试) - `part.step` — STEP 格式零件文件(用于导入测试) 缺少测试数据时,相关测试会自动 skip。 ### 失败自动截图 测试失败时,conftest 会自动截取主窗口截图保存到 `screenshots/` 目录。 ## GuiController 核心方法 | 方法 | 说明 | |------|------| | `connect(pid/title)` | 连接应用 | | `click_action_by_name(name)` | 按名称点击控件(含 invoke 回退) | | `click_action_by_auto_id(id)` | 按 auto_id 点击控件 | | `click_ribbon_action(text, category)` | 点击 Ribbon 按钮(含 class 回退搜索) | | `send_shortcut(keys)` | 发送快捷键(`^o`=Ctrl+O, `^s`=Ctrl+S) | | `open_project_by_shortcut(path)` | Ctrl+O 打开项目 | | `save_project_by_shortcut()` | Ctrl+S 保存项目 | | `click_popup_menu_item(text, parent)` | 点击弹出菜单项 | | `wait_for_window_title_contains(text)` | 等待窗口标题包含指定文本 | | `screenshot_on_failure(name, dir)` | 失败时截图 | ## LogParser 核心方法 | 方法 | 说明 | |------|------| | `find_latest_log()` | 查找最新日志文件 | | `find_pattern(pattern)` | 正则搜索日志 | | `wait_for_pattern(pattern, timeout)` | 等待日志中出现指定模式 | | `find_errors()` | 扫描错误行 | | `find_key_events()` | 提取关键事件(初始化/打开/排样/NC等) | | `get_log_summary()` | 获取日志摘要 | ## C++ 函数测试原理 对于无法直接从 Python 调用的 C++ 函数,采用 **CLI 驱动程序** 方案: 1. 将 C++ 函数源码复制到 `cpp_drivers/` 下,包装为命令行程序 2. 通过命令行参数传入输入,程序输出 JSON 格式结果 3. Python 测试通过 `subprocess` 调用 exe,解析 JSON 并断言 ### 编译 C++ 驱动程序 ```powershell cpp_drivers\build.bat ``` ### 新增 C++ 函数测试 1. 在 `cpp_drivers/` 下创建 `test_driver_<函数名>.cpp`,包含函数源码和 `main()` 入口 2. `main()` 从 `argv` 读取输入,输出 JSON 结果到 `stdout` 3. 在 `testcases/` 下创建 `test_<函数名>.py`,通过 `subprocess` 调用 exe 4. 运行 `cpp_drivers\build.bat` 编译 ## 配置说明 `utils/config.py` 中的关键配置项: | 配置项 | 默认值 | 说明 | |--------|--------|------| | `exe_path` | `../../bin/Debug/HSTubeNest.exe` | 应用路径 | | `app_start_timeout` | 60s | 启动超时 | | `dialog_timeout` | 10s | 对话框超时 | | `nest_timeout` | 300s | 排样超时 | | `window_title` | `HSTubeNest` | 窗口标题匹配 | | `pywinauto_backend` | `uia` | pywinauto 后端 | | `log_dir` | `../../bin/Debug/log` | 日志目录 | | `toolbar_actions` | 见源码 | 工具栏 action objectName 映射 | | `toolbar_shortcuts` | 见源码 | 工具栏快捷键映射 |