Chapter 03: Chapter 03

學習目標:
  • 待定義
前置需求:
  • 完成 Chapter 2,擁有一個可運行的 TODO 應用
  • 理解基本的軟體測試概念
  • 熟悉 JavaScript/TypeScript
  • 準備好 Playwright 測試環境
預計時間: 2 hours

§ 第二樂章:AI 擔任測試策略師

§ 章節概述

在上一章中,我們學會了如何指揮 AI 生成應用程式。現在,我們要讓 AI 轉換角色,成為專業的測試策略師。AI 將分析程式碼、理解功能,並制定全面的測試計劃,確保應用程式的品質和可靠性。

§ 學習目標

完成本章節後,你將能夠:

  • 引導 AI 分析程式碼並理解應用架構
  • 讓 AI 制定完整的端到端測試策略
  • 理解測試覆蓋率的重要性和計算方法
  • 評估 AI 生成的測試計劃品質
  • 掌握測試優先級和風險評估技巧

§ 前置需求

  • 完成 Chapter 2,擁有一個可運行的 TODO 應用
  • 理解基本的軟體測試概念
  • 熟悉 JavaScript/TypeScript
  • 準備好 Playwright 測試環境

§ 核心概念

§ 1. AI 作為測試策略師的優勢

傳統測試策略制定需要:

  • 深入理解業務需求
  • 分析程式碼結構
  • 識別關鍵路徑
  • 評估風險區域
  • 設計測試案例

AI 測試策略師能夠:

  • 快速分析整個程式碼庫
  • 識別潛在的邊界情況
  • 生成全面的測試場景
  • 優先排序測試重要性
  • 預測可能的失敗點

§ 2. 測試金字塔與 AI 的結合

        /\
       /E2E\      <- AI 擅長設計使用者旅程
      /______\
     /整合測試\    <- AI 識別元件互動
    /__________\
   /  單元測試   \  <- AI 確保函數正確性
  /______________\

§ 3. 測試策略的組成要素

一個完整的測試策略應包含:

  1. 測試範圍 (Test Scope)
  2. 測試類型 (Test Types)
  3. 測試優先級 (Test Priority)
  4. 測試資料 (Test Data)
  5. 預期結果 (Expected Results)
  6. 風險評估 (Risk Assessment)

§ 實作練習:制定 TODO 應用測試策略

§ 步驟 1:準備程式碼分析提示詞

You are an expert QA strategist and test architect. Please analyze the following TODO application code and create a comprehensive test strategy.

[Code Analysis Request]
1. Review the application architecture
2. Identify key functionalities
3. Locate critical user paths
4. Find potential failure points
5. Assess data flow and state management

[Test Strategy Requirements]
Please provide:
1. Test scope definition
2. Test types breakdown (unit, integration, E2E)
3. Priority matrix for test cases
4. Test data requirements
5. Risk assessment and mitigation

[Output Format]
Present the strategy in Traditional Chinese with:
- Executive summary
- Detailed test plan
- Test case specifications
- Risk matrix
- Implementation roadmap

請使用繁體中文撰寫完整的測試策略文檔。

§ 步驟 2:功能分解與測試映射

讓 AI 創建功能到測試的映射表:

Create a function-to-test mapping for the TODO application:

[Core Functions]
1. Create TODO
2. Update TODO
3. Delete TODO
4. Mark Complete/Incomplete
5. Filter TODOs
6. Search TODOs
7. Data Persistence
8. UI Interactions

For each function, specify:
- Happy path scenarios
- Edge cases
- Error conditions
- Performance considerations
- Security concerns

輸出格式請使用表格,並以繁體中文說明每個測試案例。

§ 步驟 3:生成詳細測試案例

§ 範例:新增 TODO 功能的測試案例

Generate detailed test cases for the "Add TODO" functionality:

[Test Case Template]
- Test ID
- Test Name
- Description
- Preconditions
- Test Steps
- Expected Results
- Test Data
- Priority (Critical/High/Medium/Low)

[Scenarios to Cover]
1. Valid input scenarios
2. Invalid input handling
3. Boundary conditions
4. Concurrent operations
5. Storage limitations
6. UI responsiveness

