Alex Rivera | Logout

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

Asked 2009-02-17T07:34:56.400
16

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.

Edit
Report

3 Answers

2

Errors in tests :)

answered 2009-02-17T07:47:44.387
1

Well if your tests don't test the thing which happens in the code covered. If you have this method which adds a number to properties for example:

public void AddTo(int i)
{
NumberA += i;
NumberB -= i;
}

If your test only checks the NumberA property, but not NumberB, then you will have 100% coverage, the test passes, but NumberB will still contain an error.

Conclusion: a unit test with 100% will not guarantee that the code is bug-free.

answered 2009-02-17T07:43:25.113
0

Code Coverage doesn't mean much. What matters is whether all (or most) of the argument values that affect the behavior are covered.

For eg consider a typical compareTo method (in java, but applies in most languages):

//Return Negative, 0 or positive depending on x is <, = or > y
int compareTo(int x, int y) {
   return x-y;
}

As long as you have a test for compareTo(0,0), you get code coverage. However, you need at least 3 testcases here (for the 3 outcomes). Still it is not bug free. It also pays to add tests to cover exceptional/error conditions. In the above case, If you try compareTo(10, Integer.MAX_INT), it is going to fail.

Bottomline: Try to partition your input to disjoint sets based on behavior, have a test for at least one input from each set. This will add more coverage in true sense.

Also check for tools like QuickCheck (If available for your language).

answered 2009-02-17T16:35:49.890

Your Answer