testing-strategy

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Testing Strategy

测试策略

Overview

概述

Analyze the project context and recommend a comprehensive testing strategy. This skill selects appropriate frameworks, defines the testing pyramid, establishes coverage thresholds, and generates test configuration files. The goal is a repeatable, measurable testing foundation that the team can maintain.
Announce at start: "I'm using the testing-strategy skill to define the testing approach."

分析项目上下文并推荐一套完整的测试策略。本技能会选择合适的框架、定义测试金字塔、设置覆盖率阈值,并生成测试配置文件。目标是搭建可重复、可衡量的测试基础,供团队长期维护。
启动时声明: "我将使用testing-strategy技能来定义测试方案。"

Phase 1: Analyze Project

第1阶段:分析项目

Goal: Understand the current stack, existing tests, and CI setup before recommending anything.
目标: 在给出任何推荐前,先了解当前技术栈、已有测试和CI配置情况。

Actions

执行动作

  1. Identify the tech stack (language, framework, runtime)
  2. Survey existing tests (what testing exists already?)
  3. Review CI/CD pipeline (how do tests run?)
  4. Measure current coverage levels
  5. Map external dependencies (services, databases, APIs)
  1. 确定技术栈(编程语言、框架、运行时)
  2. 调研已有测试(当前已经有哪些测试?)
  3. 梳理CI/CD流水线(测试的运行方式是什么?)
  4. 衡量当前覆盖率水平
  5. 梳理外部依赖(服务、数据库、API)

Discovery Commands

调研命令

bash
undefined
bash
undefined

Identify test files

识别测试文件

find . -name ".test." -o -name ".spec." | head -30
find . -name ".test." -o -name ".spec." | head -30

Check for test config

检查测试配置

ls vitest.config.* jest.config.* pytest.ini pyproject.toml .mocharc.* 2>/dev/null
ls vitest.config.* jest.config.* pytest.ini pyproject.toml .mocharc.* 2>/dev/null

Check current coverage

查看当前覆盖率

cat coverage/coverage-summary.json 2>/dev/null || echo "No coverage report found"
cat coverage/coverage-summary.json 2>/dev/null || echo "未找到覆盖率报告"

Check CI config

检查CI配置

