Skip to content

测试策略

组合 layers, 比率, and policies 到测试策略那个 scales 使用你的 team.

你将学到

  • 设计 layered 策略: 单元, integration, E2E, and manual
  • 选择测试比率那个 match 你的项目's risk profile
  • Rolling out 测试 on legacy codebase incrementally
  • Measuring and evolving 你的策略 over 时间

概念讲解

A Layered Strategy

A testing strategy is a deliberate mix of test types. The testing pyramid (many unit, fewer integration, few E2E) is a starting point, but the right shape depends on your project. A UI-heavy app may look more like a testing trophy (many component tests). A distributed system may need more contract and integration tests. The strategy is not the pyramid shape; it is the conscious choice of where to invest, based on where your bugs actually come from.

Ratios and Risk

Allocate tests where the risk and the bug rate are highest. If most production incidents come from API integration, invest there. If UI regressions dominate, add component and visual regression tests. Track which test layer catches the most real bugs and shift investment toward it. A good strategy is evidence-based: measure where bugs escape, then strengthen the layer that should have caught them.

Legacy Rollout

你不能retrofit tests onto a legacy codebase all at once. Adopt the Boy Scout rule: every time you touch a file, add a test for the behavior you changed. Use characterization tests — tests that capture the current behavior (even if buggy) — to lock in a safety net before refactoring. Prioritize the most-changed, highest-risk modules. Over months, coverage climbs without a big-bang effort that stalls.

Evolve and Measure

A strategy is not set-and-forget. Track metrics: suite runtime, flake rate, coverage, bug escape rate, and time-to-first-test-failure. Review them quarterly. If the suite is slow, push tests down the pyramid. If bugs escape, strengthen the leaking layer. If flake is high, invest in isolation. Communicate the strategy to the team so everyone writes tests that fit the plan, and revisit it as the product and team grow.

In Practice

Document your testing strategy in a short TESTING.md that names the layers, the target ratios, and the tools — this aligns the team and onboards new hires. Track bug escape rate: when a bug reaches production, note which test layer should have caught it and add a regression test there. Over a quarter, the pattern of escapes tells you where to invest. If integration tests dominate the runtime, push logic down to pure functions and unit-test it. If E2E tests are flaky, invest in test-data isolation and auto-waiting. Share the metrics with the team so everyone sees the trend — a suite that is getting slower or flakier is a shared problem. The best strategy evolves with the product: what worked at ten engineers may not work at fifty, so revisit the plan when the team or architecture changes meaningfully.

示例代码

              // A characterization test for legacy code: capture current behavior
// before refactoring, so the safety net exists even if the behavior is odd.
import { describe, it, expect } from "vitest";
import { legacyDiscount } from "./legacyPricing";

describe("legacyDiscount (characterization)", () => {
  // These expected values are whatever the code does TODAY.
  // They lock in behavior so refactors do not change it silently.
  it.each([
    [100, "regular", 100],
    [100, "vip", 90],
    [100, "vip", 90], // captured even if the rule is surprising
  ])("legacyDiscount(%i, %s) === %i", (price, tier, expected) => {
    expect(legacyDiscount(price, tier)).toBe(expected);
  });
});

// Next: refactor legacyDiscount with this safety net in place,
// then add new tests for the desired (fixed) behavior.
            

Characterization 测试 pin 下 current behavior — 偶数 bugs — 所以你 能 refactor 使用 confidence. 一次 safety net exists, 你添加测试 for corrected behavior and fix code.

动手试一试

  • CI YAML Generator

    Wire 你的 layered 策略到 CI 使用 sharded, gated 作业.

  • Code Diff

    Review characterization 测试 diffs as 你 refactor legacy code.

常见错误

错误写法

复制 another 项目's pyramid 不使用 analyzing 你的 bugs

正确做法

Measure 哪里你的 bugs come 从 and invest 那里. 正确 shape depends on 你的架构 and risk profile, 不 textbook diagram.

错误写法

Trying 到 retrofit 完整 coverage on legacy in one 推送

正确做法

Use Boy Scout 规则: 添加测试 for 每个 file 你 touch. Coverage climbs steadily 不使用 stalled 大-bang effort.

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误