4

When checking statement coverage for my code:

 double programme(double x, double y)
 {
    double z 
    if(x>=5){
      z = 15;
    }
    else if(x>=3){
      z= 10;
    }
    else {
      z=0;
    }
    if (y>z)
    {
      z=y;
    }

    return z;
}

using two test cases (eg test 1: x = 6, y = 10 and test 2: = 3, y =5)

I'm not sure if the statement coverage is equal to 100% or 66% based on the fact that I'm not sure if you count the last if statement as it is false both times.

3
  • 1
    So do you cover z = 0; statement? Commented Mar 22, 2015 at 19:32
  • If y ends up being smaller than z (eg y = 5. z = 10), do you still add that statement to the overall statement coverage? Commented Mar 22, 2015 at 19:37
  • Well, the question is - is z = 0; ever executed? Commented Mar 22, 2015 at 19:38

1 Answer 1

4

There are eight statements in your method - three conditionals, four assignments, and a return:

  1. if (x>=5)
  2. z=15
  3. if (x>=3)
  4. z=10
  5. z=0
  6. if (y>z)
  7. z=y
  8. return

The first test case covers statements 1, 2, 6, and 8. The second test case covers 1, 3, 4, 6, and 8. Therefore, statements 1, 2, 3, 4, 6, and 8 are covered, for 6 out of 8 or 75% coverage.

Sign up to request clarification or add additional context in comments.

4 Comments

Is that not branch coverage though?
@CormacHallinan In this case it's close, because each branch has a single statement. Statement coverage could get squeed, though, when a single covered branch contains lots of statements, while several non-covered branches have a single statement each.
I'm not 100% sure what you mean by this. Could you give an example based on the code I made up?
@CormacHallinan Imagine that the first branch of if(x>=5) has 92 additional assignment statements. Now the code has 100 statements, with 98 statements covered, for an extremely healthy 98% coverage. Branch coverage, however, does not change at all - it remains 60% (3 out of 5 branches are covered), so counting branch coverage gives you a better measurement of code coverage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.