Skip to content

行 Coverage

一句话解释

行 coverage 是 simplest coverage metric — 什么百分比的 你的 code 行是 执行通过测试.

用大白话说

行 coverage 是大多数基础 coverage metric. 它简单地数量如何许多行 的 code 是执行期间你的测试运行, 划分通过总计数字的 行.

For 示例, 如果你的 file 有 100 行的 code and 你的测试执行 85 的它们, 你的行 coverage 是 85%.

Line coverage 是简单到 理解但 能是 misleading. 行能 是 'covered' (执行) 不使用是 properly tested. 例如, 如果行 运行但 你从不 assert on 它的输出, 它's covered 但不 验证. 也, 单个行 使用 ternary 运算符可能有 两者都分支但 行 coverage 仅数量它 as one 行.

Use 行 coverage as 开始点, 但也 look at 分支 and 函数 coverage for 更多完整 picture.

示例

javascript
// Source file: math.js
function abs(n) {
  if (n < 0) return -n;   // line 2 — covered when n < 0
  return n;               // line 3 — covered when n >= 0
}

// Test file: math.test.js
test('abs of negative', () => {
  expect(abs(-5)).toBe(5);  // covers line 2 only
});

// Coverage report:
// File     | % Stmts | % Branch | % Lines |
// math.js  |   75%   |   50%    |  100%   |
// Line 3 is covered (return n) but the n >= 0 branch is not tested explicitly.

行覆盖率追踪测试执行了多少源代码行。这里测试覆盖了第 2 行(n < 0 路径);第 3 行作为回退执行了,但正数输入分支未被显式测试,因此分支覆盖率只有 50%。

相关术语

相关资源

相关课程

解读错误信息

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

查看错误

← 返回术语表