Wednesday, April 12, 2006

Test Coverage

Writing unit tests to hit a particular code coverage percentage is a sucker's game. It's very easy to write "tests" that exercise the code but don't determine whether it does the right thing. So you can get a nice high coverage number without having any good tests. This is especially tempting when you're writing the tests after the code.

Sometimes you can't exercise all the code. Or you decide that it's not important or too difficult to test. But writing tests to cover code always gives you a better coverage percentage than officially ignoring code. For example, if your test currently covers four of ten lines (40%), then ignoring two lines gives you 50% coverage (4/8), but writing tests to cover those two lines gives you 60% coverage (6/10). Not a profound insight, but there it is.

No comments: