In computer science, code coverage is ameasure used to describe the degree to which the source code of a program istested by a particular test suite. A program with high code coverage has beenmore thoroughly tested and has a lower chance of containing software bugs thana program with low code coverage. Many different metrics can be used tocalculate code coverage; some of the most basic are the percent of programsubroutines and the percent of program statements called during execution ofthe test suite.
在计算机科学中,代码覆盖率是一种度量,它被用来描述程序中源代码被特定测试套件所测试的等级。高代码覆盖率的程序已经被彻底地测试过了,所以它含有软件错误的可能性要比低代码覆盖率的程序低很多。许多不同的度量方式都可以计算代码覆盖率;最基本度量方式是测试套件在执行过程中的子程序覆盖比率和语句覆盖比率
Code coverage was among the first methodsinvented for systematic software testing. The first published reference was byMiller and Maloney in Communications of the ACM in 1963.
代码覆盖率是为系统软件测试而发明出来的最早方式中的一种。1963年,它在Miller and Maloney的ACM通讯中被第一次出版定义。
Coverage criteria
覆盖率准则
To measure what percentage of code has beenexercised by a test suite, one or more coverage criteria are used. Coveragecriteria is usually defined as a rule or requirement, which test suite needs tosatisfy.
为了测量测试套件的代码覆盖率,会用一种或多种覆盖率准则。覆盖率准则通常以测试套件需要满足的规则或需求来定义。
Basic coverage criteria
基本覆盖率准则
There are a number of coverage criteria,the main ones being:
有许多覆盖率准则,以下是其中主要的几种:
Function coverage - Has each function (orsubroutine) in the program been called?
Statement coverage - Has each statement inthe program been executed?
Branch coverage - Has each branch (alsocalled DD-path) of each control structure (such as in if and case statements)been executed? For example, given an if statement, have both the true and falsebranches been executed? Another way of saying this is, has every edge in theprogram been executed?
Condition coverage (or predicate coverage)- Has each Boolean sub-expression evaluated both to true and false?
For example, consider the following Cfunction:
函数覆盖率:程序中是否每个函数(或子程序)都被调用过?
语句覆盖率:程序中是否每条语句都被执行过?
分支覆盖率(判定覆盖率):是否每个控制结构(如if and case控制语句)中的每个分支(也被称为DD-path)都被执行过?举个例子,一条if控制语句,是否true和false两个分支都被执行过?另一种说法是,是否程序的每条边都被执行过?
条件覆盖率(或谓词覆盖率):是否每个子逻辑表达式true和false两个值都取过。
例如,考虑下面的C函数:
int foo (int x, int y)
{
int z = 0;
if ((x>0) && (y>0))
{
z = x;
}
return z;
}
Assume this function is a part of somebigger program and this program was run with some test suite.
假设此函数是某个更大程序的一部分,同时某个测试套件执行了这个程序
If during this execution function 'foo' wascalled at least once, then function coverage for this function is satisfied.
Statement coverage for this function willbe satisfied if it was called e.g. as foo(1,1), as in this case, every line inthe function is executed including z = x;.
Tests calling foo(1,1) and foo(0,1) willsatisfy branch coverage because, in the first case, the 2 if conditions are metand z = x; is executed, while in the second case, the first condition (x>0)is not satisfied, which prevents executing z = x;.
Condition coverage can be satisfied withtests that call foo(1,1), foo(1,0) and foo(0,0). These are necessary because inthe first two cases, (x>0) evaluates to true, while in the third, itevaluates false. At the same time, the first case makes (y>0) true, whilethe second and third make it false.
在测试执行过程中,只要函数’foo’被调用过,那么函数覆盖就满足了。
这个函数如果要满足语句覆盖,只有像foot(1,1)这样被调用,因为在这种情况下,这个函数的每一行包括z = x都被执行过。
测试如果调用过foo(1,1)和foo(0,1),将满足判定覆盖。因为在第一种情景下,这2个if条件成立,因而z = x;语句被执行,同时在第二种情景下,第一个条件(x>0)不满足,因而z = x;不会被执行。(即if和else分支都被执行过)
要满足条件覆盖,必须调用foo(1,1)、foo(1,0)和foo(0,0)。因为在前两个测试情景下,(x>0)为true,而在第三个则为false。同时,在第一个情景下(y>0)为true,而第二和第三种则为false。
Condition coverage does not necessarilyimply branch coverage. For example, consider the following fragment of code:
满足条件覆盖不一定意味着满足分支覆盖。例如,考虑下面这个代码片段:
if a and b then |
Condition coverage can be satisfied by twotests:
只要以下两个测试就可以满足条件覆盖:
a=true, b=false
a=false, b=true
However, this set of tests does not satisfybranch coverage since neither case will meet the if condition.
但是,这个测试集合并不满足分支覆盖,因为没有哪个使得if条件成立
Fault injection may be necessary to ensurethat all conditions and branches of exception handling code have adequatecoverage during testing.
为了确保在测试中异常处理代码的所有条件和分支都有足够的覆盖率,有时错误注入可能很有必要。