shift-left-testing

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Shift-Left Testing

左移测试

<default_to_action> When implementing early testing practices:
  1. VALIDATE requirements before coding (testability, BDD scenarios)
  2. WRITE tests before implementation (TDD red-green-refactor)
  3. AUTOMATE in CI pipeline (every commit triggers tests)
  4. FOLLOW test pyramid: Many unit (70%), some integration (20%), few E2E (10%)
  5. FIX defects immediately - never let them accumulate
Quick Shift-Left Levels:
  • Level 1: Unit tests with each PR (developer responsibility)
  • Level 2: TDD practice (tests before code)
  • Level 3: BDD/Example mapping in refinement (requirements testing)
  • Level 4: Risk analysis in design (architecture testing)
Critical Success Factors:
  • Defects found in requirements cost 1x; in production cost 100x
  • Every commit must run automated tests
  • Quality is built in, not tested in </default_to_action>
<default_to_action> 在实施早期测试实践时:
  1. 在编码前验证需求(可测试性、BDD场景)
  2. 在实现前编写测试(TDD红-绿-重构流程)
  3. 在CI流水线中实现自动化(每次提交触发测试)
  4. 遵循测试金字塔:大量单元测试(70%)、部分集成测试(20%)、少量端到端测试(10%)
  5. 立即修复缺陷——绝不让缺陷累积
快速了解左移测试层级:
  • 层级1:每个PR附带单元测试(开发者负责)
  • 层级2:践行TDD实践(先写测试再编码)
  • 层级3:在需求细化阶段使用BDD/示例映射(需求测试)
  • 层级4:在设计阶段进行风险分析(架构测试)
关键成功因素:
  • 需求阶段发现的缺陷修复成本为1倍;生产环境发现的缺陷修复成本为100倍
  • 每次提交必须运行自动化测试
  • 质量是内置的,而非事后测试出来的 </default_to_action>

Quick Reference Card

快速参考卡片

When to Use

适用场景

  • Reducing cost of defects
  • Implementing CI/CD pipelines
  • Starting TDD practice
  • Improving requirements quality
  • 降低缺陷修复成本
  • 实施CI/CD流水线
  • 开始践行TDD实践
  • 提升需求质量

Cost of Defects by Phase

各阶段缺陷修复成本

PhaseRelative CostExample
Requirements1xFix doc: 30 min
Design5xRedesign: few hours
Development10xCode fix: 1 day
Testing20xFix + retest: 2 days
Production100xHotfix + rollback + investigation
阶段相对成本示例
需求阶段1x修复文档:30分钟
设计阶段5x重新设计:数小时
开发阶段10x代码修复:1天
测试阶段20x修复+重新测试:2天
生产环境100x热修复+回滚+问题调查

Test Pyramid

测试金字塔

       /\        E2E (10%) - Critical user journeys
      /  \
     /    \      Integration (20%) - Component interactions
    /      \
   /________\    Unit (70%) - Fast, isolated, comprehensive
       /\        E2E (10%) - 关键用户流程
      /  \
     /    \      集成测试 (20%) - 组件交互
    /      \
   /________\    单元测试 (70%) - 快速、隔离、全面

Shift-Left Levels

左移测试层级

LevelPracticeWhen
1Unit tests in PRBefore merge
2TDDBefore implementation
3BDD/Example MappingDuring refinement
4Risk AnalysisDuring design

层级实践内容实施时机
1PR中包含单元测试合并前
2TDD实践代码实现前
3BDD/示例映射需求细化阶段
4风险分析设计阶段

Level 1: Tests in Every PR

层级1:每个PR都包含测试

yaml
undefined
yaml
undefined

CI pipeline - tests run on every commit

CI pipeline - tests run on every commit

name: CI on: [push, pull_request] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: npm ci - run: npm run test:unit - run: npm run test:integration - run: npm run lint
quality-gate: needs: test steps: - name: Coverage Check run: npx coverage-check --min 80 - name: No New Warnings run: npm run lint -- --max-warnings 0

---
name: CI on: [push, pull_request] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: npm ci - run: npm run test:unit - run: npm run test:integration - run: npm run lint
quality-gate: needs: test steps: - name: Coverage Check run: npx coverage-check --min 80 - name: No New Warnings run: npm run lint -- --max-warnings 0

---

Level 2: TDD Practice

层级2:TDD实践

javascript
// Red: Write failing test first
test('calculates discount for orders over $100', () => {
  const order = new Order([{ price: 150 }]);
  expect(order.discount).toBe(15); // 10% off
});

// Green: Minimal implementation
class Order {
  get discount() {
    return this.total > 100 ? this.total * 0.1 : 0;
  }
}

// Refactor: Improve while keeping green

javascript
// Red: Write failing test first
test('calculates discount for orders over $100', () => {
  const order = new Order([{ price: 150 }]);
  expect(order.discount).toBe(15); // 10% off
});

// Green: Minimal implementation
class Order {
  get discount() {
    return this.total > 100 ? this.total * 0.1 : 0;
  }
}

// Refactor: Improve while keeping green

Level 3: BDD in Refinement

层级3:需求细化阶段的BDD实践

gherkin
undefined
gherkin
undefined

