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 tool install auto-pytest-generator -
升级:
uv tool upgrade auto-pytest-generator
-
-
录制(生成用例)
-
启动代理并录制前端产生的 GET/XHR 流量:
uv run apg recorder --url-prefix http://1.2.3.4:5678/ -
录制完成后,生成的测试用例通常位于 ./generated_tests/(或根据配置输出到指定目录)。
-
配置浏览器代理到 apg 代理服务,访问 http://mitm.it 检查代理是否生效。
-
-
更新 response 断言(refresh)
-
使用已有用例回放并更新 response 断言:
uv run apg response-refresh --origin-dir ./tests -
可选指定输出目录(默认会在项目下生成 refreshed_tests/):
uv run apg response-refresh --origin-dir ./tests --target-dir ./refreshed_tests/ -
输出示例目录: ./refreshed_tests/unchanged/ ./refreshed_tests/data_changed/ ./refreshed_tests/error/
-
-
运行生成的用例
- 将生成的 *.py 拷贝到测试运行环境(依赖 pytest, requests),使用 pytest 运行。
说明
- 录制阶段建议在稳定的 test 环境进行,尽量保证测试数据可重置以提升用例稳定性。
- refresh 阶段用于在新项目或后端变更时校验并自动更新断言,便于迁移验证。
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.1.tar.gz.
File metadata
- Download URL: auto_pytest_generator-1.0.1.tar.gz
- Upload date:
- Size: 20.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78e3f4d346d89f3813e18c1f387dacbef93ca8a550ca888b4d6f991cd88aabe5
|
|
| MD5 |
c22a2eff90a4cca3daf7a146b5a44f0f
|
|
| BLAKE2b-256 |
bebf00f2cf9d1da9a441dc604a5ec601f6a959f714b6df0c5cd3fef03e258651
|
File details
Details for the file auto_pytest_generator-1.0.1-py3-none-any.whl.
File metadata
- Download URL: auto_pytest_generator-1.0.1-py3-none-any.whl
- Upload date:
- Size: 19.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac1062696d7b9a369bcf67763d23d8caa3b58f6c477f471dc62889d810130848
|
|
| MD5 |
e2f01d40a5f29d72af4ad6c696e60c98
|
|
| BLAKE2b-256 |
08ef9e71b583afc11a081159df7173b9094d5cb2a4aa680e9ae1096f5b3c303a
|