backend/test/doc/HOW_TO_RUN_FLOWS.md

193 lines
4.4 KiB
Markdown
Raw Normal View History

2025-11-07 07:44:23 +00:00
# 如何運行流程測試
## 📝 說明
`user-profile-flow.js` 是一個**場景模組**,不是可以直接運行的測試文件。它提供了可重複使用的流程函數,需要在測試文件中導入並使用。
## 🚀 快速開始
### 方式 1: 使用已創建的測試文件(推薦)
我們已經創建了使用這些流程的測試文件:
#### 冒煙測試
```bash
# 設置 API 地址
export BASE_URL=http://localhost:8888
# 運行使用者資料流程冒煙測試
make smoke-user-profile-flow
```
#### 負載測試
```bash
# 設置 API 地址
export BASE_URL=http://localhost:8888
# 運行使用者資料流程負載測試
make load-user-profile-flow
```
### 方式 2: 使用通用 run 命令
```bash
# 設置 API 地址
export BASE_URL=http://localhost:8888
# 運行指定的流程測試
make run TEST=tests/smoke/smoke-user-profile-flow-test.js
```
### 方式 3: 直接使用 k6如果本地已安裝
```bash
# 設置 API 地址
export BASE_URL=http://localhost:8888
# 運行測試
k6 run tests/smoke/smoke-user-profile-flow-test.js
```
## 📋 可用的流程函數
`user-profile-flow.js` 提供了以下流程函數:
### 1. `getAndUpdateProfileFlow`
取得並更新個人資料流程
```javascript
import { getAndUpdateProfileFlow } from '../../scenarios/e2e/user-profile-flow.js';
const result = getAndUpdateProfileFlow({
baseUrl: 'http://localhost:8888',
accessToken: 'your_access_token',
updateData: {
nickname: 'NewNickname',
preferred_language: 'zh-tw',
},
});
```
### 2. `emailVerificationFlow`
完整 Email 驗證流程(請求 → 提交)
```javascript
import { emailVerificationFlow } from '../../scenarios/e2e/user-profile-flow.js';
const result = emailVerificationFlow({
baseUrl: 'http://localhost:8888',
accessToken: 'your_access_token',
verifyCode: '123456', // 需要從外部獲取
});
```
### 3. `phoneVerificationFlow`
完整手機驗證流程(請求 → 提交)
```javascript
import { phoneVerificationFlow } from '../../scenarios/e2e/user-profile-flow.js';
const result = phoneVerificationFlow({
baseUrl: 'http://localhost:8888',
accessToken: 'your_access_token',
verifyCode: '123456', // 需要從外部獲取
});
```
### 4. `passwordChangeFlow`
登入狀態下修改密碼流程
```javascript
import { passwordChangeFlow } from '../../scenarios/e2e/user-profile-flow.js';
const result = passwordChangeFlow({
baseUrl: 'http://localhost:8888',
accessToken: 'your_access_token',
currentPassword: 'OldPassword123!',
newPassword: 'NewPassword123!',
});
```
### 5. `userProfileInitializationFlow`
完整的使用者資料初始化流程(註冊 → 登入 → 取得資訊 → 更新資訊)
```javascript
import { userProfileInitializationFlow } from '../../scenarios/e2e/user-profile-flow.js';
const result = userProfileInitializationFlow({
baseUrl: 'http://localhost:8888',
loginId: 'test@example.com',
password: 'Test123456!',
updateData: {
nickname: 'TestUser',
preferred_language: 'zh-tw',
currency: 'TWD',
},
});
```
## 📝 創建自定義測試文件
如果你想創建自己的測試文件來使用這些流程,可以參考以下模板:
```javascript
/**
* 自定義流程測試
*/
import { userProfileInitializationFlow } from '../../scenarios/e2e/user-profile-flow.js';
export const options = {
scenarios: {
custom_flow: {
executor: 'shared-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
tags: { test_type: 'custom', flow: 'user_profile' },
},
},
thresholds: {
checks: ['rate==1.0'],
http_req_duration: ['p(95)<3000'],
},
};
export default function () {
const baseUrl = __ENV.BASE_URL || 'http://localhost:8888';
// 使用流程函數
const result = userProfileInitializationFlow({
baseUrl,
loginId: `test_${Date.now()}@example.com`,
password: 'Test123456!',
});
if (!result.success) {
console.error(`Test failed: ${result.step} - ${result.error}`);
return;
}
console.log('Test passed!');
}
```
然後運行:
```bash
make run TEST=tests/your-custom-test.js
```
## 🎯 測試文件位置
- **冒煙測試**: `tests/smoke/smoke-user-profile-flow-test.js`
- **負載測試**: `tests/pre/load-user-profile-flow-test.js`
## 📚 相關文檔
- [AI_GUIDE.md](./AI_GUIDE.md) - 如何添加新 API 場景
- [README.md](./README.md) - 完整文檔
- [QUICK_START.md](./QUICK_START.md) - 快速開始指南