|
| 1 | +--- |
| 2 | +description: BDD - 從 feature file 和架構文件生成 integration tests(測試先行) |
| 3 | +--- |
| 4 | + |
| 5 | +# SDD-INTEGRATION-TEST: BDD Integration Tests |
| 6 | + |
| 7 | +**輸入:** {{prompt}} |
| 8 | +格式:`<feature_file_path>` |
| 9 | +範例:`features/shopping_cart.feature` |
| 10 | + |
| 11 | +**角色:** 協助開發者實踐 BDD(行為驅動開發)測試先行 |
| 12 | + |
| 13 | +**目標:** |
| 14 | +1. 讀取 feature file 和對應的 architecture.md |
| 15 | +2. 生成 integration tests(先寫測試,後寫實作) |
| 16 | +3. 測試會失敗(紅燈),等待實作後轉為通過(綠燈) |
| 17 | + |
| 18 | +## 核心原則 |
| 19 | +- **BDD**:測試描述業務行為,非技術實作 |
| 20 | +- **Integration**:測試真實整合場景(API/DB/Service) |
| 21 | +- **Scenario-driven**:每個 Scenario 對應一個測試案例 |
| 22 | +- **測試先行**:先寫測試(紅燈)→ 再實作(綠燈) |
| 23 | + |
| 24 | +## 執行步驟 |
| 25 | + |
| 26 | +### 1. 讀取輸入檔案 |
| 27 | +```bash |
| 28 | +# 讀取 feature file |
| 29 | +cat <feature_file_path> |
| 30 | + |
| 31 | +# 讀取對應的架構文件 |
| 32 | +cat docs/features/<feature_name>/architecture.md |
| 33 | +``` |
| 34 | + |
| 35 | +### 2. 掃描專案與框架偵測 |
| 36 | +```bash |
| 37 | +ls -la | grep -E "package.json|requirements.txt|go.mod|pom.xml" |
| 38 | +find . -name "*test*" -o -name "*spec*" | head -3 |
| 39 | +``` |
| 40 | + |
| 41 | +**框架優先順序:** architecture.md 指定 > 專案既有 > 預設 |
| 42 | +- **TypeScript**: Jest/Vitest + Supertest |
| 43 | +- **Python**: pytest + httpx |
| 44 | +- **Go**: testing + testify |
| 45 | + |
| 46 | +### 3. 從 architecture.md 提取資訊 |
| 47 | +- 程式語言與框架 |
| 48 | +- 資料模型(實體、列舉) |
| 49 | +- 服務介面(方法簽名) |
| 50 | +- 檔案結構與路徑 |
| 51 | + |
| 52 | +### 4. 從 feature file 提取測試場景 |
| 53 | +- 每個 Scenario 對應一個測試案例 |
| 54 | +- 從 Given-When-Then 轉換為測試邏輯 |
| 55 | +- 保持業務語言描述 |
| 56 | + |
| 57 | +### 5. 測試結構(BDD 風格) |
| 58 | + |
| 59 | +每個測試遵循 **Given-When-Then** 模式: |
| 60 | +- **Given**: 準備測試資料與環境 |
| 61 | +- **When**: 執行被測試的行為 |
| 62 | +- **Then**: 驗證預期結果 |
| 63 | + |
| 64 | +**測試命名:** 使用 Scenario 描述,保持業務語言 |
| 65 | +- ✅ `應該拒絕無效的電子郵件格式` |
| 66 | +- ✅ `當庫存不足時應該返回錯誤` |
| 67 | +- ❌ `test_validate_email_regex` |
| 68 | + |
| 69 | +## 測試要求 |
| 70 | +- [ ] 根據 architecture.md 使用正確的資料模型與介面 |
| 71 | +- [ ] 根據 feature file 的所有 Scenario 生成測試 |
| 72 | +- [ ] Given-When-Then 結構清晰 |
| 73 | +- [ ] 適當的 setup/teardown(例如:資料庫連線、測試資料清理) |
| 74 | +- [ ] 可編譯/可執行(但會失敗,因為功能未實作) |
| 75 | + |
| 76 | +## 生成測試檔案 |
| 77 | + |
| 78 | +**檔案位置:** |
| 79 | +- 依據 architecture.md 指定的測試目錄 |
| 80 | +- 或依專案既有測試結構 |
| 81 | +- 預設:`tests/integration/<feature_name>.test.<ext>` |
| 82 | + |
| 83 | +**測試內容包含:** |
| 84 | +1. Import 對應的資料模型與服務介面(從 architecture.md) |
| 85 | +2. 測試 setup/teardown |
| 86 | +3. 每個 Scenario 一個測試函數 |
| 87 | +4. Given-When-Then 結構註解標示 |
| 88 | +5. 呼叫尚未實作的服務(會導致測試失敗/紅燈) |
| 89 | + |
| 90 | +## 驗證 |
| 91 | +```bash |
| 92 | +# 執行測試(預期失敗/紅燈,因為功能尚未實作) |
| 93 | +npm test / pytest -v / go test -v |
| 94 | +``` |
| 95 | + |
| 96 | +**預期結果:** 測試會失敗(紅燈),這是正常的。 |
| 97 | + |
| 98 | +## BDD 工作流程(測試先行) |
| 99 | + |
| 100 | +1. **規格階段**:定義 feature file(`/sdd-spec`) |
| 101 | +2. **架構階段**:設計技術架構(`/sdd-arch`) |
| 102 | +3. **測試階段**(本指令):生成 integration tests(🔴 紅燈) |
| 103 | +4. **實作階段**:實作功能(`/sdd-impl`) |
| 104 | +5. **驗證階段**:確認測試通過(`/sdd-verify` → 🟢 綠燈) |
| 105 | + |
| 106 | +## 使用範例 |
| 107 | + |
| 108 | +```bash |
| 109 | +# 先執行 sdd-spec 和 sdd-arch |
| 110 | +/sdd-spec Create a shopping cart with add, remove, checkout |
| 111 | +/sdd-arch features/shopping_cart.feature |
| 112 | + |
| 113 | +# 生成 integration tests(測試會失敗/紅燈) |
| 114 | +/sdd-bds features/shopping_cart.feature |
| 115 | + |
| 116 | +# 實作功能 |
| 117 | +/sdd-impl features/shopping_cart.feature |
| 118 | + |
| 119 | +# 驗證(測試應該通過/綠燈) |
| 120 | +/sdd-verify features/shopping_cart.feature |
| 121 | +``` |
| 122 | + |
| 123 | +開始讀取檔案並生成 integration tests。 |
0 commit comments