100%分支覆盖率且无故障的测试用例? [英] Test case for 100% branch coverage with no fault?

查看:266
本文介绍了100%分支覆盖率且无故障的测试用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题陈述是

一种方法具有零错误,您可以编写一个测试套件以覆盖100%的语句覆盖率,但找不到该错误,而另一个测试套件具有100%的分支覆盖率却可以揭示该错误?

这是我为同一方法编写的方法

Here is the method I wrote for the same

public  faultyMethod1(int x, int y) {
  int X =x;
  int Y = y;

  if (Y !=0){
    Z = X/Y;
  } else {
    System.out.println("Sorry. That's an DiviDeByZeroException");
  }
}

faultyMethod1 (1,2);
faultyMethod1 (2,0);

以上代码实现了具有100%分支覆盖率的测试套件,确实可以揭示故障"

The above code to achieve test suite that has 100% branch coverage that does reveal the fault"

具有100%语句覆盖率但没有发现错误的测试套件怎么样??

What about test suite to that has 100% statement coverage but doesn't find the fault ?

推荐答案

让我们再举一个例子...

Let's make another example...

// The method, according to our imaginary specification, should do:
//  return x * y, IF x is less than 2
//  return x + y, in any other case
public int doSomething(int x, int y) {

if (x < 2 ) {
   return x * x;  // this is our bug, it SHOULD be x * y
}

return x + y;

}

现在假设我们有两个测试:

Now imagine we have two tests:

assertEquals(0, doSomething( 0, 12) );  // works, as 0 * 0 == 0 * 12
assertEquals(20, doSomething( 10, 10 ) ); // works fine

因此,现在我们具有100%的测试覆盖率(因为x< 2分支以及另一个分支都已覆盖).但是我们没有发现错误,因为使用零作为x的值会将其隐藏(因为0 *始终为0,所以y是无关紧要的).我们本来需要这样的东西...

So, now we have 100% test coverage (because the x < 2 branch has been covered, as well as the other one). But we didn't find the bug, since using zero as value for x hides it (since 0 * something is always 0, y is irrelevant). We would have needed something like this...

assertEquals(12, doSomething( 1, 12) );  // fails, because it will be 1*1 == 1

对于任何其他情况,包括被零除,都可能发生相同的问题.无法想象一个好的例子,但是我想您已经基本了解了如何获得100%的覆盖率并不意味着找到所有bug的100%. (找到它们的一种很酷的方法是进行突变测试,但这是一个相当高级的主题.)

The same problem can occur for any other situation, including a division by zero. Can't imagine a good example, but I guess you get the basic idea how having a 100% coverage does not mean finding 100% of all bugs. (A cool way to find them would be mutation testing, but that's a pretty advanced topic.)

这篇关于100%分支覆盖率且无故障的测试用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