Tuesday, November 10, 2009

Unit Testing: Are they valuable?

In my opinion no.

The first company I worked in, Unit tests were the holy grail, something they always wanted to do, but our codebase was so old, so complex and so intertwined it would have taken months of dev effort to write these tests and management didn't see the benefit. I left that company with the opinion that Unit tests were great, it would solve all problems and I would be a super effective programmer.

Enter my current job, I came in too a near completed version of our product and we had some serious server issue, tricky logic was just confusing us and in the end it was decided that unit tests would save us, bring us out of the maze and into the light, it was then I learnt the truth, unit tests are clunky and ugly.

Our problem was that we made the tests after we wrote the server code. So you write the test framework, run it, it goes red, you change it and run it and it goes green, but does it go green because it actually is correct or because the test suffers from the same logic issues that causes you to write the test in the first place.

So in theory you write the tests before you start the code base. So you write the test then you write the function, test the function rinse and repeat. Now some people would argue that this is the correct way to do it, but for every line of production code you have you have a line of testing, and the testing isn't guarenteed to help. Everytime you refactor you need to rewrite the tests, which could lead to the problem that we had that your test is no longer accurate.

Some people would say that writing the tests allows them to nail down the perfect design and stops the need to refactor. To that I say, anyone who says they are such a good developer that they never need to refactor their code is a liar. Code evolves with time and refactoring is the way to keep the codebase lean and clear with this evolution.

Now I am not saying that all tests are wrong, I am just saying that the fine granularity of Unit tests are wrong, Intergration/Acceptance tests are an entirely different beast and exceptionally value in a development suite.

For more information about this please read this following blog as it is the one that got me thinking about unit tests

No comments:

Post a Comment