# api-test-framework **Repository Path**: li-pengBU/api-test-framework ## Basic Information - **Project Name**: api-test-framework - **Description**: 接口自动化测试框架:分层设计 + YAML 数据驱动 + JSON Schema 契约测试,内置被测服务开箱即跑,28 个用例 2 秒完成 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-11-03 - **Last Updated**: 2026-11-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: pytest, 接口测试, 数据驱动, 测试框架, 自动化测试 ## README # API Test Framework > 一套可以直接拿去做项目的接口自动化框架:**分层设计 + 数据驱动 + 契约测试 + HTML 报告**,`git clone` 下来一条命令就能跑,不依赖任何外部接口。 ![Python](https://img.shields.io/badge/Python-3.10%2B-3776AB) ![pytest](https://img.shields.io/badge/pytest-8.x-0A9EDC) ![tests](https://img.shields.io/badge/tests-28%20passed-brightgreen) ![license](https://img.shields.io/badge/license-MIT-blue) ## 为什么不是又一个「请求 + assert」的脚本堆 大部分接口自动化最后会烂在这几个地方,这个框架每一层都在解决其中一个: | 常见问题 | 这里的做法 | | --- | --- | | URL 散落在用例里,换环境要全局替换 | 环境配置收敛到 `config/config.yaml`,`API_ENV=dev` 一行切换 | | 断言失败只报 `assert 400 == 200`,看不到实际响应 | `Assertor` 失败时打印完整响应体 + 期望值 | | 用例越写越多,接口一改字段名全崩 | `test_contract.py` 用 JSON Schema 锁结构,改字段先挂契约测试 | | 每个用例登录一次,慢 | `token` 是 session 级 fixture,全程只登录一次 | | 造数据互相污染,不能重复执行 | 数据驱动用例的邮箱自动加随机后缀,跑完即删 | | 换台机器跑不起来,还得手动起服务 | `conftest.py` 自动拉起仓库内的 mock 服务 | ## 快速开始 ```bash git clone <本仓库地址> cd api-test-framework pip install -r requirements.txt pytest # 全量,约 2 秒 pytest -m smoke # 只跑冒烟 pytest tests/test_contract.py -v ``` 跑完看报告:`reports/report.html`(自带样式,可直接发给同事)。 不需要事先启动服务——被测的 mock 服务会在测试开始时自动拉起,结束时自动关闭。 ## 执行结果 ``` ............................ [100%] 28 passed in 2.00s ``` | 文件 | 用例数 | 覆盖内容 | | --- | --- | --- | | `test_user_crud.py` | 14 | 登录鉴权、列表/筛选/详情、增删改、404、未授权 | | `test_data_driven.py` | 9 | 由 YAML 驱动的正常/边界/异常场景 | | `test_contract.py` | 5 | 用户对象、列表、错误响应、登录响应的结构契约 | ## 目录结构 ``` api-test-framework/ ├── config/ │ └── config.yaml # 多环境配置:base_url / 超时 / 重试 ├── core/ │ ├── config.py # 配置加载,环境变量可覆盖 │ ├── client.py # 请求封装:日志、耗时、重试、token 注入 │ ├── assertor.py # 断言层:状态码/业务码/字段/Schema/耗时 │ └── logger.py # 双输出日志,落盘 logs/run.log ├── apis/ │ └── user_api.py # 业务接口层,用例里只写语义不写 URL ├── data/ │ └── user_cases.yaml # 数据驱动用例,加场景只改 YAML ├── tests/ │ ├── conftest.py # fixture:自动起服务、登录、client │ ├── test_user_crud.py │ ├── test_data_driven.py │ └── test_contract.py ├── mock_server/ │ └── server.py # 内置被测服务(零依赖,标准库实现) ├── docs/ │ └── 框架设计说明.md ├── pytest.ini └── requirements.txt ``` ## 分层说明 ``` 用例层 (tests/) 只描述业务:创建用户 → 查询 → 校验 ↓ 接口层 (apis/) 一个接口一个方法,隐藏 URL 与参数细节 ↓ 核心层 (core/) 请求、断言、日志、配置,与业务无关 ↓ 被测服务 本地 mock,或换成真实服务的 base_url ``` **换项目怎么改**:只动两个地方——`config/config.yaml` 里的 `base_url`,和 `apis/` 下新增一个接口类。用例层和核心层不用动。 示例,接入一个新接口: ```python # apis/order_api.py class OrderApi: def __init__(self, client): self.client = client def create(self, **fields): return self.client.post("/api/orders", json=fields) ``` ```python # tests/test_order.py def test_create_order(api_order): Assertor(api_order.create(product_id=1, qty=2)).status(201).field("status", "CREATED") ``` ## 数据驱动的写法 `data/user_cases.yaml` 里一条就是一个场景: ```yaml - title: 姓名超过 32 个字符 payload: name: 一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三 email: len33@example.com expect: status: 400 code: INVALID_PARAM ``` 加一条边界用例 = 加 7 行 YAML,不用写 Python。这是把「用例维护」和「代码维护」拆开的关键。 ## 断言层做了什么 ```python Assertor(resp).status(200).field("role", "admin").schema(USER_SCHEMA).faster_than(1000) ``` 一行链式断言四件事:状态码、字段值、JSON Schema 结构、响应耗时上限。 失败时的输出是带响应体的: ``` 状态码不符 期望: 201 实际: 400 响应体: {"code": "INVALID_PARAM", "message": "email 格式不合法"} ``` 排查接口问题时,「实际返回了什么」比「断言挂了」有用得多。 ## 技术栈 Python 3.10+ / pytest / requests / PyYAML / jsonschema / pytest-html ## License MIT