测试覆盖率衡量有多少代码被测试执行 ——通常以百分比表示(例如 80% 的覆盖率)。这是一个寻找未测试代码的有用指标,但理解它的局限性很重要:高覆盖率不保证有好的测试。
覆盖率衡量的内容
text
TEST COVERAGE = the % of code exercised by the test suite:
LINE coverage → % of code lines executed by tests
BRANCH coverage → % of branches (if/else paths) taken
FUNCTION coverage → % of functions called
STATEMENT coverage → % of statements executed
→ Coverage tools instrument the code and report what tests do/don't exercise.
覆盖率为什么有用
text
✓ FINDS UNTESTED code — low coverage areas reveal what lacks tests (a gap finder)
✓ A measurable quality signal; track it over time; many teams set a target (e.g. ≥80%)
✓ Highlights branches/paths/error cases that tests miss