cat .github/workflows/*.yml 2>/dev/null | head -50
undefined
cat .github/workflows/*.yml 2>/dev/null | head -50
undefined

STOP — Do NOT proceed to Phase 2 until:

停止 — 未满足以下条件请勿进入第2阶段:

  • Tech stack is identified
  • Existing test infrastructure is mapped
  • CI pipeline status is known
  • External dependencies are listed

  • 已确定技术栈
  • 已梳理现有测试基础设施
  • 已明确CI流水线状态
  • 已列出所有外部依赖

Phase 2: Recommend Testing Pyramid

第2阶段:推荐测试金字塔

Goal: Select frameworks and define the pyramid ratios.
目标: 选择测试框架并定义金字塔各层占比。

Framework Selection Table

框架选择表

StackUnitIntegrationE2E
Node.js/TSVitestVitest + SupertestPlaywright
React/Next.jsVitest + Testing LibraryVitest + MSWPlaywright/Cypress
Pythonpytestpytest + httpxPlaywright
Gotesting + testifytesting + testcontainersPlaywright
Rustcargo testcargo test + testcontainers-
PHP/LaravelPest/PHPUnitPest + HTTP testsPlaywright/Dusk
技术栈单元测试集成测试E2E测试
Node.js/TSVitestVitest + SupertestPlaywright
React/Next.jsVitest + Testing LibraryVitest + MSWPlaywright/Cypress
Pythonpytestpytest + httpxPlaywright
Gotesting + testifytesting + testcontainersPlaywright
Rustcargo testcargo test + testcontainers-
PHP/LaravelPest/PHPUnitPest + HTTP testsPlaywright/Dusk

Testing Pyramid Ratios

测试金字塔占比

        /\
       /  \     E2E Tests (10%)
      /    \    Critical user journeys only
     /------\
    /        \   Integration Tests (30%)
   /          \  API endpoints, DB queries, service interactions
  /------------\
 /              \ Unit Tests (60%)
/                \ Pure functions, business logic, utilities
        /\
       /  \     E2E测试 (10%)
      /    \    仅覆盖核心用户旅程
     /------\
    /        \   集成测试 (30%)
   /          \  API端点、数据库查询、服务交互
  /------------\
 /              \ 单元测试 (60%)
/                \ 纯函数、业务逻辑、工具方法

What to Test at Each Level

各层测试范围

LevelTest TheseDo NOT Test These
Unit (60%)Pure functions, business logic, data transformations, validations, state managementFramework internals, third-party libraries
Integration (30%)API endpoints, database queries, service-to-service calls, auth flowsIndividual functions in isolation
E2E (10%)Critical user journeys (signup, purchase), cross-browser, accessibilityEdge cases (handle at unit level)
层级测试内容不测试内容
单元测试 (60%)纯函数、业务逻辑、数据转换、校验、状态管理框架内部实现、第三方库
集成测试 (30%)API端点、数据库查询、服务间调用、鉴权流程独立运行的单个函数
E2E测试 (10%)核心用户旅程(注册、支付)、跨浏览器兼容性、可访问性边界场景(放在单元测试层覆盖)

STOP — Do NOT proceed to Phase 3 until:

停止 — 未满足以下条件请勿进入第3阶段:

  • Framework selection matches the tech stack
  • Pyramid ratios are defined
  • Testing scope at each level is documented

  • 选择的框架匹配技术栈
  • 已定义金字塔各层占比
  • 已记录各层级的测试范围

Phase 3: Define Coverage Thresholds

第3阶段:定义覆盖率阈值

Goal: Set realistic, enforceable coverage targets.
目标: 设置符合实际、可强制执行的覆盖率目标。

Coverage Threshold Table

覆盖率阈值表

CategoryMinimumTargetNotes
Overall70%85%Lines covered
Critical paths90%95%Auth, payments, data access
New code (PRs)80%90%Enforced in CI
Utilities95%100%Pure functions are easy to test
类别最低要求目标值说明
整体70%85%代码行覆盖率
核心路径90%95%鉴权、支付、数据访问
新代码(PR提交)80%90%在CI中强制校验
工具方法95%100%纯函数易测试

Threshold Selection Decision Table

阈值选择决策表

Project MaturityOverall MinimumNew Code MinimumRationale
Greenfield80%90%Start high, maintain standard
Active (good coverage)70%85%Maintain and improve
Legacy (low coverage)50%80%Raise floor gradually
Prototype/MVP60%70%Cover critical paths, accept gaps
项目成熟度整体最低覆盖率新代码最低覆盖率逻辑
全新项目80%90%高起点起步,保持标准
活跃迭代(覆盖率良好)70%85%保持并逐步提升
遗留项目(覆盖率低)50%80%逐步提升最低要求
原型/MVP60%70%覆盖核心路径,允许存在缺口

STOP — Do NOT proceed to Phase 4 until:

停止 — 未满足以下条件请勿进入第4阶段:

  • Coverage thresholds are realistic for the project maturity
  • Critical path coverage targets are defined
  • CI enforcement strategy is decided

  • 覆盖率阈值符合项目成熟度的实际情况
  • 已定义核心路径的覆盖率目标
  • 已确定CI强制校验策略

Phase 4: Generate Configuration

第4阶段:生成配置文件

Goal: Produce working test configuration files and CI integration.
目标: 输出可运行的测试配置文件和CI集成配置。

Actions

执行动作

  1. Generate test runner config (
    vitest.config.ts
    ,
    jest.config.js
    ,
    pytest.ini
    )
  2. Configure coverage with thresholds
  3. Add test commands to CI workflow
  4. Set up test environment (
    .env.test
    , test databases)
  1. 生成测试运行器配置(
    vitest.config.ts
    jest.config.js
    pytest.ini
  2. 配置带阈值的覆盖率规则
  3. 在CI工作流中添加测试命令
  4. 搭建测试环境(
    .env.test
    、测试数据库)

Example: Vitest Config

示例:Vitest配置

typescript
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    globals: true,
    environment: 'jsdom',
    coverage: {
      provider: 'v8',
      reporter: ['text', 'json', 'html'],
      thresholds: {
        lines: 80,
        functions: 80,
        branches: 80,
        statements: 80,
      },
    },
    include: ['src/**/*.test.{ts,tsx}'],
  },
});
typescript
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    globals: true,
    environment: 'jsdom',
    coverage: {
      provider: 'v8',
      reporter: ['text', 'json', 'html'],
      thresholds: {
        lines: 80,
        functions: 80,
        branches: 80,
        statements: 80,
      },
    },
    include: ['src/**/*.test.{ts,tsx}'],
  },
});

STOP — Do NOT proceed to Phase 5 until:

停止 — 未满足以下条件请勿进入第5阶段:

  • Config files are syntactically valid
  • Coverage thresholds match Phase 3 decisions
  • CI integration commands are defined

  • 配置文件语法正确
  • 覆盖率阈值和第3阶段的决策一致
  • 已定义CI集成命令

Phase 5: Create Test Templates

第5阶段:创建测试模板

Goal: Provide example test files demonstrating project conventions.
目标: 提供示例测试文件,展示项目的测试规范。

Actions

执行动作

  1. Create a unit test example with Arrange-Act-Assert
  2. Create an integration test with setup/teardown
  3. Create mock/stub patterns for external dependencies
  4. Create test data factories/fixtures
  5. Create a snapshot test example (when appropriate)
  1. 创建遵循Arrange-Act-Assert规范的单元测试示例
  2. 创建包含setup/teardown的集成测试示例
  3. 提供外部依赖的mock/stub模式示例
  4. 创建测试数据工厂/夹具示例
  5. (适用时)创建快照测试示例

