The journal of Paul M. Watson.

Wednesday, December 14, 2005

Unit tests plus Code Coverage, that is the kicker

I just had a bit of a tada moment with test driven development. Unit tests are nice to have but lose a lot of value without code coverage results. The idea is that you want to be confident your code can handle what it is supposed to handle. You need to be confident and so you write unit tests to bang it about and make sure bits don't fall off.

If you don't do code coverage reports against your unit tests though you can't be confident that all of your code is being tested. And believe me you will come across routes through your code that you never thought possible. So don't fool yourself into thinking you don't need code coverage because your master genius brain is all knowing. It isn't, take a hit of humble juice man.

All of that I was told and understood and thankfully I am a humble programmer who is happy to have anything that can sort out his mess of wires.

The tada moment though came today while doing some unit tests for validation code I had yet to write (test driven development, remember.)

A class was meant to have a Validate method in which a series of checks ensured the values in the properties were all above board. I started out with some length checks on string data, did some numeric tests and then started on making sure that the string properties were not null and not empty. The Validate method would throw exceptions if it came across invalid data. These were ArgumentOutOfRangeException exceptions and almost all of the checks threw the same exception. The unit tests in turn ensured that as the bad data was passed-in the code threw the exception.

So all hunky-dory (that is a programming term for "I haven't got a flying clue what is going on but it hasn't fallen over yet so all's good.") and code coverage was at 100%.

I then did the null test on the Title string property and implemented the null check in the Validate method. That, and all the other tests, passed but suddenly my code coverage was way down to 34% or something awful. I nearly cried, our test manager was sure to beat me for having just 34% code coverage. What the hell was going on.

It turns out that because the null check threw an ArgumentOutOfRangeException and because the null check was at the top of the Valdiate method it was throwing for the other tests which were testing for other things but expecting that very same exception. So to the other tests it passed. But to code coverage there was a big chunk of code below the null check that was never run.

So the unit tests all passed but code coverage didn't and that is what alerted me to a problem in the validation code. I was then able to fix that and have passing unit tests and 100% code coverage again.

My test manager will be so pleased.


Post a Comment

Links to this post:

Create a Link

<< Home