Tuesday, February 10, 2009

Poll - Unit Tests, The Benefits

Unit tests are simply tests that exercise small amounts of isolated functionality. That is, if you have a function that adds two numbers, instead of depending on running a user function that eventually calls the function, exercise the function directly. This often requires the use of mock objects that pretend to be things that the function needs in order to test the function in isolation from other functions that it depends on. Unit testing and refactoring are often done hand in hand.

The cost of unit tests is in writing the tests themselves and refactoring code as new functionality is introduced to keep the unit tests testing at the right level. The benefit is that you can easily test changes quickly to find simple problems before doing more thorough and slower testing. It also provides a good safety net for refactoring, gets developers more involved in testing, and usually improves the design of the software.

So, if you were starting a new software company with your own hard-earned cash, would you use unit tests or wouldn't you?

Next: Poll - Stand-up Meetings, The Benefits


Paul W. Homer said...

Honestly, I leave the degree of unit testing up to the individual programmers. Some may need techniques like TDD, or better testing. Some, with more experience or writing easier code may not need it at all.

Full system integration testing should set a reasonable level for quality, unit testing can often just be excess code that needs to be maintained.

Initially, unless you have lots of money, the idea is to get to the final product as fast as possible. It's OK to change this later, once the product starts earning ....

Damon Poole said...

Hi Paul,

When done well, unit testing can reduce the amount of system testing that you need to do. It also means that developers can run a small set of tests related to the area they are changing to get feedback fast in their environment rather than have to wait for some later date when system testing is done. Plus, with all the changes in the meantime, the connection between change,problem,fix is long. Unit tests mean you find "dumb" problems right away and nobody else has to be impacted by them.

Unit tests also create a safety net for the future. You may be an amazing coder, but everybody makes mistakes. A month from now when you are changing code that has unit tests and you make a mistake, there's a much better chance you will find out.

I hear you on the $$$ side. I agree. But as Uncle Bob (Martin) says, "to go fast, go well."