STOP — Verification Gate before claiming complete:

停止 — 标记完成前的验证关卡:

  • Framework selection matches tech stack
  • Coverage thresholds are realistic
  • Test configuration files are valid
  • Example tests actually run
  • CI integration is configured

  • 选择的框架匹配技术栈
  • 覆盖率阈值符合实际情况
  • 测试配置文件有效
  • 示例测试可正常运行
  • 已配置CI集成

Anti-Patterns / Common Mistakes

反模式/常见错误

Anti-PatternWhy It Is WrongCorrect Approach
Testing implementation detailsBreaks on every refactor, provides false confidenceTest behavior and outcomes
Excessive mockingTests nothing real, mocks mask real failuresMock at boundaries only
Brittle CSS selectors in E2EBreak with styling changesUse data-testid or accessible roles
Test interdependenceOrdering failures, flaky in CIEach test must run independently
Slow tests blocking CIDevelopers skip running testsParallelize, use test databases, mock external APIs
Snapshot overuseSnapshots approved without reading, stale baselinesUse for stable output only
No coverage enforcement in CICoverage degrades over timeEnforce thresholds in CI pipeline
Same coverage target everywhereUtilities and critical paths differUse per-category thresholds

反模式错误原因正确做法
测试实现细节每次重构都会导致测试失败,提供虚假的信心测试行为和结果
过度mock没有测试真实逻辑,mock会掩盖真实故障仅在边界层做mock
E2E测试中使用脆弱的CSS选择器样式修改就会导致测试失败使用data-testid或可访问性角色
测试相互依赖会出现顺序相关的失败,在CI中表现不稳定每个测试都要能独立运行
测试运行慢阻塞CI开发者会跳过运行测试并行执行、使用测试数据库、mock外部API
滥用快照快照不经审核就被批准,基线过时仅用于稳定的输出场景
CI中没有强制校验覆盖率覆盖率会随时间逐步降低在CI流水线中强制校验阈值
所有模块使用相同的覆盖率目标工具方法和核心路径的要求存在差异按分类设置不同阈值

Decision Table: Mock Strategy

决策表:Mock策略

Dependency TypeMock StrategyExample
External APIMSW / nock / responsesThird-party payment API
DatabaseTest database or in-memoryPostgreSQL test container
File systemVirtual FS or temp directoryFile upload processing
Time/DateFake timersExpiration logic
Environment varsOverride in test setupFeature flags
Random/UUIDSeed or stubID generation

依赖类型Mock策略示例
外部APIMSW / nock / responses第三方支付API
数据库测试数据库或内存数据库PostgreSQL测试容器
文件系统虚拟FS或临时目录文件上传处理
时间/日期假计时器过期逻辑
环境变量在测试启动时覆盖功能开关
随机数/UUID固定种子或stubID生成

Integration Points

集成点

SkillRelationship
test-driven-development
Strategy defines frameworks; TDD defines the cycle
acceptance-testing
Strategy includes acceptance test infrastructure
code-review
Review checks that tests follow the defined strategy
senior-frontend
Frontend testing uses strategy-selected frameworks
senior-backend
Backend testing uses strategy-selected frameworks
performance-optimization
Load tests are part of the overall testing strategy
webapp-testing
Playwright E2E tests follow strategy pyramid

技能关联关系
test-driven-development
测试策略定义框架,TDD定义开发周期
acceptance-testing
测试策略包含验收测试基础设施
code-review
代码评审检查测试是否符合定义的策略
senior-frontend
前端测试使用策略选定的框架
senior-backend
后端测试使用策略选定的框架
performance-optimization
压力测试属于整体测试策略的一部分
webapp-testing
Playwright E2E测试遵循策略的金字塔结构

Key Principles

核心原则

  • Test behavior, not implementation — what it does, not how
  • Fast feedback — unit tests should run in seconds
  • Deterministic — no flaky tests, no time-dependent logic
  • Readable — tests are documentation; make them clear
  • Maintainable — tests should help refactoring, not block it

  • 测试行为,而非实现 — 关注做什么,而非怎么做
  • 快速反馈 — 单元测试应当在数秒内运行完成
  • 确定性 — 不存在不稳定的测试,不存在时间依赖逻辑
  • 可读性 — 测试就是文档,要清晰易懂
  • 可维护性 — 测试应当助力重构,而非阻碍重构

Skill Type

技能类型

FLEXIBLE — Adapt framework selection and coverage thresholds to the project context. The five-phase process and testing pyramid structure are strongly recommended but can be scaled to project size.
灵活适配型 — 可以根据项目上下文调整框架选择和覆盖率阈值。强烈建议遵循五阶段流程和测试金字塔结构,但可根据项目规模做适配。