Unit Testing != Quality Code
Unless you are strictly following a Test-Driven Development (TDD) methodology, unit testing is only extra work developers need to do (on top all the work they already have!) ‘Sounds like fun!’ I hear you say. If you are not following TDD, unit tests aren’t giving you much return on investment. ‘Why not?’ I hear you say (You are very talkative today, aren’t you?). The fact that a unit test passed tells you nothing about the quality of the code it tested or the quality of the unit test.
You should still test!
This post came about as a result of a very long email debate around unit testing. The email referred to this article. I’m not saying I disagree with the article and this post is not about said article, it is about why unit testing doesn’t work in a non TDD environment… well it doesn’t really work as expected.
I’m not saying don’t test you work, I’m saying don’t rely on unit test to up your quality. Yes, you might catch a few bugs, but how easy is it going to be to fix those bugs? If the quality of the code is bad then ‘fixing’ one bug could cause two other unit tests to start failing. It is like a gecko’s tail being cut of, but growing back slightly different.
Why is it bad?
So, why are unit tests on their own bad for trying to improve code quality, let me count the ways.
1. Unit tests are are just too forgiving and kind. The function being tested can contain the type of code you buy at the toilet store and the unit test still won’t care and would still pass it as long as the asserts are true.
2. If the code being tested is written badly I don’t think there is much hope for the unit testing code to be any better.
3. What are you trying to test? TDD is about testing requirements, not just individual functions. The unit tests might pass, but it doesn’t mean it is meeting the requirements.
4. If you have a problem with code quality at the moment, is adding more code the answer?
5. Last one before this list gets too long. If a project’s deadline is coming in for the kill, what do you think will be the first thing it catches and eats? Unit test just can’t run as fast.
Change the game
If you want to up the quality of your code look at the processes, methodologies and attitude of the team. Change how the team is doing things, because obviously something is wrong. You need to diagnose issues and see what needs to change to solve them, maybe TDD is needed, but then change the game completely.
Expecting unit testing to just work is like going to a doctor who just gives you a Panado and sends you on your way without even looking at you.
What are your thoughts on getting the quality of your code up? Will adding unit tests work, how about a strict review process or automated code evaluation on checking?
Also, subscribe to my blog and you won’t miss any future posts.