Test case for 100% branch coverage with no fault?

半世苍凉 提交于 2019-12-10 13:09:35

问题


The problem statement is

A method that has a zero fault that you can write a test suite to that has 100% statement coverage but doesn't find the fault and another test suite that has 100% branch coverage that does reveal the fault?

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);

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

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


回答1:


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

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

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.)




回答2:


How about confusing a logical AND with an OR?

// This method must never throw; on error, it shall return -1.
int foo(final String s, final int n) {
    if (s != null || n != 0) {
        return s.length() / n;
    } else {
        return -1;
    }
}

The following test inputs achieve 100 % statement coverage and don't unveil the bug.

assert foo("everything is fine", 6) == 3;  // happy path, ok
assert foo(null, 0) == -1;                 // error path, ok

The test-suite has not foll branch-coverage, however, because the two expressions ORed together evaluate to the same value in each case. Adding the following two test cases completes branch coverage and exposes the bug.

assert foo("bingo", 0) == -1;  // ArithmeticException
assert foo(null, 4) == -1;     // NullPointerException

As a matter of fact, these four inputs together also achieve path coverage which is an even stronger requirement than branch coverage.



来源:https://stackoverflow.com/questions/33536168/test-case-for-100-branch-coverage-with-no-fault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!