For the last couple of years I have been zealous in my TDD discipline. By zealous I mean that when I write "public" on a method, I write a test. I was fortunate enough to attend a session on Testing Patterns (see previous blog entry). I had a question for the speaker about how he would handle testing a particular simple method. In this case the method was something like AddNewJob(xxx). To my suprise the recommendation was that I should not bother unit testing it as it was too simple and I should leave it up to the testers to cover it off in their testing activities!
This was a real eye-opener for me, so now I am in a bit of a quandry. Lots of the code I right for mid-range applications is very similar to this method. The AddNewJob(xxx) method inserts data into a few related tables. My tests have historically queried the SQL tables to ensure that the insert was completed correctly. I had noticed a significant reduction in error rates following this approach.
As an experiment, I have recently ( a few weeks ago) stopped writing unit tests for all "public" methods and I am just writing tests for more complex methods. I have made a few observations as a result of this experiment:
- I don't think my error rate has gone up
- I do not believe that my coding speed has improved
- I am writing almost no tests (most of my code is very simple - which is a good thing I suppose)
- I don't feel virtuous any more
In the short-term then, I seem to have retained the benefits of TDD even though I am not doing it as much! I suspect in the long term that my "untested" code will suffer a little as I do not have automated tests around it. I think that this means I will not spot side-effects of code changes as early as I will on tested code.
I suspect that my retention of quality is a direct result of the extra coding discipline that TDD has instilled in me. I find my self being much more careful about parameter checking etc these days. I think that the longer I go without writing tests, the more lax I will become in my coding and my error rate will begin to creep up again.
So, the result of my experiment is pretty inconclusive (largely because I have not formally collected data) and I think I will retain to my more zealous approach, if for no other reason than it makes me feel more confident and more virtuous about my code!