Software Development and Project Management
The values of temp and err can be chosen independently of each other. For the code 'if (temp > 80) { fan = HIGH; } else { fan = LOW; } if (err) { log(); }', what is the minimum number of test cases needed to achieve statement coverage (executing every statement at least once)?
- A2
- B3
- C4
Show answer and explanation
Answer: A. 2
There are three statements: fan = HIGH, fan = LOW and log(). Only one of fan = HIGH and fan = LOW is executed in a single run, so at least two runs are needed; for example, the two test cases (temp > 80, err true) and (temp ≤ 80, err false) execute all three statements. 3 is the number obtained by making one test case per statement, and 4 is the number of all combinations of true and false for the two conditions (all paths). Statement coverage is the weakest coverage criterion; for example, it can be achieved without ever taking the false side of the second if (the path that does nothing).
- A. Correct. Two runs are needed to go through both the if and the else, and log() can be executed together with either of them.
- B. This makes one test case per statement. One test case can execute several statements, so fewer are needed.
- C. This is the number of all combinations of true and false (paths); statement coverage does not require that many.
Continue: 9 more Software Development and Project Management questions