Today, I decided to go through what is line coverage as it was discussed yet again throughout the duration of the day, and then onto branch coverage, and what is the importance between the two.
Taken a bit of theoretical snippet of code as an example : -
if (cond)
{
line1();
line2();
line3();
line4();
}
else
{
line5();
}
So in branching, if both conditions are tested, then 2 out of 2 branches are covered aswell as 5 out of 5 lines covered.
If your test only exercises the cond being true and never runs the else branch you have -
4 out of 5 lines covered
1 out of 2 branches covered
Whilst if your test only exercises the cond being false you have -
1 out of 5 lines covered
1 out of 2 branches covered
But is it worth getting 100% line statement coverage, but in some cases, it is worth it. In some cases 100% statement coverage is far too lax a requirement. Moreso, the key question to ask yourself is, "what's the impact if the software fails (and produces the wrong result)?". In most cases, the impact of a bug is relatively low with the lines not fully covered in comparison with what if the branch isn't fully covered. It's like saying (kind of), does the car manages to cover a swerve on a wet road in a ride with the powered steering on (and has the test covered it), rather than it hasn't been test and that scenario hasn't been covered. Is it better than rather testing does the steering wheel steer that half a center meter (1/2cm ) more than what it should expect to cover. Well yes!
Line coverage generally provides a fairly superficial view of test completeness by checking whether each line of code has been executed.
On the other hand, branch coverage provides a deeper understanding by evaluating the execution of code branches stemming from decision points. So is 100% line code coverage correct? No, not really as what matters is the impact of the software code giving the intended required correct result.