# How to Calculate Statement Coverage

Statement Coverage a metric which gives you an idea if every statement inside the code is executed at least once. This is a white box technique.

As the objective says it is the lowest possible test cases that will cover all the statements at least once.

Let us take a piece of code:

IF A > B THEN

C = A – B

ELSE

C = A + B

ENDIF

Read D

IF C = D Then

Print “Error”

ENDIF

here 3 statements needs to be covered.Our objective is to find out what is the minimum number of test cases that is possible value of A,B,C,D will satisfies this….

lets take a case where A<B

A=40 and B=50,D=20

This values will execute statement –2 and will skip statement-1.In the very next if condition will fail and will not execute Statement-3…But this condition will satisfy Statement-2 only.

Now twist the input a little bit.

Here i will consider A>B

A=50,B=30,D=20

These values will surely satisfy the Statement –1 and in the second case will satisfy Statement-3…But again this will not execute –Statement-2.

So the conclusion is Minimum number of tetstcases required to execute each statemnt once is –2.

Hence the Statement Coverage-2

This is a very small piece of code where we found out the statement coverage…but if this is a very large lines of code then manually finding out of this value is really tough.

There is tool called Profiler that will indicate the no of statement executed and by doing trials we will be able to find out the Statement coverage.

As the objective says it is the lowest possible test cases that will cover all the statements at least once.

Let us take a piece of code:

IF A > B THEN

C = A – B

ELSE

C = A + B

ENDIF

Read D

IF C = D Then

Print “Error”

ENDIF

here 3 statements needs to be covered.Our objective is to find out what is the minimum number of test cases that is possible value of A,B,C,D will satisfies this….

lets take a case where A<B

A=40 and B=50,D=20

This values will execute statement –2 and will skip statement-1.In the very next if condition will fail and will not execute Statement-3…But this condition will satisfy Statement-2 only.

Now twist the input a little bit.

Here i will consider A>B

A=50,B=30,D=20

These values will surely satisfy the Statement –1 and in the second case will satisfy Statement-3…But again this will not execute –Statement-2.

So the conclusion is Minimum number of tetstcases required to execute each statemnt once is –2.

Hence the Statement Coverage-2

This is a very small piece of code where we found out the statement coverage…but if this is a very large lines of code then manually finding out of this value is really tough.

There is tool called Profiler that will indicate the no of statement executed and by doing trials we will be able to find out the Statement coverage.

## Post a Comment