statement and branch coverage

limonik picture limonik · Jul 25, 2015 · Viewed 9.3k times · Source

Hello everyone in these days i am preparing ISTQB exam and i am stuck with one of the questions.

mock exam QUESTION 16

Question is ( originally copy paste) here:

Given the following code, which is true:

IF A > B THEN
    C = A – B 
ELSE 
    C = A + B 
ENDIF 
Read D 
IF C = D 
    Then Print “Error” 
ENDIF 

a.1 test for statement coverage, 3 for branch coverage

b.2 tests for statement coverage, 2 for branch coverage

c.2 tests for statement coverage. 3 for branch coverage

d.3 tests for statement coverage, 3 for branch coverage

I solved as 3 tests for branch coverage ( 1. if A>B is true 2.if A>B is false 3. if C=D is true )and 3 for statement coverage ( C=A+B ,C=A-B and Error).

However answer says that 2 tests for statement coverage and 2 for branch coverage. Could someone please explain me.

Answer

user007 picture user007 · Jul 25, 2015

Branch Coverage ::

Branch coverage is a requirement that, for each branch in the program (e.g., if statements, loops), each branch have been executed at least once during testing. (It is sometimes also described as saying that each branch condition must have been true at least once and false at least once during testing.)

Statement Coverage ::

Statement coverage is a white box testing technique, which involves the execution of all the statements at least once in the source code. It is a metric, which is used to calculate and measure the number of statements in the source code which have been executed.

So, try making line numbers to your code ::

1. READ A --> Added just to make it more clear
2. READ B --> Added just to make it more clear
3. IF A > B 
4.     THEN C = A – B 
5. ELSE 
6.     C = A + B ENDIF 
7. Read D 
8. IF C = D 
9.     Then Print “Error” ENDIF 
10. END OF PROGRAM

So, considering the definitions above and taking some test cases ::

Test Case 1 :: A = 10, B = 11, D = 21

Statements Covered = 1, 2, 3, 5, 6, 7, 8, 9, 10.

Test Case 2 :: A = 11, B = 10, D = 10

Statements Covered = 1, 2, 3, 4, 7, 8, 10.

So, if you look at the Statements covered, you would realize that only 2 test cases are needed to cover all the statements.

Now, coming to Branch Coverage

If you make a program flow diagram of the above code, and going by the definition above, there is a branch at statement 3 and at statement 8, since they are if conditions so, they can be either true or false hence the branch is there. So, the definition of the Branch Coverage says that we need to traverse each branch in the program.

Since from 3 I can go to either 4 or 5 (the 2 branches), let us say the branch 3 to 4 is 3L and branch 3 to 5 is 3R (L and R mean left and right). Similarly for statement 8, the 2 branches can be 8 to 10 (if C != D) and 8 to 9 and then 10. Let these 2 branches be then called 8L and 8R respectively. (Just naming for understanding)

So, from Test Case 2 you can realize that you have covered your branch 3L and 8R from Test Case 1, you can realize you have covered your branch 3R and 8L

So, with just 2 test cases you have covered all your branches and statements.

Hope it makes you clear! Tried my best to do so. Just in case you don't understand try making a program flow graph and re-reading the answer.

EDIT :: In your question description you quote

I solved as 3 branches ( 1. if A>B is true 2.if A>B is false 3. if C=D is true )and 3 statements ( C=A+B ,C=A-B and Error).

Every line that I have numbered is a statement, not only the ones that you have written. Moreover, there are 4 branches 2 for each if condition. The point is to cover every branch and every statement with the test cases. And the question also asks how many test cases are needed and not the number of branches and statements!