har2pytest
从 HAR 抓包文件和 Swagger 文档生成 Python API 接口文件,并自动生成 pytest + allure 参数化测试用例的 CLI 工具。
安装
pip install har2pytest
验证安装:
har2pytest --help
快速开始
1. 从 HAR 文件生成 API 文件
# 生成 API 文件(同步/异步统一,运行时自动适配)
har2pytest api api_request.har
2. 从 Swagger 文档生成 API 文件
har2pytest swagger https://petstore.swagger.io/v2/api-docs
3. 生成测试用例
命令格式:
har2pytest testcase [har_file] --pattern <模式> [options]
参数说明:
| 参数 | 缩写 | 默认值 | 说明 |
|---|---|---|---|
har_file |
- | api_request.har |
HAR 文件路径(位置参数,可省略) |
--pattern |
- | list_query |
生成模式:list_query / complex_scenario / batch |
--url |
-u |
- | 目标接口 URL,指定后只生成该接口的测试用例 |
--mark |
-m |
- | pytest 标记(如 test_4291),生成 @pytest.mark.test_4291 |
--output |
-o |
testcases |
测试用例输出目录 |
--api-dir |
- | apis |
API 文件目录(用于匹配 API 文件) |
--api-files |
- | api_request.har |
batch 模式专用:API 文件目录或 HAR 文件,多个用逗号分隔 |
--overwrite |
- | False |
强制覆盖已存在的测试用例文件 |
--async |
- | False |
生成异步模式代码(async/await + async_client) |
示例:
# 查询类参数化测试(指定 URL)
har2pytest testcase api_request.har --pattern list_query --url /api/user/list
# 查询类参数化测试(全参数),生成异步测试用例
har2pytest testcase api_request.har \
--pattern list_query \
--url /api/user/list \
--mark test_4291 \
--output testcases/async \
--api-dir apis \
--overwrite \
--async
# 复杂场景流程测试(多步骤业务流),生成同步测试用例
har2pytest testcase api_request.har \
--pattern complex_scenario \
--url /api/order/create \
--mark test_5012 \
--output testcases/sync \
--api-dir apis \
--overwrite
# 批量生成(指定 API 文件目录,无 HAR 文件)
har2pytest testcase \
--pattern batch \
--api-files apis/mall_store_application \
--output testcases/sync \
--api-dir apis \
--overwrite
# 批量生成(指定 HAR 文件,自动提取接口并匹配 API 文件)
har2pytest testcase \
--pattern batch \
--api-files api_request.har \
--output testcases/sync \
--api-dir apis
# 批量生成(多个 API 文件目录,用逗号分隔)
har2pytest testcase \
--pattern batch \
--api-files apis/mall_store_application,apis/mall_center_member \
--output testcases/sync \
--overwrite
# 异步模式测试用例(生成 async/await 代码,自动切换为 async_client)
har2pytest testcase api_request.har \
--pattern list_query \
--url /api/user/list \
--output testcases/async \
--async \
--overwrite
4. 查看 HAR 文件摘要
har2pytest summary api_request.har
详细文档
| 文档 | 说明 |
|---|---|
| API 文件生成 | 从 HAR / Swagger 生成 API 文件的详细说明 |
| HAR 文件解析 | HAR 解析规则与参数过滤 |
| Swagger 文档解析 | Swagger 文档获取与参数提取 |
| 测试用例生成模式 | list_query / complex_scenario / batch 三种模式详解 |
配置
在项目根目录创建 har2pytest_config.json 自定义配置。配置项详见 APIConfig 类(config.py)。
项目结构
har2pytest/
├── har2pytest/ # 核心代码
│ ├── __init__.py
│ ├── __main__.py # CLI 入口
│ ├── client.py # HTTP 客户端 (Client, AsyncClient)
│ ├── config.py # 配置管理 (APIConfig)
│ ├── har_parser.py # HAR 文件解析 (HARParser)
│ ├── har_generator.py # HAR → API 文件生成
│ ├── api_generator.py # API 文件生成 (APIGenerator)
│ ├── swagger_handler.py # Swagger 文档处理 (SwaggerHandler)
│ ├── testcase_generator.py # 测试用例生成 (TestCaseGenerator)
│ ├── url_matcher.py # URL 规范化与匹配 (URLMatcher)
│ ├── utils.py # 工具函数
│ └── logger.py # 日志配置
├── docs/ # 详细文档
├── tests/ # 单元测试
│ └── har2pytest_config_test.json # 测试配置示例
├── pyproject.toml # 项目配置
└── mkdocs.yml # 文档站点配置
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 har2pytest-1.5.0.tar.gz.
File metadata
- Download URL: har2pytest-1.5.0.tar.gz
- Upload date:
- Size: 99.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6b752837a0b2e686a5c2194064b59f7e0bb09e19bf10d217d43433dc4401197
|
|
| MD5 |
197b1f0e003ce44c06f8193083753949
|
|
| BLAKE2b-256 |
98d85383b8767256e423b828693b832a15c06e2c06e683cabe8c2d5dcbd6d80a
|
Provenance
The following attestation bundles were made for har2pytest-1.5.0.tar.gz:
Publisher:
publish.yml on hewei198711/har2pytest
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
har2pytest-1.5.0.tar.gz -
Subject digest:
a6b752837a0b2e686a5c2194064b59f7e0bb09e19bf10d217d43433dc4401197 - Sigstore transparency entry: 2415710077
- Sigstore integration time:
-
Permalink:
hewei198711/har2pytest@b3f687e59799d2d116d9962949347aab5df57804 -
Branch / Tag:
refs/tags/v1.5.0 - Owner: https://github.com/hewei198711
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b3f687e59799d2d116d9962949347aab5df57804 -
Trigger Event:
push
-
Statement type:
File details
Details for the file har2pytest-1.5.0-py3-none-any.whl.
File metadata
- Download URL: har2pytest-1.5.0-py3-none-any.whl
- Upload date:
- Size: 59.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
55a4ae18e5276afb6258fbfb0d89deaca809269e5434bc07088c034afc8e899e
|
|
| MD5 |
cb8039f619129d8de2428ac965a54626
|
|
| BLAKE2b-256 |
11200a96f5c0603f8c437d79e9fe6e414ddabb4e55ff2a53c9dcb41ffe26683f
|
Provenance
The following attestation bundles were made for har2pytest-1.5.0-py3-none-any.whl:
Publisher:
publish.yml on hewei198711/har2pytest
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
har2pytest-1.5.0-py3-none-any.whl -
Subject digest:
55a4ae18e5276afb6258fbfb0d89deaca809269e5434bc07088c034afc8e899e - Sigstore transparency entry: 2415710208
- Sigstore integration time:
-
Permalink:
hewei198711/har2pytest@b3f687e59799d2d116d9962949347aab5df57804 -
Branch / Tag:
refs/tags/v1.5.0 - Owner: https://github.com/hewei198711
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b3f687e59799d2d116d9962949347aab5df57804 -
Trigger Event:
push
-
Statement type: