test(expiry): 完成 #32 接口集成测试资产

This commit is contained in:
root
2026-03-04 18:37:45 +08:00
parent 6c303abd58
commit 3668b10be3
4 changed files with 314 additions and 0 deletions
@@ -0,0 +1,33 @@
# 保质期接口集成测试报告(2026-03-04)
## 范围
- 登录获取 Token`/api/v1/auth/login`
- 物品接口:创建、列表、汇总、更新、状态标记、删除
- 设置接口:获取与更新
## 测试资产
- Postman Collection: `docs/expiry/tests/postman/expiry.postman_collection.json`
- Postman Environment: `docs/expiry/tests/postman/expiry.postman_environment.json`
- 一键脚本: `scripts/expiry/run_integration_tests.sh`
## 执行方式
```bash
# 方式一:已有 token
BASE_URL=http://127.0.0.1:8080 TOKEN=<session_key> scripts/expiry/run_integration_tests.sh
# 方式二:通过 wx.login 的 code 登录
BASE_URL=http://127.0.0.1:8080 MINI_PROGRAM_ID=1 WX_CODE=<wx_code> scripts/expiry/run_integration_tests.sh
```
## 断言规则
- 登录接口:`HTTP 200 && code == 200`
- 业务接口:`HTTP 200 && code == 0`
- 错误接口:预期返回 4xx/5xx 与对应 message
## 性能测试建议
脚本内置 `hey` 调用(若系统已安装):
```bash
hey -n 100 -c 20 -H "Authorization: Bearer <token>" http://127.0.0.1:8080/api/expiry/summary
```
目标:平均响应时间 < 200ms(开发环境仅作参考,生产环境需复测)。
@@ -0,0 +1,170 @@
{
"info": {
"_postman_id": "3f8f9f2a-8b2f-4cc0-91df-0c8d7a03b101",
"name": "Expiry API Integration",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"description": "保质期提醒小程序后端接口集成测试集合"
},
"item": [
{
"name": "Auth - Login",
"request": {
"method": "POST",
"header": [
{ "key": "Content-Type", "value": "application/json" }
],
"body": {
"mode": "raw",
"raw": "{\n \"mini_program_id\": {{mini_program_id}},\n \"code\": \"{{wx_code}}\"\n}"
},
"url": {
"raw": "{{base_url}}/api/v1/auth/login",
"host": ["{{base_url}}"],
"path": ["api", "v1", "auth", "login"]
}
},
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test('login status 200', function () {",
" pm.response.to.have.status(200);",
"});",
"const json = pm.response.json();",
"pm.test('login business code 200', function () {",
" pm.expect(json.code).to.eql(200);",
"});",
"if (json.data && json.data.session_key) {",
" pm.environment.set('token', json.data.session_key);",
"}"
],
"type": "text/javascript"
}
}
]
},
{
"name": "Expiry - Create Item",
"request": {
"method": "POST",
"header": [
{ "key": "Content-Type", "value": "application/json" },
{ "key": "Authorization", "value": "Bearer {{token}}" }
],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"自动化牛奶\",\n \"category\": \"food\",\n \"expiry_date\": \"2030-12-31\",\n \"quantity\": 2,\n \"location\": \"冰箱\"\n}"
},
"url": {
"raw": "{{base_url}}/api/expiry/items",
"host": ["{{base_url}}"],
"path": ["api", "expiry", "items"]
}
},
"event": [
{
"listen": "test",
"script": {
"exec": [
"const json = pm.response.json();",
"pm.test('create success', function () {",
" pm.expect(pm.response.code).to.eql(200);",
" pm.expect(json.code).to.eql(0);",
"});",
"if (json.data && json.data.id) {",
" pm.environment.set('item_id', json.data.id);",
"}"
],
"type": "text/javascript"
}
}
]
},
{
"name": "Expiry - Get Items",
"request": {
"method": "GET",
"header": [
{ "key": "Authorization", "value": "Bearer {{token}}" }
],
"url": {
"raw": "{{base_url}}/api/expiry/items?status=all&page=1&page_size=20",
"host": ["{{base_url}}"],
"path": ["api", "expiry", "items"],
"query": [
{ "key": "status", "value": "all" },
{ "key": "page", "value": "1" },
{ "key": "page_size", "value": "20" }
]
}
}
},
{
"name": "Expiry - Get Summary",
"request": {
"method": "GET",
"header": [
{ "key": "Authorization", "value": "Bearer {{token}}" }
],
"url": {
"raw": "{{base_url}}/api/expiry/summary",
"host": ["{{base_url}}"],
"path": ["api", "expiry", "summary"]
}
}
},
{
"name": "Expiry - Update Status Used",
"request": {
"method": "POST",
"header": [
{ "key": "Content-Type", "value": "application/json" },
{ "key": "Authorization", "value": "Bearer {{token}}" }
],
"body": {
"mode": "raw",
"raw": "{\n \"status\": \"used\"\n}"
},
"url": {
"raw": "{{base_url}}/api/expiry/items/{{item_id}}/status",
"host": ["{{base_url}}"],
"path": ["api", "expiry", "items", "{{item_id}}", "status"]
}
}
},
{
"name": "Expiry - Settings Get",
"request": {
"method": "GET",
"header": [
{ "key": "Authorization", "value": "Bearer {{token}}" }
],
"url": {
"raw": "{{base_url}}/api/expiry/settings",
"host": ["{{base_url}}"],
"path": ["api", "expiry", "settings"]
}
}
},
{
"name": "Expiry - Settings Update",
"request": {
"method": "POST",
"header": [
{ "key": "Content-Type", "value": "application/json" },
{ "key": "Authorization", "value": "Bearer {{token}}" }
],
"body": {
"mode": "raw",
"raw": "{\n \"remind_days\": [10, 5, 1]\n}"
},
"url": {
"raw": "{{base_url}}/api/expiry/settings",
"host": ["{{base_url}}"],
"path": ["api", "expiry", "settings"]
}
}
}
]
}
@@ -0,0 +1,14 @@
{
"id": "4f72d514-2fe4-4125-9ef0-2b5240ca07c4",
"name": "Expiry Local Env",
"values": [
{ "key": "base_url", "value": "http://127.0.0.1:8080", "enabled": true },
{ "key": "mini_program_id", "value": "1", "enabled": true },
{ "key": "wx_code", "value": "请替换为wx.login()返回code", "enabled": true },
{ "key": "token", "value": "", "enabled": true },
{ "key": "item_id", "value": "", "enabled": true }
],
"_postman_variable_scope": "environment",
"_postman_exported_at": "2026-03-04T18:40:00.000Z",
"_postman_exported_using": "postman"
}