Skip to main content

har2pytest

从 HAR 抓包文件和 Swagger 文档生成 Python API 接口文件,并自动生成 pytest + allure 参数化测试用例的 CLI 工具。

主要特性:

  • API 元数据:生成 API 文件时同步写 <module>.api.json,下游结构化读取,不再反向解析生成物
  • 同步/异步自动适配:client 代理默认走同步 requests,通过 client.set_client(async_client) 切换为 aiohttp 异步
  • 模式配置化:支持 TESTCASE_PROFILES 按 URL 显式指定参数化/场景模式
  • Jinja2 代码生成:测试用例与 API 文件由模板渲染,写盘前做语法校验

安装

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;list_query/complex_scenario 模式必填,batch 模式忽略
--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/demo_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/demo_store_application,apis/demo_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 自定义配置,完整配置项见 配置参考。

TESTCASE_PROFILES(推荐)

单个接口“列表参数化 vs 场景流程”的模式判定支持按 URL 显式配置, 避免依赖中文描述关键字(默认仍会回落 LIST_QUERY_KEYWORDS 启发式)。 配置格式、可用模式与优先级说明见 模式配置化(TESTCASE_PROFILES)。

{
    "TESTCASE_PROFILES": [
        { "url": "/mobile/order/List", "mode": "list_query" },
        { "url": "/mobile/order/{orderNo}", "mode": "complex_scenario" },
        { "url": "/mall/order/*", "mode": "auto", "keywords": ["列表"], "exclude_keywords": ["详情"] }
    ]
}

项目结构

har2pytest/
├── har2pytest/              # 核心代码
│   ├── __init__.py
│   ├── __main__.py          # CLI 入口
│   ├── allure_compat.py     # Allure 版本兼容层
│   ├── api_generator.py     # API 文件生成 (APIGenerator)
│   ├── api_metadata.py      # API 元数据 .api.json 读写(结构化事实源)
│   ├── client.py            # HTTP 客户端 (Client, AsyncClient)
│   ├── codegen.py           # Jinja2 渲染 + ast.parse 写盘前语法校验
│   ├── config.py            # 配置管理 (APIConfig)
│   ├── har_generator.py     # HAR → API 文件生成 (generate_api_files_from_har)
│   ├── har_parser.py        # HAR 文件解析 (HARParser)
│   ├── logger.py            # 日志配置
│   ├── models.py            # 核心数据模型 (ParsedRequestInfo/SwaggerInfo/APIFileInfo)
│   ├── param_builder.py     # 参数化条目构建(常量/分页/时间参数过滤)
│   ├── parametrized_codegen.py # list_query 模式测试方法代码生成
│   ├── pytest_utils.py      # pytest 工具 (retry_step 重试装饰器)
│   ├── scenario_codegen.py  # complex_scenario 模式场景测试代码生成
│   ├── swagger_handler.py   # Swagger 文档处理 (SwaggerHandler)
│   ├── testcase_generator.py # 测试用例生成 (TestCaseGenerator)
│   ├── time_pair.py         # 时间参数对处理(start/end 关联与 datetime 计算)
│   ├── url_matcher.py       # URL 规范化与匹配 (URLMatcher)
│   ├── utils.py             # 工具函数(parse_api_file / format_headers / deduplicate_values 等)
│   └── templates/           # *.j2 代码生成模板
├── docs/                    # 详细文档
├── tests/                   # 单元测试
│   └── har2pytest_config_test.json  # 测试配置示例
├── pyproject.toml           # 项目配置
└── mkdocs.yml               # 文档站点配置

Metadata

Release files for har2pytest 1.7.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for har2pytest 1.7.2
File Size Uploaded Provenance Actions
har2pytest-1.7.2.tar.gz 207.2 kB With provenance Details for har2pytest-1.7.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for har2pytest 1.7.2
File Interpreter ABI Platform Actions
har2pytest-1.7.2-py3-none-any.whl Python 3 none any Details for har2pytest-1.7.2-py3-none-any.whl

Total release size: 324.4 kB

Release files / har2pytest-1.7.2.tar.gz

Download URL har2pytest-1.7.2.tar.gz
Size 207.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b6304aa3306019c6688ced1e2679e01cc4fb9672ca2d737e147f5f352d2b2797
BLAKE2b-256 checksum
How to use checksums
6b25196b7573a7ccb1540fdb837541229b7a7d6edbc4e939691d269186f9c1f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Release files / har2pytest-1.7.2-py3-none-any.whl

Download URL har2pytest-1.7.2-py3-none-any.whl
Size 117.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5241e582ee42b1bfbf17bc470400ee1f628ee32b39990631efb3ee29d22f69da
BLAKE2b-256 checksum
How to use checksums
e81e124d4e7b7c56da68e1c869e003d030bcbaa1af481ae53ed960a565af2e84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Security for har2pytest 1.7.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for har2pytest-1.7.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page