Test version to cover 100% branches without errors?

Problem statement

A method that has zero error, that you can write a test suite that has 100% coverage of the operator but does not find an error and another test suite that has 100% coverage of the branches that detects an error?

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 an DiviDeByZeroException"); } } faultyMethod1 (1,2); faultyMethod1 (2,0); 

The above code to get a test suite that has 100% branch coverage that detects an error "

What about a test suite that contains 100% of the statements but doesn't find an error?

+5
source share
2 answers

Take 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 that 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% testing coverage (because the x <2 branch was covered, as well as the other). But we did not find an error, since using zero as the value for x hides it (since 0 * something is always 0, y does not matter). We would need something like this ...

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

The same problem can arise for any other situation, including division by zero. I can’t imagine a good example, but I think you understand that using 100% coverage does not mean finding 100% of all errors. (A cool way to find them is through mutation testing, but that's a pretty complicated topic.)

+5
source

How about confusing logical AND with 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 entries provide coverage for 100% of applications and do not disclose the error.

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

However, the test package has no foll branch coverage because the two ORed expressions together evaluate the same value in each case. Adding the following two test cases completes the coverage of the branches and provides an error.

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

In essence, these four entrances together also provide route coverage, which is an even stronger requirement than branch coverage.

+1
source

Source: https://habr.com/ru/post/1235267/


All Articles