Skip to content

测试 Overview

什么 software 测试是, 为什么它 matters, and 如何它 keeps 你的 code reliable and maintainable.

你将学到

  • 什么 software 测试平均值 and 为什么每个 developer 必须做 它
  • cost 的 bugs and 如何测试 reduces risk early
  • 测试 pyramid: 单元, integration, and 端-到-端测试
  • 如何测试 fits 到 development workflow

概念讲解

What Is Software Testing?

Software testing is the practice of verifying that your code behaves the way you intend. A test is a small, automated program that runs a piece of your real code, feeds it inputs, and checks that the outputs match your expectations. When a test passes, you gain confidence that the code works; when it fails, you get a precise signal that something is broken and where.

Why Testing Matters

Bugs are cheapest to fix when they are caught immediately by the developer who wrote the code. The later a bug is discovered — in code review, in QA, in production, or by a paying customer — the more expensive it becomes, sometimes by orders of magnitude. Testing gives you a fast feedback loop: you run a command and within seconds know whether your change is safe. Tests also act as living documentation, showing how a function is expected to be used, and they form a safety net that lets you refactor without fear.

The Testing Pyramid

The classic testing pyramid suggests many unit tests at the base (fast, isolated, cheap), fewer integration tests in the middle (verify modules work together), and very few end-to-end tests at the top (simulate real user journeys, slow and brittle). Inverting the pyramid — relying mostly on slow end-to-end tests — leads to long, flaky test suites. A balanced pyramid gives you speed at the bottom and confidence at the top.

Testing in the Workflow

Modern teams write tests alongside or even before production code. Tests run on every save locally and on every push in continuous integration. This lesson series will take you from the core concepts to concrete tools like Vitest and Jest, through test doubles, coverage, TDD, and finally to strategies for testing components, hooks, APIs, and accessibility.

In Practice

Starting out, you do not need a framework — Node's built-in assert module is enough to write your first test and feel the feedback loop. As your project grows, adopt a framework (Vitest or Jest) to get watch mode, mocking, and reporters. Begin by testing the pure functions that hold your business logic: calculations, validations, transformations. These are the cheapest to test and the most valuable to get right. Resist the urge to test everything through the UI; push assertions down to the layer where the logic actually lives. Over the rest of this series you will build the vocabulary and habits that make testing a natural, everyday part of writing software rather than a chore bolted on at the end.

示例代码

              // A minimal test written with Node's built-in assert module.
import assert from "node:assert/strict";

function add(a, b) {
  return a + b;
}

// Arrange — set up the inputs
const expected = 5;
// Act — call the code under test
const actual = add(2, 3);
// Assert — verify the result
assert.equal(actual, expected, "add(2, 3) should equal 5");
console.log("Test passed: add works correctly");
            

每个测试 follows 排列-Act-Assert 模式: 设置输入, 运行 code, 然后验证输出. 节点's assert 模块是 simplest 方式到 开始不使用任何框架.

动手试一试

  • Code Diff

    Compare expected 对比实际输出到 理解测试 failures.

  • JSON Formatter

    Inspect structured 数据你的测试经常 compare.

常见错误

错误写法

Treating 测试 as 可选 or '仅 for QA'

正确做法

测试是 核心 developer skill. 编写测试 as 你编写 code — 它们是 部分的 deliverable, 不 afterthought.

错误写法

编写仅 端-到-端测试因为它们 feel '真实'

正确做法

开始使用快 单元测试 for logic, 添加 integration 测试 for 边界, and keep 端-到-端测试少数 and focused. Follow pyramid.

练习 测试 Overview

2 个练习

练习 测试 Overview

查阅不认识的术语

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

查看术语表

动手做项目

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

查看项目

解读错误信息

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

查看错误