mwj-apitest
一款无需编码且易用于二次开发的 HTTP 接口测试框架。支持 JSON、YAML、Python 三种用例格式,内置数据驱动、变量提取、断言校验、失败重跑、多线程执行、HTML 报告生成及多渠道通知(邮件/钉钉/企业微信)。
本框架可拆解用于自研接口自动化平台的接口执行引擎。
特性
- 数据驱动:支持 JSON / YAML / Python 文件编写用例,零代码即可完成接口测试
- 变量系统:
${{var}}引用变量,F{func()}调用内置或自定义函数 - 数据提取:支持 JSONPath 和正则表达式从响应中提取数据
- 断言校验:支持
eq(相等)和contains(包含)两种断言方式 - 钩子机制:
setup_hook/teardown_hook/db_check_hook等前后置钩子 - 数据库校验:支持 MySQL 前后置查询及数据校验
- 多线程执行:基于
ThreadPoolExecutor的并发测试执行 - 失败重跑:可配置重试次数和间隔
- HTML 报告:Jinja2 渲染,支持两种报告模板,含历史趋势数据
- 通知推送:邮件(SMTP)、钉钉机器人、企业微信群消息
环境要求
- Python >= 3.8
- 推荐使用 uv 管理虚拟环境和依赖
安装
使用 uv(推荐)
uv pip install mwj-apitest
使用 pip
pip install mwj-apitest
从源码安装(开发模式)
git clone https://github.com/Lvan826199/mwjApiTest.git
cd mwjApiTest
uv venv
uv pip install -e .
发布到 PyPI
# 1. 修改 pyproject.toml 中的 version 字段
# 2. 构建包
uv build
# 3. 发布到 PyPI(需要 PyPI token)
uv publish --token <your-pypi-token>
# 或使用用户名密码方式
uv publish --username __token__ --password <your-pypi-token>
快速开始
1. 创建项目
mwj create my_project
cd my_project
2. 运行测试
mwj run
3. 指定文件运行
mwj run --file case_yaml/test_case_demo.yaml
命令行参数
mwj create <project_name> 创建测试项目
mwj run 运行当前目录下的所有用例
mwj run --file <path> 指定用例文件或目录
mwj run --thread <n> 设置并发线程数
mwj run --rerun <n> 设置失败重跑次数
mwj run --interval <s> 设置重跑间隔(秒)
mwj -V 查看版本号
用例编写
YAML 格式
- testSet:
name: 用户接口测试
host: http://httpbin.org
interface: /get
method: GET
Cases:
- title: 测试GET请求
interface: /get
method: GET
verification:
- [eq, 200, status_code]
JSON 格式
[
{
"name": "用户接口测试",
"host": "http://httpbin.org",
"interface": "/get",
"method": "GET",
"Cases": [
{
"title": "测试GET请求",
"interface": "/get",
"method": "GET",
"verification": [["eq", 200, "status_code"]]
}
]
}
]
Python 格式
from mwjApiTest.core.httptest import HttpCase
class TestDemo(HttpCase):
name = "接口测试示例"
host = "http://httpbin.org"
interface = "/get"
method = "GET"
Cases = [
{
"title": "测试GET请求",
"interface": "/get",
"method": "GET",
"verification": [["eq", 200, "status_code"]]
}
]
配置文件 (settings.py)
# 调试模式
DEBUG = True
# 日志文件路径
LOG_FILE_PATH = "./logs"
# 全局变量
ENV = {
"host": "http://httpbin.org"
}
# 并发线程数
THREAD = 1
# 失败重跑次数
RERUN = 0
# 重跑间隔(秒)
INTERVAL = 2
# 测试报告配置
TEST_RESULT = {
"filename": "report.html",
"report_dir": "./reports",
"title": "接口测试报告",
"tester": "测试员",
"desc": "接口自动化测试报告",
"templates": 2,
}
# 数据库配置(可选)
DB = [
{
"name": "db1",
"type": "mysql",
"config": {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "123456",
"database": "test_db"
}
}
]
# 邮件通知(可选)
EMAIL = {
"host": "smtp.qq.com",
"port": 465,
"email_account": "your_email@qq.com",
"password": "smtp授权码",
"to_addrs": ["receiver@example.com"],
}
# 钉钉通知(可选)
DINGTALK = {
"webhook": "https://oapi.dingtalk.com/robot/send?access_token=xxx",
"key": "关键字",
}
# 企业微信通知(可选)
WECHAT = {
"chatid": "群ID",
"access_token": "access_token",
}
变量与函数
变量引用
使用 ${{变量名}} 引用环境变量:
headers:
Authorization: "Bearer ${{token}}"
内置函数
使用 F{函数名(参数)} 调用内置数据生成函数:
| 函数 | 说明 |
|---|---|
F{random_mobile()} |
随机手机号 |
F{random_name()} |
随机中文姓名 |
F{random_ssn()} |
随机身份证号 |
F{random_email()} |
随机邮箱 |
F{random_addr()} |
随机地址 |
F{random_city()} |
随机城市 |
F{get_timestamp()} |
当前时间戳 |
F{base64_encode("text")} |
Base64 编码 |
F{md5_encrypt("text")} |
MD5 加密 |
F{rsa_encrypt("msg","pubkey")} |
RSA 加密 |
数据提取
从响应中提取数据并存储到变量中:
extract:
token: [ENV, jsonpath, "$.data.token"]
user_id: [env, re, '"id":\s*(\d+)']
- 第一个参数:变量作用域(
ENV全局 /env局部) - 第二个参数:提取方式(
jsonpath/re) - 第三个参数:提取表达式
断言
verification:
- [eq, 200, status_code]
- [eq, "success", "V{{$.msg}}"]
- [contains, "token", "V{{$.data}}"]
- 第一个参数:断言方式(
eq相等 /contains包含) - 第二个参数:预期值
- 第三个参数:实际值(
status_code或V{{jsonpath表达式}})
许可证
MIT License
Metadata
Release files for mwj-apitest 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mwj_apitest-1.3.0.tar.gz | 39.2 kB | Without provenance | Details for mwj_apitest-1.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mwj_apitest-1.3.0-py3-none-any.whl | Python 3 | none | any | Details for mwj_apitest-1.3.0-py3-none-any.whl |
Total release size: 97.4 kB
Release files / mwj_apitest-1.3.0.tar.gz
| Download URL | mwj_apitest-1.3.0.tar.gz |
|---|---|
| Size | 39.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4fe866b582bffdc71cd8fa054145502be39a23763662c515e9ca9429162f8a39
|
|
BLAKE2b-256 checksum How to use checksums |
697d70f0ea5f8cf1625df9efcd402d79664e124714aba70a5a25ff9c82a37b35
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / mwj_apitest-1.3.0-py3-none-any.whl
| Download URL | mwj_apitest-1.3.0-py3-none-any.whl |
|---|---|
| Size | 58.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2d124fafc6f01e0f703fc5885b1b636953e72f7908740a1a5d4ff6ed6501c376
|
|
BLAKE2b-256 checksum How to use checksums |
85ad3b3594cafb6428ce023f45cd558f81497f38bd9ffda59f36714d4195165b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for mwj-apitest 1.3.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.