Loading...
Loading...
Compare original and translation side by side
undefinedundefinedundefinedundefined| Stack | Unit | Integration | E2E |
|---|---|---|---|
| Node.js/TS | Vitest | Vitest + Supertest | Playwright |
| React/Next.js | Vitest + Testing Library | Vitest + MSW | Playwright/Cypress |
| Python | pytest | pytest + httpx | Playwright |
| Go | testing + testify | testing + testcontainers | Playwright |
| Rust | cargo test | cargo test + testcontainers | - |
| PHP/Laravel | Pest/PHPUnit | Pest + HTTP tests | Playwright/Dusk |
| 技术栈 | 单元测试 | 集成测试 | E2E测试 |
|---|---|---|---|
| Node.js/TS | Vitest | Vitest + Supertest | Playwright |
| React/Next.js | Vitest + Testing Library | Vitest + MSW | Playwright/Cypress |
| Python | pytest | pytest + httpx | Playwright |
| Go | testing + testify | testing + testcontainers | Playwright |
| Rust | cargo test | cargo test + testcontainers | - |
| PHP/Laravel | Pest/PHPUnit | Pest + HTTP tests | Playwright/Dusk |
/\
/ \ 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%)
/ \ 纯函数、业务逻辑、工具方法| Level | Test These | Do NOT Test These |
|---|---|---|
| Unit (60%) | Pure functions, business logic, data transformations, validations, state management | Framework internals, third-party libraries |
| Integration (30%) | API endpoints, database queries, service-to-service calls, auth flows | Individual functions in isolation |
| E2E (10%) | Critical user journeys (signup, purchase), cross-browser, accessibility | Edge cases (handle at unit level) |
| 层级 | 测试内容 | 不测试内容 |
|---|---|---|
| 单元测试 (60%) | 纯函数、业务逻辑、数据转换、校验、状态管理 | 框架内部实现、第三方库 |
| 集成测试 (30%) | API端点、数据库查询、服务间调用、鉴权流程 | 独立运行的单个函数 |
| E2E测试 (10%) | 核心用户旅程(注册、支付)、跨浏览器兼容性、可访问性 | 边界场景(放在单元测试层覆盖) |
| Category | Minimum | Target | Notes |
|---|---|---|---|
| Overall | 70% | 85% | Lines covered |
| Critical paths | 90% | 95% | Auth, payments, data access |
| New code (PRs) | 80% | 90% | Enforced in CI |
| Utilities | 95% | 100% | Pure functions are easy to test |
| 类别 | 最低要求 | 目标值 | 说明 |
|---|---|---|---|
| 整体 | 70% | 85% | 代码行覆盖率 |
| 核心路径 | 90% | 95% | 鉴权、支付、数据访问 |
| 新代码(PR提交) | 80% | 90% | 在CI中强制校验 |
| 工具方法 | 95% | 100% | 纯函数易测试 |
| Project Maturity | Overall Minimum | New Code Minimum | Rationale |
|---|---|---|---|
| Greenfield | 80% | 90% | Start high, maintain standard |
| Active (good coverage) | 70% | 85% | Maintain and improve |
| Legacy (low coverage) | 50% | 80% | Raise floor gradually |
| Prototype/MVP | 60% | 70% | Cover critical paths, accept gaps |
| 项目成熟度 | 整体最低覆盖率 | 新代码最低覆盖率 | 逻辑 |
|---|---|---|---|
| 全新项目 | 80% | 90% | 高起点起步,保持标准 |
| 活跃迭代(覆盖率良好) | 70% | 85% | 保持并逐步提升 |
| 遗留项目(覆盖率低) | 50% | 80% | 逐步提升最低要求 |
| 原型/MVP | 60% | 70% | 覆盖核心路径,允许存在缺口 |
vitest.config.tsjest.config.jspytest.ini.env.testvitest.config.tsjest.config.jspytest.ini.env.testimport { 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}'],
},
});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}'],
},
});| Anti-Pattern | Why It Is Wrong | Correct Approach |
|---|---|---|
| Testing implementation details | Breaks on every refactor, provides false confidence | Test behavior and outcomes |
| Excessive mocking | Tests nothing real, mocks mask real failures | Mock at boundaries only |
| Brittle CSS selectors in E2E | Break with styling changes | Use data-testid or accessible roles |
| Test interdependence | Ordering failures, flaky in CI | Each test must run independently |
| Slow tests blocking CI | Developers skip running tests | Parallelize, use test databases, mock external APIs |
| Snapshot overuse | Snapshots approved without reading, stale baselines | Use for stable output only |
| No coverage enforcement in CI | Coverage degrades over time | Enforce thresholds in CI pipeline |
| Same coverage target everywhere | Utilities and critical paths differ | Use per-category thresholds |
| 反模式 | 错误原因 | 正确做法 |
|---|---|---|
| 测试实现细节 | 每次重构都会导致测试失败,提供虚假的信心 | 测试行为和结果 |
| 过度mock | 没有测试真实逻辑,mock会掩盖真实故障 | 仅在边界层做mock |
| E2E测试中使用脆弱的CSS选择器 | 样式修改就会导致测试失败 | 使用data-testid或可访问性角色 |
| 测试相互依赖 | 会出现顺序相关的失败,在CI中表现不稳定 | 每个测试都要能独立运行 |
| 测试运行慢阻塞CI | 开发者会跳过运行测试 | 并行执行、使用测试数据库、mock外部API |
| 滥用快照 | 快照不经审核就被批准,基线过时 | 仅用于稳定的输出场景 |
| CI中没有强制校验覆盖率 | 覆盖率会随时间逐步降低 | 在CI流水线中强制校验阈值 |
| 所有模块使用相同的覆盖率目标 | 工具方法和核心路径的要求存在差异 | 按分类设置不同阈值 |
| Dependency Type | Mock Strategy | Example |
|---|---|---|
| External API | MSW / nock / responses | Third-party payment API |
| Database | Test database or in-memory | PostgreSQL test container |
| File system | Virtual FS or temp directory | File upload processing |
| Time/Date | Fake timers | Expiration logic |
| Environment vars | Override in test setup | Feature flags |
| Random/UUID | Seed or stub | ID generation |
| 依赖类型 | Mock策略 | 示例 |
|---|---|---|
| 外部API | MSW / nock / responses | 第三方支付API |
| 数据库 | 测试数据库或内存数据库 | PostgreSQL测试容器 |
| 文件系统 | 虚拟FS或临时目录 | 文件上传处理 |
| 时间/日期 | 假计时器 | 过期逻辑 |
| 环境变量 | 在测试启动时覆盖 | 功能开关 |
| 随机数/UUID | 固定种子或stub | ID生成 |
| Skill | Relationship |
|---|---|
| Strategy defines frameworks; TDD defines the cycle |
| Strategy includes acceptance test infrastructure |
| Review checks that tests follow the defined strategy |
| Frontend testing uses strategy-selected frameworks |
| Backend testing uses strategy-selected frameworks |
| Load tests are part of the overall testing strategy |
| Playwright E2E tests follow strategy pyramid |
| 技能 | 关联关系 |
|---|---|
| 测试策略定义框架,TDD定义开发周期 |
| 测试策略包含验收测试基础设施 |
| 代码评审检查测试是否符合定义的策略 |
| 前端测试使用策略选定的框架 |
| 后端测试使用策略选定的框架 |
| 压力测试属于整体测试策略的一部分 |
| Playwright E2E测试遵循策略的金字塔结构 |