請為每個場景生成至少 3 個具體的測試案例,使用繁體中文描述。

§ 步驟 4:E2E 測試場景設計

Design comprehensive E2E test scenarios that simulate real user journeys:

[User Journeys]
1. First-time user experience
2. Power user workflow
3. Mobile user interaction
4. Accessibility navigation
5. Error recovery flow

[E2E Test Scenario Structure]
- Scenario name
- User persona
- Initial state
- Action sequence
- Verification points
- Expected outcome

為每個使用者旅程創建詳細的 E2E 測試腳本,包含具體的操作步驟和驗證點。

§ 進階測試策略技巧

§ 1. 風險導向測試 (Risk-Based Testing)

教導 AI 識別和評估風險:

Perform risk analysis for the TODO application:

[Risk Categories]
1. Data Loss Risk
2. Security Vulnerabilities
3. Performance Bottlenecks
4. Usability Issues
5. Compatibility Problems

[Risk Assessment Matrix]
For each identified risk:
- Probability (High/Medium/Low)
- Impact (Critical/Major/Minor)
- Detection Difficulty
- Mitigation Strategy
- Test Coverage Required

創建風險矩陣並提供測試優先級建議。

§ 2. 測試資料策略

Design a comprehensive test data strategy:

[Data Categories]
1. Valid Data Sets
   - Typical user data
   - Boundary values
   - International characters

2. Invalid Data Sets
   - Malformed inputs
   - SQL injection attempts
   - XSS payloads

3. Performance Data Sets
   - Large volume data
   - Concurrent access patterns
   - Storage limit scenarios

4. Edge Case Data
   - Empty values
   - Special characters
   - Maximum length inputs

為每個類別提供具體的測試資料範例和使用場景。

§ 3. 測試覆蓋率分析

Analyze and recommend test coverage targets:

[Coverage Metrics]
1. Code Coverage
   - Line coverage: target 80%
   - Branch coverage: target 75%
   - Function coverage: target 90%

2. Functional Coverage
   - Feature coverage: 100%
   - User story coverage: 95%
   - Edge case coverage: 80%

3. Requirements Coverage
   - Business requirements: 100%
   - Technical requirements: 90%
   - Non-functional requirements: 85%

提供達成這些覆蓋率目標的具體策略。

§ 實戰案例:複雜功能的測試策略

§ 案例 1:批量操作測試

為批量操作功能制定測試策略:

Create a test strategy for bulk operations:

[Bulk Operations]
- Select multiple TODOs
- Bulk delete
- Bulk status update
- Bulk export/import

[Test Considerations]
1. Performance with large selections
2. Partial failure handling
3. Undo/Redo functionality
4. Progress indication
5. Concurrent user actions

設計完整的測試方案,包含效能基準和失敗恢復測試。

§ 案例 2:即時協作測試

為即時協作功能設計測試:

Design test strategy for real-time collaboration:

[Collaboration Features]
- Multiple users editing
- Conflict resolution
- Change synchronization
- Offline/Online transitions

[Test Scenarios]
1. Simultaneous edits
2. Network interruption
3. Data consistency
4. Race conditions
5. Session management

創建詳細的測試矩陣,涵蓋所有協作場景。

§ 案例 3:智慧功能測試

為 AI 輔助功能制定測試策略:

Develop test strategy for AI-powered features:

[AI Features]
- Smart categorization
- Due date prediction
- Task prioritization
- Natural language input

[Test Challenges]
1. Non-deterministic outputs
2. Model accuracy validation
3. Edge case handling
4. Performance benchmarks
5. Fallback mechanisms

設計能夠有效驗證 AI 功能的測試方法。

§ 測試計劃評估檢查表

§ 完整性檢查

  • [ ] 涵蓋所有主要功能
  • [ ] 包含正向和負向測試
  • [ ] 考慮邊界條件
  • [ ] 包含效能測試
  • [ ] 涵蓋安全測試

§ 可行性評估

  • [ ] 測試案例可執行
  • [ ] 測試資料可準備
  • [ ] 時間估算合理
  • [ ] 資源需求明確
  • [ ] 自動化可能性評估

