摘要:条件覆盖测试是一种白盒测试,用于测试程序中所有条件表达式的所有可能结果。它也称为谓词覆盖。那么条件覆盖率和分支覆盖率有什么区别呢?在分支覆盖中,所有条件都必须至少执行一次。另一方面,在条件覆盖中,所有条件的所有可能结果必须至少测试一次。
考虑下面的代码片段,它将用于进行条件覆盖率测试:
int a = 10;
if (a > 0){
cout << "a is positive";
}
Branch coverage requires that the condition a > 0 is executed at least once.
Condition coverage requires that both the outcomes a > 0 = True and a > 0 = False of the condition a > 0 are executed at least once.
考虑下面的代码片段,它将用于进行条件覆盖率测试:
int num1 = 0;
if(num1 > 0){
cout << "valid input";
}else{
cout << "invalid input";
}
The condition coverage testing of the code above will be as follows:
Test case number | num1>0 | Final output |
1 | True | True |
2 | False | False |
考虑下面的代码片段,它将用于进行条件覆盖率测试:
int num1 = 0;
int num2 = 0;
if((num1 > 0 || num2 < 10)){
cout << "valid input";
}else{
cout << "invalid input";
}
The condition coverage testing of the code above will be as follows:
Test case number | num1>0 | num2<10 | Final output |
1 | True | Not required | True |
2 | False | True | True |
3 | False | False | False |
考虑下面的代码片段,它将用于进行条件覆盖率测试:
int num1 = 0;
int num2 = 0;
if((num1 > 0) && (num1+num2 < 15)){
cout << "valid input";
}else{
cout << "invalid input";
}
The condition coverage testing of the code above will be as follows:
Test case number | num1>0 | num1+num2<15 | Final output |
1 | True | True | True |
2 | True | False | False |
3 | False | Not required | False |
考虑下面的代码片段,它将用于进行条件覆盖率测试:
int num1 = 0;
int num2 = 0;
if((num1>0 || num2<10) && (num1+num2<15)){
cout << "valid input";
}else{
cout << "invalid input";
}
The condition coverage testing of the code above will be as follows:
Test case number | num1>0 | num2<10 | num1+num2<15 | Final output |
1 | True | don't care | True | True |
2 | True | don't care | False | False |
3 | False | True | True | True |
4 | False | True | False | False |
5 | False | False | Not required | False |