Example mapping before coding

Example mapping before coding

Feature: Loyalty Discount Scenario: Gold member gets 15% discount Given a customer with "gold" membership When they checkout with $200 in cart Then discount applied is $30 And order total is $170
Scenario: New customer gets no discount Given a customer with no membership When they checkout with $200 in cart Then no discount is applied

---
Feature: Loyalty Discount Scenario: Gold member gets 15% discount Given a customer with "gold" membership When they checkout with $200 in cart Then discount applied is $30 And order total is $170
Scenario: New customer gets no discount Given a customer with no membership When they checkout with $200 in cart Then no discount is applied

---

Level 4: Risk Analysis in Design

层级4:设计阶段的风险分析

typescript
// During architecture review
await Task("Risk Analysis", {
  phase: 'design',
  component: 'payment-service',
  questions: [
    'What happens when payment gateway times out?',
    'How do we handle duplicate submissions?',
    'What if inventory changed during checkout?'
  ]
}, "qe-requirements-validator");

// Output: Testability requirements, failure mode tests

typescript
// During architecture review
await Task("Risk Analysis", {
  phase: 'design',
  component: 'payment-service',
  questions: [
    'What happens when payment gateway times out?',
    'How do we handle duplicate submissions?',
    'What if inventory changed during checkout?'
  ]
}, "qe-requirements-validator");

// Output: Testability requirements, failure mode tests

Agent-Assisted Shift-Left

Agent辅助的左移测试

typescript
// Validate requirements testability
await Task("Requirements Validation", {
  requirements: userStories,
  check: ['INVEST-criteria', 'testability', 'ambiguity'],
  generateBDD: true
}, "qe-requirements-validator");

// Generate tests from requirements
await Task("Generate Tests", {
  source: 'requirements',
  types: ['unit', 'integration', 'e2e'],
  coverage: 'comprehensive'
}, "qe-test-generator");

// Smart test selection for changes
await Task("Select Regression Tests", {
  changedFiles: prFiles,
  algorithm: 'risk-based',
  targetReduction: 0.7  // 70% time savings
}, "qe-regression-risk-analyzer");

typescript
// Validate requirements testability
await Task("Requirements Validation", {
  requirements: userStories,
  check: ['INVEST-criteria', 'testability', 'ambiguity'],
  generateBDD: true
}, "qe-requirements-validator");

// Generate tests from requirements
await Task("Generate Tests", {
  source: 'requirements',
  types: ['unit', 'integration', 'e2e'],
  coverage: 'comprehensive'
}, "qe-test-generator");

// Smart test selection for changes
await Task("Select Regression Tests", {
  changedFiles: prFiles,
  algorithm: 'risk-based',
  targetReduction: 0.7  // 70% time savings
}, "qe-regression-risk-analyzer");

Agent Coordination Hints

Agent协作提示

Memory Namespace

内存命名空间

aqe/shift-left/
├── requirements/*       - Validated requirements
├── generated-tests/*    - Auto-generated tests
├── coverage-targets/*   - Coverage goals by component
└── pipeline-results/*   - CI/CD test history
aqe/shift-left/
├── requirements/*       - Validated requirements
├── generated-tests/*    - Auto-generated tests
├── coverage-targets/*   - Coverage goals by component
└── pipeline-results/*   - CI/CD test history

Fleet Coordination

集群协作

typescript
const shiftLeftFleet = await FleetManager.coordinate({
  strategy: 'shift-left',
  agents: [
    'qe-requirements-validator',     // Level 3-4
    'qe-test-generator',             // Level 2
    'qe-regression-risk-analyzer'    // Smart selection
  ],
  topology: 'sequential'
});

typescript
const shiftLeftFleet = await FleetManager.coordinate({
  strategy: 'shift-left',
  agents: [
    'qe-requirements-validator',     // Level 3-4
    'qe-test-generator',             // Level 2
    'qe-regression-risk-analyzer'    // Smart selection
  ],
  topology: 'sequential'
});

Related Skills

相关技能

  • tdd-london-chicago - TDD practices
  • holistic-testing-pact - Proactive testing
  • cicd-pipeline-qe-orchestrator - Pipeline integration
  • shift-right-testing - Production feedback

  • tdd-london-chicago - TDD practices
  • holistic-testing-pact - Proactive testing
  • cicd-pipeline-qe-orchestrator - Pipeline integration
  • shift-right-testing - Production feedback

Remember

要点回顾

Earlier = Cheaper. Requirements defects cost 1x; production defects cost 100x. Test pyramid: 70% unit, 20% integration, 10% E2E. Every commit runs tests. TDD builds quality in.
With Agents: Agents validate requirements testability, generate tests from specs, and select optimal regression suites. Use agents to implement shift-left practices consistently.
越早发现,成本越低。 需求阶段的缺陷修复成本为1倍;生产环境的缺陷修复成本为100倍。测试金字塔:70%单元测试、20%集成测试、10%E2E测试。每次提交都要运行测试。TDD将质量内置到开发过程中。
借助Agent: Agent可验证需求的可测试性、根据需求生成测试、选择最优回归测试套件。使用Agent可确保左移测试实践的一致性落地。