Hello everyone in these days i am preparing ISTQB exam and i am stuck with one of the questions.
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.
Branch Coverage ::
Statement Coverage ::
So, try making line numbers to your code ::
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
3and at statement8, since they areifconditions so, they can be eithertrueorfalsehence the branch is there. So, the definition of the Branch Coverage says that we need to traverse each branch in the program.Since from
3I can go to either4or5(the 2 branches), let us say the branch3to4is3Land branch3to5is3R(L and R mean left and right). Similarly for statement8, the 2 branches can be8to10(ifC != D) and8to9and then10. Let these 2 branches be then called8Land8Rrespectively. (Just naming for understanding)So, from Test Case 2 you can realize that you have covered your branch
3Land8Rfrom Test Case 1, you can realize you have covered your branch3Rand8LSo, 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
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
ifcondition. 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!