Skip to content

Code Coverage

一句话解释

Code coverage measures 如何 much 的你的 code 是 exercised 通过测试 — e.g., '85% 的行 是执行期间测试.'

用大白话说

Code coverage tells 你哪个部分的 你的 code ran 期间测试 and 哪个 didn't. 它's 通常 expressed as 百分比: '80% 的行 covered' 平均值 80% 的你的 code 行是 执行期间测试.

There 是不同类型的 coverage:

  • Line coverage: 百分比的 行执行
  • Branch coverage: 百分比的 如果/else 分支 taken (两者都真 and 假 paths)
  • Function coverage: 百分比的 函数 called
  • Statement coverage: 百分比的 语句执行

Coverage 是 useful metric 但不 guarantee 的 quality. 100% coverage doesn't 平均值你的 code 是 bug-free — 它只是平均值每个行 ran. 你能 有 100% coverage 使用 weak assertions 那个 don't actually 测试任何事 meaningful. Use coverage as 指南到 查找 untested code, 不 as goal in 它自己.

示例

javascript
// Run Jest with coverage
// npx jest --coverage

// Output:
// File     | % Stmts | % Branch | % Funcs | % Lines
// math.js  |   100%  |   85%    |  100%   |  100%
// utils.js |    80%  |   70%    |   75%   |   82%

Coverage report shows 不同 coverage 类型每 file.

常见混淆

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

    关键区别: Coverage measures 哪个 code ran, 不 whether assertions 是 meaningful. 高 coverage 使用 weak assertions gives 假 confidence.

相关术语

相关资源

相关课程

解读错误信息

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

查看错误

← 返回术语表