Skip to content

Flaky 测试

一句话解释

flaky 测试是 unreliable — 它通过 and 失败 randomly 不使用 code 更改, eroding trust in 测试 suite.

用大白话说

flaky 测试是 enemy 的 healthy 测试 suite. 它's 测试那个 doesn't give consistent results — 它可能通过 on 你的 machine 但失败 in CI, or 通过 today and 失败 tomorrow, 使用否 code 更改.

Common causes 的 flakiness:

  • Timing issues: relying on setTimeout or 真实时间
  • Shared state: 测试那个 depend on execution 顺序
  • External dependencies: network calls, 真实数据库, file 系统
  • Race conditions: 异步操作 completing in 不同顺序
  • Random data: using Math.random() 不使用 seeding

Flaky 测试是 dangerous 因为它们 erode trust. 如果 developers see 失败测试 and think 'oh, 那个 one 是只是 flaky,' 它们开始 ignoring failures — and 真实 bugs slip 通过. Fix flaky 测试 immediately or quarantine 它们.

示例

javascript
// FLAKY: depends on timing
test('data loads within 100ms', () => {
  const start = Date.now();
  fetchData(); // might take 50ms or 150ms
  expect(Date.now() - start).toBeLessThan(100);
});
// FIX: use fake timers or test behavior, not timing

这个测试是 flaky 因为真实 timing varies. Use fake timers or 测试 result, 不 duration.

相关术语

相关资源

相关课程

解读错误信息

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

查看错误

← 返回术语表