Unit Testing? Why?

17. Oct 2007

Could unit testing save your life?

For a lot of software developers, unit testing may seem like an unnecessary extra effort. And that available time should be rather spent on writing actual product code, producing more features or fixing bugs instead of wasting it on writing code to test the actual product code. Some may see it as suitable for large teams and big software corporations but for smaller teams and corporations with very restricted time and resources it wouldn’t make sense.

Not only does unit testing allow developers to recognize buggy code early on, but it also has several other important aspects. The most important among them might be that it provides a living and breathing contract, so to speak, of your code and how it should function. And each time tests are run, you make sure that your code adheres to this contract.

Another important benefit is that it simplifies the process of refactoring code, because it will always ensure that your code will still work appropriately, just as it did before refactoring.

One more significant benefit that might be the most disregarded is that unit tests could be the most efficient form of documentation for your code, particularly if your product code is a software library that other developers will use. Because it provides a clear exemplar code on how your product should be used.

Unit testing also encourages good practices in writing code. A general rule is that a unit test should not go outside the limits of its respective class. And if you have classes that reference other classes and you are forced to spread out your test to these referenced classes, then you should abstract your code using interfaces which effectively reduces the degree of coupling in your code.

I think it is granted that in every serious software product, code is destined to change, have bugs, include design flaws and will have some parts that are less readable and understandable than others. And unit testing will help you greatly with all these aspect.

However, if you think that your software product runs perfectly without any problems, you don’t think it will ever change, it has an impeccable design and it is completely readable and understandable. Then you clearly have bigger issues than deciding whether to use unit testing or not and I apologize for wasting the past few minutes of your life and I would be glad to inform you that the Google Copernicus Center is hiring and I really think you should apply.

AddThis Social Bookmark Button Add to DZone AddThis Feed Button


Tags: unit-testing agile
Comments
RE: Unit Testing? Why?
Posted 17. Oct 2007 by anonymous
Brilliant punchline ;) I'd like to add up one thing which I've seen several times myself which is that if you create really killer good unit tests you often see "design flaws" or places where your code sucks in regards to user interface towards "consumers" of your code. This is especially true it you write libraries... ;) Also I've heard a couple of guys saying that when YOU are finished creating unit tests for your own code you should go and get another member of your team to write up a dozens of unit tests too since that will further "tighten" your code in regards to code since other people often don't have the same "assumptions" as you yourself have... Thomas :)

 
Legal note: the meaning and content expressed on these blogs don't necassary correspond with the meaning of the legal company Gaiaware AS, but are considered to be personal opinions and expressions.