Generate pytest tests from captured fetch/XHR traffic via mitmproxy
Project description
Smoke-Test-Recorder
本工具旨在为0自动化覆盖的团队快速创建一套基于API流量的冒烟测试用例。它通过启动一个本地代理,捕获您在浏览器中与被测系统交互时产生的fetch/XHR流量,并自动生成基于requests的pytest测试代码。
项目结构
auto-pytest-generator/
│
├── src/
│ └── auto_pytest_generator/
│ ├── __init__.py
│ ├── main.py # CLI入口和mitmproxy启动逻辑
│ ├── dispatcher_addon.py # 网络流量分发器,过滤静态文件流量,分发流量对应的pytest_generator实例
| ├── pytest_generator.py # 使用jinja2生成pytest用例的模板逻辑
| ├── response_refresher.py # 用于更新已有用例的response断言部分
│ └── templates/
│ └── pytest_template.jinja2 # Jinja2模板文件
│
├── tests/
│ └── ... # 工具自身的单元测试
│
├── .gitignore
├── pyproject.toml # 项目打包和依赖管理
└── README.md # 使用文档
特点
- 0配置启动:只需指定目标系统的URL前缀即可开始。
- 自动化生成:在浏览器中操作,用例自动生成。
- 基于真实数据断言:使用录制时的响应体做断言,无需依赖接口文档。
- 易于集成:生成的代码是标准的
pytest格式,可直接运行。
安装
# 推荐使用uv或pipx进行安装,以避免污染全局环境
uv tool install auto-pytest-generator
# 或者使用pip
pip install auto-pytest-generator
使用
初次录制
- 启动代理服务 uv run apg proxy --url-prefix http://1.2.3.4:5678/
- 配置浏览器代理到apg代理服务
- 访问 http://mitm.it 检查是否代理成功
- 开始在客户端操作,用例被生成在./generated_tests/
- 拷贝所有*.py文件到测试用例运行环境(依赖pytest,requests)
二次录制
- 已有录制的用例在./tests/中
- (可选)修改./tests/config中修改运行环境和auth token
- 运行uv run apg response-refresh --origin_dir ./tests/ --target-dir ./refreshed_tests/
- apg会自动根据config自动重新运行接口用例,并将结果输出到./refreshed_tests/unchanged/ ./refreshed_tests/data_changed/ ./refreshed_tests/error/ 下
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file auto_pytest_generator-1.0.0.tar.gz.
File metadata
- Download URL: auto_pytest_generator-1.0.0.tar.gz
- Upload date:
- Size: 19.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2317e2b61e6387ab1b187fde345b8cce5b6cd37d90053fa01894f9e1b5b3f1dc
|
|
| MD5 |
2820c49d3761478be79eaa13a50ca212
|
|
| BLAKE2b-256 |
adac37b48d9a2a76fd5d9495ac9e59b76a808fa8633344b1716304e1ac0833a6
|
File details
Details for the file auto_pytest_generator-1.0.0-py3-none-any.whl.
File metadata
- Download URL: auto_pytest_generator-1.0.0-py3-none-any.whl
- Upload date:
- Size: 19.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa957514859236840f3ff966ef7a144bb4086658f14212256202bdaa3b051154
|
|
| MD5 |
a120c94b0774fe432e74f39b053321ff
|
|
| BLAKE2b-256 |
dd24ae32d2d0991a6092e71b8684cc43754be430e154040619636c575230ed0d
|