What code is worth unit testing ?

This is a topic that came up in a vigorous conversation recently. The example we used was whether or not tests should be written for accessors. I was on the yes side and will expose my arguments here.

My first argument was yes because it has happened time and again to me that the wrong member was returned by the accessor. The counter argument was that code inspection would often reveal those issues.

My second argument was that the tests are not written only to validate the code at the time it is written but also for the future when changes happen. The counter argument being that in the mean time it is that much more code that needs to be maintained.

My final argument was that in any case it was simpler and cheaper to set as a policy that everything should be tested and then agree on exceptions during code reviews compared to debate for every piece of code whether it should be tested or not. That being said the policy might also be ‘test everything except accessors’.

I failed however to make the argument that I find the most useful: if is not worth testing, why is it worth writing ?

Your thoughts, arguments and counter arguments, as always, are very much welcomed.

2 Responses to “What code is worth unit testing ?”

  1. Eric Says:

    I find it very useful to question the necessity of accessors in the first place. Sometimes this is just a code smell showing that the code is not very object-oriented (the “Hollywood principle”).

  2. Chester Blyther Says:

    Simply just observed the swallowed dude using any Jeter top & shouting on cars and trucks such as he’d some thing about whatever.

Leave a Reply