Easy-to-use API testing with DevOps automation support
Project description
Drun:现代 HTTP API 测试框架
Drun 是一个面向 HTTP API 的 YAML 驱动测试框架。你可以用简洁的 YAML 描述接口、变量提取、断言、套件编排和报告输出,把接口验证、调试和 CI/CD 串成一条可维护的链路。
核心能力
- YAML DSL:通过
config、steps、extract、validate、caseflow编写测试。 - 模板系统:支持
$var、${ENV(KEY)}、${uuid()}等动态表达式。 - 丰富断言:内置
eq、contains、regex、len_eq、gt等校验。 - 测试编排:支持测试套件、
invoke调用、步骤repeat、标签过滤。 - 延时步骤:支持
sleep: 2这类显式等待 DSL。 - 结果输出:支持 HTML、JSON、Allure 报告,以及日志、代码片段导出。
- 调试友好:支持
drun q快速发请求,支持从 cURL、Postman、HAR、OpenAPI 转换用例。
安装
推荐 Python 3.10+。
pip install drun
如果你使用 uv:
uv venv
source .venv/bin/activate
uv pip install drun
快速开始
1. 初始化项目
drun init myproject
cd myproject
默认会生成类似结构:
myproject/
├── testcases/
├── testsuites/
├── data/
├── converts/
├── logs/
├── reports/
├── snippets/
├── .env
└── Dhook.py
2. 配置环境变量
.env
BASE_URL=https://api.example.com
API_KEY=demo-token
3. 编写第一个测试
testcases/test_user_api.yaml
config:
name: 用户接口测试
base_url: ${ENV(BASE_URL)}
tags: [smoke, user]
steps:
- name: 创建用户
request:
method: POST
path: /users
headers:
Authorization: Bearer ${ENV(API_KEY)}
body:
username: test_${uuid()}
email: test@example.com
extract:
userId: $.data.id
validate:
- eq: [status_code, 201]
- regex: [$.data.id, '^\d+$']
- name: 查询用户
request:
method: GET
path: /users/${ENV(USER_ID)}
headers:
Authorization: Bearer ${ENV(API_KEY)}
validate:
- eq: [status_code, 200]
4. 执行测试
drun run testcases/test_user_api.yaml -env dev
drun run test_user_api -env dev
drun run testcases -env dev -k "smoke and not slow"
drun run test_user_api -env dev -html reports/report.html
说明:
- 支持省略
.yaml扩展名。 - 单文件临时运行时,默认只在当前目录输出一个日志文件。
- 脚手架项目运行时,会默认输出到
logs/、reports/、snippets/。
常用写法
单用例文件
config:
name: 登录接口
base_url: ${ENV(BASE_URL)}
steps:
- name: 登录
request:
method: POST
path: /login
body:
username: admin
password: pass123
extract:
token: $.data.token
validate:
- eq: [status_code, 200]
套件文件
config:
name: 冒烟套件
caseflow:
- name: 登录
invoke: test_login
- name: 查询资料
invoke: test_profile
数据驱动
config:
name: 批量注册
parameters:
- csv:
path: data/users.csv
steps:
- name: 注册 $username
request:
method: POST
path: /register
body:
username: $username
email: $email
validate:
- eq: [status_code, 201]
重复执行
steps:
- name: 重试健康检查
repeat: 3
request:
method: GET
path: /health
validate:
- eq: [status_code, 200]
延时步骤
steps:
- name: 等待服务稳定
sleep: 2
- name: 按变量等待
sleep: ${wait_seconds}
常用命令
运行与调试
drun run PATH -env dev
drun q https://api.example.com/ping
drun q https://api.example.com/users -X POST -d '{"name":"alice"}'
drun tags testcases
drun check testcases
drun fix testcases
格式转换
drun convert sample.curl -outfile out.yaml
drun convert-openapi spec/openapi/ecommerce_api.json -outdir converted
drun export curl testcases/test_user_api.yaml -outfile request.sh
报告服务
drun server
drun server -port 8080
启动后可浏览测试报告列表并查看详情页。
报告与输出
- HTML:适合本地查看与分享。
- JSON:适合流水线消费。
- Allure:适合集成到测试平台。
- Snippets:自动生成 Shell 或 Python 请求脚本,便于复现请求。
示例:
drun run testcases -env dev -html reports/report.html
drun run testcases -env dev -allure-results allure-results
allure serve allure-results
从源码开发
git clone https://github.com/Devliang24/drun.git
cd drun
pip install -e ".[dev]"
python -m pytest -q
python -m build
python -m drun.cli --version
仓库主目录说明:
drun/:核心实现。tests/:回归测试。spec/:示例 OpenAPI 规范。CHANGELOG.md:版本历史。AGENTS.md:贡献者约定与本地开发说明。
适用场景
- HTTP API 回归测试
- 冒烟测试与发布验证
- 测试数据驱动执行
- 接口调试与请求复现
- CI/CD 中的接口质量门禁
贡献
欢迎提交 issue 或 PR。提交前建议至少执行以下命令:
python -m pytest -q
python -m build
drun --help
如果你在仓库内协作开发,请先阅读 AGENTS.md。
License
MIT
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
drun-7.2.13.tar.gz
(151.7 kB
view details)
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
drun-7.2.13-py3-none-any.whl
(158.2 kB
view details)
File details
Details for the file drun-7.2.13.tar.gz.
File metadata
- Download URL: drun-7.2.13.tar.gz
- Upload date:
- Size: 151.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83921e70bb01e1bd5440e57ac920ba044c08c36db5de9018116cef96137e38ce
|
|
| MD5 |
5766103ee476d5f162e96f4dff90b955
|
|
| BLAKE2b-256 |
723544086f0ce990194495eb46bf45fa1f7e31c0e1b8434cef66407d19cf2a6e
|
File details
Details for the file drun-7.2.13-py3-none-any.whl.
File metadata
- Download URL: drun-7.2.13-py3-none-any.whl
- Upload date:
- Size: 158.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81fe2ff2b8c46f7f6ec2117a634ebbe9608bc345ea3c2cde5353bf332712a3d1
|
|
| MD5 |
9e49fa84e1e66fdd8ae0204350488726
|
|
| BLAKE2b-256 |
6e122bb5c0c43a229abc33535cd8159d76181dc45a73f88ee118c34296993f63
|