Skip to content

类型的 测试

单元, integration, functional, and 端-到-端测试解释使用当 到 use 每个类型.

你将学到

  • difference 之间单元, integration, and 端-到-端测试
  • 什么 functional, smoke, and regression 测试是
  • 当每个测试类型 gives best 返回 on investment
  • 如何 balance 速度 and confidence 跨越测试类型

概念讲解

单元测试

A unit test exercises the smallest meaningful piece of code — usually a single function or method — in isolation. Dependencies are replaced with test doubles so that the test only checks the unit's own logic. Unit tests are fast (often thousands per second), deterministic, and pinpoint failures precisely. They are the foundation of your suite.

集成测试

An integration test verifies that two or more modules work correctly together. Instead of stubbing every dependency, you let real modules interact, often with a real database or a real file system in a controlled state. Integration tests are slower than unit tests but catch wiring bugs — wrong function signatures, mismatched data shapes, transaction boundaries — that unit tests miss.

End-to-End (E2E) Tests

An end-to-end test drives the whole application from the outside, typically by automating a browser the way a real user would: clicking buttons, filling forms, and asserting that the page shows the right result. E2E tests give the highest confidence that the system truly works, but they are slow, brittle, and expensive to maintain, so you keep them few and focused on critical journeys.

Other Useful Types

Smoke tests run after a deploy to check that the system boots and core paths work. Regression tests are added specifically to prove a previously fixed bug stays fixed. Functional tests verify business requirements without caring about implementation details. Choosing the right type for each scenario is a key testing skill — you will rarely rely on just one.

In Practice

A practical rule of thumb: test a piece of logic at the lowest layer that can observe it. If a calculation can be checked with a unit test, do that — it runs in a millisecond and pinpoints the failure. If two modules must agree on a data shape, write an integration test that lets them run together. If the only way to know the feature works is to click through it like a user, write one E2E test for that journey. Mixing types is normal and healthy; what you want to avoid is putting every assertion in the slowest, most brittle layer. Tag your tests by type (@单元, @integration, @e2e) so you can run a fast subset locally and the full suite in CI. This layered approach keeps your feedback loop tight while still giving you the confidence that the whole system hangs together.

示例代码

              // Unit test: pure function in isolation
function discount(price, percent) {
  return price - price * (percent / 100);
}
assert.equal(discount(100, 20), 80);

// Integration test: repository + in-memory database together
const db = new InMemoryDb();
const repo = new UserRepository(db);
await repo.save({ id: 1, name: "Ada" });
const found = await repo.findById(1);
assert.equal(found.name, "Ada");

// E2E test (conceptual): drive a real HTTP endpoint
const res = await fetch("/api/users/1");
assert.equal(res.status, 200);
            

相同 feature 能是 tested at three levels. 单元测试检查 math, integration 测试检查数据 layer, and E2E 测试检查 HTTP contract 从之外.

动手试一试

常见错误

错误写法

Calling 测试 '单元' 虽然它 hits 真实数据库

正确做法

如果测试 touches 外部系统, 它是 integration 测试. Replace 数据库使用 fake or move 测试到 integration suite.

错误写法

Using E2E 测试 for logic 那个单元测试能 cover

正确做法

推送 logic 下到 pure 函数 and 单元-测试它们. Reserve E2E 测试 for 用户-可见 journeys 仅.

练习 类型的 测试

6 个练习

练习 类型的 测试

查阅不认识的术语

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

查看术语表

动手做项目

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

查看项目

解读错误信息

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

查看错误