§ 品質指標

  • [ ] 測試案例清晰明確
  • [ ] 預期結果可驗證
  • [ ] 優先級設定合理
  • [ ] 風險覆蓋充分
  • [ ] 可維護性考量

§ 常見問題與解決方案

§ Q1: AI 生成的測試案例太籠統怎麼辦?

解決方案: 提供更具體的上下文和範例:

Don't just say "test input validation"
Instead, specify:
- Test with empty string: ""
- Test with spaces only: "   "
- Test with special chars: "!@#$%"
- Test with Unicode: "測試🎉"
- Test with max length: 256 chars
- Test with script tags: "<script>alert('xss')</script>"

§ Q2: 如何確保測試策略的完整性?

解決方案: 使用結構化的檢查清單:

Ensure test strategy covers:
□ Functional Requirements
  - All user stories
  - Acceptance criteria
  - Business rules
□ Non-Functional Requirements
  - Performance (response < 2s)
  - Security (OWASP Top 10)
  - Accessibility (WCAG 2.1)
□ Integration Points
  - API endpoints
  - Database operations
  - Third-party services

§ Q3: 如何平衡測試覆蓋率和效率?

解決方案: 採用風險導向的優先級:

Prioritize tests based on:
1. Business Impact (Critical/High/Medium/Low)
2. Failure Probability (Likely/Possible/Unlikely)
3. User Frequency (Always/Often/Sometimes/Rarely)

Formula: Priority = Impact × Probability × Frequency

Focus on high-priority tests first.

§ 最佳實踐總結

§ 1. 測試策略原則

  • 全面性:覆蓋功能、效能、安全各面向
  • 可追溯性:每個測試連結到需求
  • 可執行性:測試步驟具體可操作
  • 可維護性:易於更新和擴展

§ 2. AI 提示詞技巧

  • 提供上下文:包含程式碼結構和業務邏輯
  • 明確期望:指定輸出格式和詳細程度
  • 迭代改進:根據結果調整提示詞
  • 範例引導:提供期望的測試案例範例

§ 3. 測試優先級框架

優先級 = 業務價值 × 失敗風險 × 使用頻率

P0 - 關鍵路徑,必須測試
P1 - 重要功能,應該測試  
P2 - 次要功能,建議測試
P3 - 邊緣案例,選擇測試

§ 思考與挑戰

§ 深度思考題

  1. 測試策略演進:如何讓測試策略隨著應用發展而演進?
  2. AI 的局限:哪些測試策略決策不適合交給 AI?
  3. 團隊協作:如何整合 AI 測試策略到現有團隊流程?
  4. ROI 分析:如何量化 AI 輔助測試的投資回報?

§ 進階挑戰

  1. 跨瀏覽器測試策略:設計支援多瀏覽器的測試方案
  2. 效能基準測試:建立效能測試基準和監控策略
  3. 安全測試整合:將安全測試納入常規測試流程
  4. 視覺回歸測試:設計 UI 視覺變化的測試策略

§ 實作練習:測試策略文檔

§ 練習目標

為你的 TODO 應用創建完整的測試策略文檔,包含:

  1. 執行摘要 (Executive Summary)
  2. 測試範圍 (Test Scope)
  3. 測試方法 (Test Approach)
  4. 測試案例 (Test Cases)
  5. 風險評估 (Risk Assessment)
  6. 資源需求 (Resource Requirements)
  7. 時程規劃 (Timeline)

§ 提交要求

  • 完整的測試策略文檔(Markdown 格式)
  • AI 互動記錄和提示詞歷史
  • 測試優先級矩陣
  • 風險評估報告

§ 下一步

恭喜你完成了第二樂章!你已經學會如何讓 AI 擔任測試策略師,制定全面的測試計劃。在下一章「第三樂章:AI 執行測試腳本編寫」中,我們將學習如何讓 AI 將這些測試策略轉化為可執行的 Playwright 測試腳本。

記住,優秀的測試策略是高品質軟體的基石。透過 AI 的幫助,我們可以更快速、更全面地識別測試需求,確保應用程式的可靠性。

§ 資源連結


「最好的測試策略不是找到所有 bug,而是找到最重要的 bug。」