Skip to content

Integration 测试

一句话解释

integration 测试检查那个 two or 更多部分的 你的系统工作正确地当 已连接, catching issues 那个单元测试 miss.

用大白话说

如果单元测试是 如测试 car brake 通过它自己, integration 测试是 如测试 brake together 使用 brake pedal and hydraulic 行. 你're 检查那个部分工作正确地当 已连接.

Integration 测试 sit 之间单元测试 and 端-到-端测试. 它们测试真实连接之间模块 — 如 service talking 到数据库, or two 模块 communicating 通过它们的接口.

They 是 slower than 单元测试 (因为它们 use 真实 dependencies) 但 faster than E2E 测试 (因为它们 don't 测试通过 UI). 它们 catch problems 如数据格式 mismatches, 接口 incompatibilities, and 不正确 wiring 之间组件.

示例

javascript
// Integration test: repository + real database
test('user is saved and retrieved', () => {
  const db = new InMemoryDb();
  const repo = new UserRepository(db);
  repo.create({ id: 1, name: 'Alice' });
  expect(repo.findById(1).name).toBe('Alice');
});

这个测试 uses 真实数据库 (in-memory) 相反的 mock, 测试完整数据 flow.

常见混淆

  • 容易混淆的对象: 单元测试

    关键区别: Integration 测试 use 真实 dependencies; 单元测试 mock 它们. Integration 测试是 slower 但 catch integration bugs.

  • 容易混淆的对象: E2E 测试

    关键区别: Integration 测试检查内部模块 interactions; E2E 测试检查完整用户 journey 通过 UI.

相关术语

相关资源

相关课程

解读错误信息

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

查看错误

← 返回术语表