testing-strategy

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Testing Strategy

测试策略

When to Use

适用场景

When the user asks "should I test this?", "how should I test this?", "what tests do I need?", or is writing new functionality and needs testing guidance.
当用户询问「我应该测试这个吗?」、「我该怎么测试这个?」、「我需要哪些测试?」,或是在开发新功能需要测试指导时使用。

Instructions

使用指南

  1. Identify the type of code being tested:
    • Pure logic (calculations, transformations) → Unit tests
    • Integration points (database, API, file I/O) → Integration tests
    • User workflows (end-to-end paths) → E2E tests
  2. Apply the testing pyramid:
    • Many unit tests (fast, isolated, cheap)
    • Fewer integration tests (slower, test boundaries)
    • Minimal E2E tests (slowest, test critical paths only)
  3. Test naming convention:
    test_<function>_<scenario>_<expected_result>
  4. Each test should have: Arrange, Act, Assert (AAA pattern)
  5. Prioritize testing:
    • Error paths and edge cases over happy paths
    • Public API over internal implementation
    • Complex logic over trivial getters/setters
  6. Coverage goal: aim for 80% on business logic, don't chase 100% everywhere
  1. 确定待测试代码的类型:
    • 纯逻辑(计算、转换)→ 单元测试
    • 集成点(数据库、API、文件I/O)→ 集成测试
    • 用户工作流(端到端路径)→ E2E测试
  2. 遵循测试金字塔原则:
    • 大量单元测试(速度快、隔离性好、成本低)
    • 较少的集成测试(速度较慢,测试边界交互)
    • 最少的E2E测试(速度最慢,仅测试核心关键路径)
  3. 测试命名规范:
    test_<function>_<scenario>_<expected_result>
  4. 每个测试都应当遵循:Arrange(准备)、Act(执行)、Assert(断言)(AAA模式)
  5. 测试优先级:
    • 错误路径和边界用例优先于正常路径
    • 公共API优先于内部实现
    • 复杂逻辑优先于简单的getter/setter方法
  6. 覆盖率目标:业务逻辑覆盖率争取达到80%,不要盲目追求全量100%覆盖率

Examples

示例

User: "I added a new
calculate_discount()
function, what tests do I need?" Agent:
  • Unit test:
    test_calculate_discount_percentage_applied_correctly
  • Unit test:
    test_calculate_discount_zero_amount_returns_zero
  • Unit test:
    test_calculate_discount_negative_amount_returns_error
  • Unit test:
    test_calculate_discount_exceeds_max_caps_at_limit
用户:「我新增了一个
calculate_discount()
函数,我需要写哪些测试?」 Agent:
  • 单元测试:
    test_calculate_discount_percentage_applied_correctly
  • 单元测试:
    test_calculate_discount_zero_amount_returns_zero
  • 单元测试:
    test_calculate_discount_negative_amount_returns_error
  • 单元测试:
    test_calculate_discount_exceeds_max_caps_at_limit