Rookie
Rookie

Reputation: 31

How to make the following code coverage up to 100%

public void test(int a)

{
    if (a<=3)
      {...}
    else if (3<a && a<=8)
      {...}
    else
      {...}
}

The above code is typed in vs 2010 and create UT for it. I tried to set the parameter a to be 2,3,4,8,9. seems it already covers all the code. but after running the UT, the code coverage still not be up to 100%. Any suggestion will be very appreciate!

Upvotes: 3

Views: 174

Answers (2)

andriys
andriys

Reputation: 2222

The fact that you cannot cover the block of code with a test is often a good indication that you may simplify your code without losing any of the execution paths. Consider the following code that does the same thing with 100% coverage:

    public void test(int a)
    {
        if (a <= 3)
        {
        }
        else if (a <= 8)
        {
        }
        else
        {
        }
    }

Upvotes: 1

hmakholm left over Monica
hmakholm left over Monica

Reputation: 23332

It is not possible for the 3<a test to come out false (because the first test has already disposed of that), so the jump from 3<a to the bottom else block is never executed.

Upvotes: 2

Related Questions