Skip to content

分支 Coverage

一句话解释

分支 coverage 检查那个每个如果/else 分支 (两者都真 and 假 paths) 是 tested, 不只是行.

用大白话说

分支 coverage 是 stricter than 行 coverage. 它检查不 只是那个行 ran, 但那个每个可能 path 通过 conditionals 是 taken.

For 示例, consider:

if (x > 0) { 返回 '正'; }
return '非-正';

If 你的测试仅 calls 这个使用 x=5, 行 coverage 是 50% (仅首先返回运行). 但偶数如果你 测试使用 x=5 and x=-1, 获取 100% 行 coverage, 分支 coverage 检查那个你 tested 两者都真 path (x > 0) AND 假 path (x <= 0).

Branch coverage catches 情况哪里你 有 100% 行 coverage 但仅 tested one 边的 条件. 它's 更多 meaningful metric than 行 coverage alone.

示例

javascript
function check(x) {
  if (x > 0) return 'positive';   // branch 1: true
  return 'non-positive';           // branch 1: false
}
// Test with x=5: covers true branch only (50% branch coverage)
// Test with x=5 and x=-1: covers both branches (100% branch coverage)

分支 coverage 需要测试两者都真 and 假 paths 的每个 conditional.

相关术语

相关资源

相关课程

解读错误信息

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

查看错误

← 返回术语表