没有接口测试环境和可以实战的测试项目,怎么练习和操作接口测试?
接口测试练习可以选择 公开免费的开源 / 公益接口(无访问限制、无需 API 密钥、场景覆盖全面),以下推荐 3 个高频用于接口测试的开源 / 公开接口,并提供完整的接口测试方案(含测试用例、Python 代码实现、测试报告)。
|
|
|
|
|
|
|
|
https://jsonplaceholder.typicode.com/
|
|
|
支持 GET/POST/PUT/DELETE,返回标准 JSON,无限制
|
|
|
https://v0.yiketianqi.com/api?unescape=1&version=v9&appid=72523977&appsecret=5nQwKf6Q
|
|
|
|
|
|
https://catfact.ninja/fact
|
|
|
|
核心选择:JSONPlaceholder(最适合接口测试)
▪ 开源地址:https://github.com/typicode/jsonplaceholder(15.6k Star,维护活跃)
• 创建用户:POST /users(模拟提交,不实际存储)
• 更新用户:PUT /users/1(模拟更新)
• 删除用户:DELETE /users/1(模拟删除)
▪ 优势:无访问频率限制、响应格式标准、支持所有 HTTP 方法,完美覆盖接口测试核心场景。
二、完整接口测试方案(以 JSONPlaceholder 为例)
验证 JSONPlaceholder 接口的 功能正确性、响应格式、状态码、异常处理,生成结构化测试报告。
pip install requests pytest pytest-html # 核心依赖
▪ pytest:测试框架(用例管理、断言、执行)
▪ pytest-html:生成 HTML 测试报告
|
|
|
|
|
|
|
|
|
|
|
状态码 200;响应为 JSON 数组;数组长度 10;包含字段id/name/email
|
|
|
|
|
|
状态码 200;id=1;name="Leanne Graham"
|
|
|
|
|
|
状态码 404;响应为空对象或提示 “not found”
|
|
|
|
|
|
状态码 201;返回创建的用户数据;包含自定义的name/email字段
|
|
|
|
|
|
状态码 200;返回更新后的完整数据;name字段为修改后的值
|
|
|
|
|
|
状态码 200;仅更新指定字段(如email),其他字段保持不变
|
|
|
|
|
|
状态码 200 或 204;响应为空或提示删除成功
|
|
|
|
|
|
状态码 200;返回用户中name包含 “Ervin” 的记录(预期 1 条)
|
四、Python 测试代码实现(pytest 框架)
创建 test_jsonplaceholder_api.py 文件,代码如下:
# 基础URL(JSONPlaceholder开源接口)
BASE_URL = "https://jsonplaceholder.typicode.com"
# ------------------- 测试用例 -------------------
def test_get_all_users():
response = requests.get(f"{BASE_URL}/users")
assert response.status_code == 200, f"预期状态码200,实际{response.status_code}"
assert response.headers["Content-Type"].startswith("application/json"), "响应格式不是JSON"
assert isinstance(data, list), "响应应为数组格式"
assert len(data) == 10, f"预期10个用户,实际{len(data)}个"
assert all(key in user for key in ["id", "name", "email", "username"]), "用户数据缺少核心字段"
def test_get_single_existing_user():
response = requests.get(f"{BASE_URL}/users/{user_id}")
assert response.status_code == 200
assert data["id"] == user_id
assert data["name"] == "Leanne Graham"
assert data["email"] == "Sincere@april.biz"
def test_get_single_non_existing_user():
response = requests.get(f"{BASE_URL}/users/{user_id}")
assert response.status_code == 404, f"预期状态码404,实际{response.status_code}"
assert data == {} or "not found" in str(data).lower(), f"404响应异常:{data}"
"email": "test@example.com",
"website": "test.example.com"
response = requests.post(f"{BASE_URL}/users", json=new_user)
assert response.status_code == 201, f"预期状态码201,实际{response.status_code}"
assert data["name"] == new_user["name"]
assert data["email"] == new_user["email"]
assert "id" in data, "创建的用户缺少id字段"
def test_update_user_put():
"""TC-005:全量更新用户信息(PUT)"""
"username": "updateduser",
"email": "updated@example.com",
"website": "updated.example.com"
response = requests.put(f"{BASE_URL}/users/{user_id}", json=updated_user)
assert response.status_code == 200
assert data["name"] == updated_user["name"]
assert data["email"] == updated_user["email"]
assert data["phone"] == updated_user["phone"]
def test_update_user_patch():
"""TC-006:部分更新用户信息(PATCH)"""
original_response = requests.get(f"{BASE_URL}/users/{user_id}")
original_data = original_response.json()
patch_data = {"email": "patched@example.com"}
response = requests.patch(f"{BASE_URL}/users/{user_id}", json=patch_data)
assert response.status_code == 200
assert data["email"] == patch_data["email"]
assert data["name"] == original_data["name"]
assert data["phone"] == original_data["phone"]
response = requests.delete(f"{BASE_URL}/users/{user_id}")
# JSONPlaceholder返回200(部分REST API返回204)
assert response.status_code in [200, 204], f"预期状态码200/204,实际{response.status_code}"
# 验证删除后无法获取该用户(JSONPlaceholder模拟删除,实际未删除,此处仅验证响应)
assert data == {}, f"删除响应异常:{data}"
def test_get_users_with_query_param():
response = requests.get(f"{BASE_URL}/users", params={"name": query_param})
assert response.status_code == 200
# 预期返回name包含"Ervin"的用户(JSONPlaceholder中为Ervin Howell)
assert len(data) == 1, f"预期1条结果,实际{len(data)}条"
assert query_param in data[0]["name"], f"用户名称不包含{query_param}"
# ------------------- 运行入口 -------------------
if __name__ == "__main__":
"--html=jsonplaceholder_api_test_report.html", # 生成HTML报告
"--self-contained-html", # 报告为独立文件(便于分享)
"test_jsonplaceholder_api.py"
python test_jsonplaceholder_api.py
或直接用 pytest 命令(更灵活):
pytest test_jsonplaceholder_api.py -v --html=api_test_report.html --self-contained-html
• 运行完成后,当前目录会生成 jsonplaceholder_api_test_report.html 文件
六、测试报告示例(JSONPlaceholder 接口测试结果)
|
|
|
|
|
|
|
|
|
PASS |
|
|
|
|
|
PASS |
|
|
|
|
|
PASS |
|
|
|
|
|
PASS |
|
|
|
|
|
PASS |
|
|
|
|
|
PASS |
|
|
|
|
|
PASS |
|
|
|
|
|
PASS |
|
|
总体结果:8 个用例全部通过,JSONPlaceholder 接口功能正常、响应符合 REST 规范。
......
本文节选自第九十二期《51测试天地》原创文章
《没有接口测试环境和项目,怎么练习接口测试?》
文章后续为大家详细讲解了:
其他可选开源接口与方案注意事项等
想继续阅读全文
或查看更多《51测试天地》的原创文章
请点击下方 阅读原文或扫描二维码 查看
声明:本文为51Testing软件测试网 Bigder 用户投稿内容,该用户投稿时已经承诺独立承担涉及知识产权的相关法律责任,并且已经向51Testing承诺此文并无抄袭内容。发布本文的用途仅仅为学习交流,不做任何商用,未经授权请勿转载,否则作者和51Testing有权追究责任。如果您发现本公众号中有涉嫌抄袭的内容,欢迎发送邮件至:editor@51testing.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。