Files

117 lines
6.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# mes-demo — MES 接口自动化测试框架
Python + Requests + Unittest + HTMLTestRunner 实现的 MES 接口自动化测试框架,
覆盖生产计划 / 工单 / 模具 / 质检 / 设备 / 系统管理等业务域。
自带本地 mock 服务与合成数据,不依赖任何外部环境,开箱即跑。
## 一、5 分钟读法
```bash
pip install -r requirements.txt
python runner.py # 启动本地 mock → 跑 18 条用例 → 生成 artifacts/reports/*.html
python -m pytest -q # 也兼容 pytest 入口(需先 pip install pytest)
```
不依赖任何真实环境:mock 服务(`mock/server.py`,纯 stdlib)随 runner 自动拉起,
数据库默认走内存实现 `dao/fake_db.py`。
## 二、解决什么问题
MES 系统(生产计划 / 工单 / 模具 / 质检 / 设备 / 系统管理)接口数量大、回归频繁,
手工回归一次要按模块逐个过 Postman。这个框架把回归收敛为一条命令:
- **用例组织**:`unittest.TestLoader().discover` 按目录自动组装 suite(`runner.py`);
- **报告**:HTMLTestRunner 生成 HTML 报告(`vendor/HTMLTestRunner.py`,Py3 重实现);
- **数据驱动**:用例数据外置 yaml(`fixtures/`),
读取后转 namedtuple 行对象(`core/data.py`);
- **登录态**:`/mes/login` → `JSESSIONID` + token,运行时获取、落盘到 gitignore 的
`artifacts/session.json`(`core/auth.py`);
- **多环境**:`config/config.ini`(local / ci / test)或环境变量 `MES_ENV` / `MES_BASE_URL`;
- **DB 校验**:接口写入后查库比对,`cursor.description → namedtuple` 行对象(`dao/conn.py`);
- **日志**:`@setLog()` 装饰器记录接口返回,含中文转码(`core/logger.py`);
- **报告邮件**:`core/email_sender.py`(smtplib,凭据只走环境变量)。
## 三、用例覆盖清单
| 模块 | 文件 | 场景 |
|---|---|---|
| 登录 | `apis/login/test_login.py` | 成功 / 用户名为空 / 密码为空 / 均为空(数据驱动 4 条) |
| 系统-用户 | `apis/system/test_user.py` | 新增用户正常 / 账号为空 / dao 行对象校验 |
| 系统-物料 | `apis/system/test_material.py` | 物料保存正常 / 编码为空 |
| 生产计划 | `apis/plan/test_work_order.py` | 工单保存正常 / 产品为空 / dao 工单查询 |
| 模具 | `apis/mold/test_mold.py` | 先查设备再批量建模具(完整业务链路)/ 名称为空 |
| 质检 | `apis/quality/test_quality.py` | 质检点新增 / 为空 / dao 质检点+类型查询 |
| 设备 | `apis/equipment/test_oee_export.py` | OEE 导出:multipart 上传(post_file)+ 二进制响应断言 |
请求封装覆盖 6 种方法:`get / post_form / post_json / post_file / put / delete`
(`core/http_client.py`)。
## 四、目录结构
```
mes-demo/
├── runner.py # 入口:mock → unittest discover → HTMLTestRunner
├── conftest.py # pytest 入口(session 级启动 mock)
├── core/ # 工具层
│ ├── http_client.py # 6 种请求方法封装
│ ├── auth.py # 登录态 cookie/token
│ ├── logger.py # @setLog() 日志装饰器
│ ├── data.py # yaml 数据驱动 → namedtuple 行
│ ├── config.py # 多环境(local / ci / test)
│ ├── path.py # 统一路径入口
│ └── email_sender.py # 报告邮件
├── dao/ # 数据层
│ ├── conn.py # 连接封装:cursor.description → namedtuple 行对象
│ ├── fake_db.py # 演示用内存 DB(未配 MES_DB_HOST 时启用)
│ └── {user,work,quality,mold,equipment}.py # 业务 SQL
├── apis/ # 用例层
│ ├── login/ system/ plan/ product/ mold/ quality/ equipment/
├── fixtures/ # 用例数据
├── config/ # 环境配置
├── mock/ # 本地 mock MES 服务(保证开箱即跑)
├── vendor/ # Py3 版 HTMLTestRunner
└── artifacts/ # 运行时产物:报告 / 日志 / 会话(.gitignore)
```
## 五、怎么跑
```bash
# 0) 依赖
pip install -r requirements.txt
# 1) 一键:mock + 全量用例 + HTML 报告
python runner.py
# 2) pytest 入口
python -m pytest -q
# 3) 单独起 mock 调试
python -m mock.server # 监听 127.0.0.1:8080
```
接真实环境时:`cp config/config.example.ini config/config.ini` 填地址,
或用环境变量 `MES_ENV / MES_BASE_URL / MES_USER / MES_PASS / MES_DB_HOST ...`。
## 六、关键难点及我的处理
1. **登录态传递**:JSESSIONID + 业务 token 双态。会话落 `artifacts/session.json`
(已 gitignore),账号口令走环境变量,凭据不进版本库;路径统一走 `core/path.py`,
不出现硬编码的用户目录。
2. **6 种请求的 headers 去重**:保留「每方法独立 headers」的写法,但抽
`_base_headers()` 收敛公共头,方法内只声明差异(如 post_file 不能手动指定
multipart 的 Content-Type),避免改一处 UA 要改 6 遍。
3. **DB 行对象选型**:`cursor.description → namedtuple` 而非 DictCursor——namedtuple
是只读值对象,字段写错立刻 AttributeError,内存更省,贴合「查出数据做断言」的场景。
4. **HTMLTestRunner 的 Py3 化**:`vendor/` 下按 `stream / title / description / run`
同一接口组织,可直接跑在 Py3。
5. **Excel 导入导出场景**:合成数据 + 内存文件流(`test_oee_export.py`)驱动,
不依赖磁盘上的 Excel 模板。
6. **报告文件名**:不用空格(`mes_demo_2026-09-21_21-59-37.html`),
带空格的路径在 CI / 邮件附件场景容易出问题。
7. **框架选型(unittest vs pytest)**:本仓用 unittest,我另有一个 pytest 版框架 ——
永续合约撮合项目,姊妹仓 [contract-match-http-autotest](https://github.com/WbiubiuW/contract-match-http-autotest)。
两套都独立写过之后的判断:选型看**用例规模 + 夹具复杂度 + 报告链路要求**,不看框架流行度。
unittest 的代价集中在三处——数据驱动要手写读取器加循环、夹具只有 `setUp/tearDown`
两个粒度、断言失败只报两个值(裸 `assert` 经 pytest 重写会打印中间结果);
用例上百条、要 session/function 夹具混用、要把覆盖率与 CI 串起来时,pytest 更合适。