function WhatIsGreater(a, b) {
let result;
if (a > b) {
result = 'a is greater than b';
}
if (b > a) {
result = 'a is less than b';
} else {
result = 'friendship :)';
}
return result;
}
a) 1 test is enough
b) 2 tests are required for full coverage
c) The required number of tests cannot be determined due to lack of information
d) Requires 3 tests
Expand the correct answer with an explanation
To answer this question you need to know the basics of flowcharts. Flowcharts use special shapes to represent different types of actions or steps in a process.
Thanks to the flowchart, we can clearly see how our code works and analyze how many tests are needed to achieve 100% Decision/Branch coverage. The main aim of the Decision/Branch coverage is to cover all the branches at least once (true and false). That is, you need to find and cover a minimum number of ways.
Picture of our code as a flowchart:
So we have 3 branches, the number of test cases for which is 3:
- The first - checks that if WhatIsGreater (2, 1), then as a result of execution if (a> b) we get 'a is greater than b'.
- The second - makes sure that if WhatIsGreater (1, 2), the program will pass if (b> a) and the result will be 'a is less than b'.
- The third test case - checks that in other cases the result is 'friendship :)'. That is, if the parameters are the same WhatIsGreater (2, 2), we get 'friendship :)'.
To achieve 100% decision coverage we need 3 test cases.