声明和决策覆盖范围之间的差异

Joh*_*n V 24 metrics code-coverage

据说语句覆盖确保代码中的每个语句至少执行一次.据说
决策/分支覆盖测试是测试决策的每个分支/输出,即将执行假/真分支中的所有语句.
但是不一样吗?在Statement覆盖中我需要执行所有语句,所以我想它只能通过运行所有可能的方式来完成.我知道我在这里遗失了一些东西..

小智 38

The answer by Paul isn't quite right, at least I think so (according to ISTQB's definitions). There's quite significant difference between statement, decision/branch and condition coverage. I'll use the sample from the other answer but modified a bit, so I can show all three test coverage examples. Tests written here gives 100% test coverage for each type.

if(a || b)) {
    test1 = true;
}
else {
    if(c) {
      test2 = true
    }
}
Run Code Online (Sandbox Code Playgroud)

We have two statements here - if(a||b) and if(c), to fully explain those coverage differences:

  1. statement coverage have to test each statement at least once, so we need just two tests:
    • a=true b=false - that gives us path if(a||b) true -> test1 = true
    • a=false, b=false and c=true - that gives us path: if(a||b) false -> else -> if(c) -> test2=true.

This way we executed each and every statement.

  1. branch/decision coverage needs one more test:

    • a=false, b=false, c=false - that leads us to that second if but we are executing false branch from that statement, that wasn't executed in statement coverage

    That way we have all the branches tested, meaning we went through all the paths.

  2. condition coverage needs another test:

    • a=false, b=true - that leads through the same path as first test but executes the other decision in OR statement (a||b) to go through it.

That way we have all conditions tested, meaning that we went through all paths (branches) and triggered it with each condition we could - first 'if' statement was true in first test because of a=true triggered it and in the last test because b=true triggered it. Of course someone can argue that case with a=true and b=true should be tested as well, but when we will check how 'or' works then we can see it isn't needed and also variable c can be of any value as in those tests it is not evaluated.

At least I interpreted it this way. If someone is still interested :)

EDIT: In most sources I found lately decision/branch coverage terms are equivalent and the term I described as decision coverage is in fact condition coverage hence that update of the answer.

  • `test1 = true;`是一个变量赋值,因此是一个可执行语句. (5认同)
  • 我不认为这是正确的.根据ISTQB术语表(http://astqb.org/glossary/):"语句覆盖率:测试套件已执行的可执行语句的百分比."在"语句覆盖"示例中,`test1 = true; `永远不会执行,但它显然是一个可执行语句. (2认同)
  • 第3项是不正确的,因为*decision*coverage不需要在复杂的决策点中执行每个单独的*条件*.如果需要,我们有*条件*覆盖. (2认同)

mur*_*ish 17

如果测试具有完整的分支覆盖,那么我们可以说它也具有完整的语句覆盖率,但反之亦然.

100%分支覆盖率=> 100%的声明覆盖率

100%的声明覆盖率并不意味着100%的分支覆盖率

原因是除了执行所有语句之外的分支覆盖,我们还应验证测试是否执行所有分支,这可以解释为覆盖控制流分支中的所有边

if(a){
   if(b){
     bool statement1 = true;
   }
}
Run Code Online (Sandbox Code Playgroud)

a = true,b = true将给出100%的语句覆盖率,但不提供分支覆盖率

在此输入图像描述

在分支覆盖中,我们需要覆盖所有边缘,我们在上面图像中显示为红线的语句覆盖中遗漏了这些边缘


ava*_*sen 6

好问题。我经常使用的解释是,没有 else 分支的 if 语句仍然有一个不可见的“空”else 语句:

  • 简单的语句覆盖只是坚持认为所有实际存在的语句都被真正执行。

  • 分支覆盖坚持认为,即使是不可见的 else 分支也会被执行。

没有 default-case 的 switch 语句和重复直到循环也会出现类似的情况。分支覆盖要求执行default-case,并且repeat-until至少执行两次。

代码示例:

if (passwordEnteredOK()) {
    enterSystem();
} 
/* Invisible else part 
else {
  // do nothing
}
*/
Run Code Online (Sandbox Code Playgroud)

通过声明覆盖,您只需检查是否使用正确的密码即可使用系统。通过分支机构覆盖,您还可以测试使用不正确的密码将无法进入系统。