Code coverage is a measure used in software testing. It describes the degree to which the source code of a program has been tested. It is a form of testing that inspects the code directly and is therefore a form of white box testing[1].
Basic coverage criteria
There are a number of coverage criteria, the main ones being:[3]
- Function coverage - Has each function (or subroutine) in the program been called?
- Statement coverage - Has each node in the program been executed?
- Branch coverage - Has every edge in the program been executed?
- Decision coverage (also known as branch coverage) - Has each control structure (such as an IF statement) evaluated both to true and false?
- Condition coverage (or predicate coverage) - Has each boolean sub-expression evaluated both to true and false? This does not necessarily imply decision coverage.
- Condition/decision coverage - Both decision and condition coverage should be satisfied.
代码覆盖率,简单来说代码被执行过,就是“覆盖过”,如果一段程序运行了一组测试用例之后,100%的代码被执行了,那么是否就说明再也不用写新的测试用例了呢?
答案是否定的。
(1)不同代码是否执行,有很多组合,一行代码被执行过,没有问题,并不表明这一行程序在所有可能条件的组合下都能正确无误地运行。
(2)代码覆盖不能测出还没有写的代码(缺少的逻辑)导致的错误。
比如:
a. 没有检查过程调用的返回值;
b. 没有释放资源。
(3)代码覆盖不能测出效能问题。
(4)代码覆盖不能测出时序问题,由时序导致的程序错误(例如:线程之间的同步)。
(5)代码中和用户界面相关的功能不能简单地以代码覆盖率来衡量优劣。
reference: