My organization is emphasizing a line and branch coverage of 80%. I have absolutely no problem with the line coverage requirement, but the branch coverage has given me an issue.
Let's take the following case in point:
if(decisionA && decisionB)
{
// path A - do some complex ninja code stuff
}
else
{
// path B - tell user i can't do anything
}
Now, I've written 2 cases, the first one that covers path A and the second one covering path B. This should give me 100% line coverage. However, this gives me only 50% branch coverage since I've only covered (True && True) + (False && False) while ommitting (True && False) + (False && True).
From my perspective, the values of decisionA and decisionB are trivial, and hardly worth testing. However, the blanket requirement in my organization now means I would have to write 4 test cases instead of 2. Bring multiple if's and nested if's and this becomes complicated.
To me, it appears that it should be in the developer's decision whether he chooses to cover branch cases, so long as he deems that the important logic (in the case the ninja code stuff in part A) is getting covered.
What are your thoughts on this? What do you think would be an acceptable branch coverage rate? Is my angst about enforcing a high branch coverage rate justified or am I not emphasizing code quality enough? I know this is a bit of a subjective question, but surely there are good patterns already established regarding this.
a & b & ...
would only need a large number of tests to be covered if the code uses logical non-short-circuit operators instead of conditional operators (&&
) which are short-circuited. In practice, programmers use the short-circuit ones, requiring a much smaller number of tests. For example, given a test witha == false
, we don't need another test withb == false
to fully cover an expression containinga && b
. The true reason why condition coverage can't be used is simply that existing code coverage tools do not support it. – Gerta