Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type of errors could my code still contain even if I have 100% code coverage?

What type of errors could my code still contain even if I have 100% code coverage? I'm looking for concrete examples or links to concrete examples of such errors.

like image 598
Fung Avatar asked Feb 17 '09 07:02

Fung


People also ask

Is having 100% code coverage a good thing?

Good coverage does not imply good tests This is the most common point from 100%-coverage detractors: a covered code does not mean it's well tested. It is better to have 50% of the project tested correctly, than 100% poorly tested. This is correct.

Does 100% path coverage always reveal fault?

In 100% statement coverage, we are certain that every node of the CFG has been executed at least once. Of coursecourse, this does not mean that we are guaranteed to uncover any faults in the statements, but if we hadn''t covered 100%, we could be certain that sometimes we would fail to uncover faults.

Is it possible to have 100% testing coverage explain why or why not?

“You can have 100 percent coverage with completely worthless tests,” they'll point out. And they'll be completely right. To someone casually consuming this metric, the percentage can easily mislead. After all, 100 percent coverage sounds an awful lot like 100 percent certainty.


2 Answers

Having 100% code coverage is not that great as one may think of it. Consider a trival example:

double Foo(double a, double b)
{
    return a / b;
}

Even a single unit test will raise code coverage of this method to 100%, but the said unit test will not tell us what code is working and what code is not. This might be a perfectly valid code, but without testing edge conditions (such as when b is 0.0) unit test is inconclusive at best.

Code coverage only tells us what was executed by our unit tests, not whether it was executed correctly. This is an important distinction to make. Just because a line of code is executed by a unit test, does not necessarily mean that that line of code is working as intended.

Listen to this for an interesting discussion.

like image 84
Anton Gogolev Avatar answered Oct 25 '22 20:10

Anton Gogolev


Code coverage doesn't mean that your code is bug free in any way. It's an estimate on how well you're test cases cover your source code base. 100% code coverage would imply that every line of code is tested but every state of your program certainly is not. There's research being done in this area, I think it's referred to as finite state modeling but it's really a brute force way of trying to explore every state of a program.

A more elegant way of doing the same thing is something referred to as abstract interpretation. MSR (Microsoft Research) have released something called CodeContracts based on abstract interpretation. Check out Pex as well, they really emphasis cleaver methods of testing application run-time behavior.

I could write a really good test which would give me good coverage, but there's no guarantees that that test will explore all the states that my program might have. This is the problem of writing really good tests, which is hard.

Code coverage does not imply good tests

like image 24
John Leidegren Avatar answered Oct 25 '22 21:10

John Leidegren