Unit Testing Old Code
28-Jan-2004 Filed in: Software
Development
If you are new to unit testing, but the source base
is old, the sheer size of code to test will seem
daunting. Here are some strategies for adding unit
testing to old code.
- Test code before refactoring or optimizing – The purpose of refactoring and optimizing is to make the code do the exact same thing but in a different way. It is perfectly suited to being unit tested since tests you write at the beginning of the change need to pass at the end. And, the unit tests will let you make more aggressive changes.
- Test code while learning what it does – When you're trying to learn a body of code, a unit test is a good way to check your assumptions about how the code works. This strategy also works well in training a new developer—explain the code by writing new tests for it together.
- Before fixing a bug, write a failing test – This is the perfect time to add a test since reliably reproducing the bug is a good first step. The beauty of this is that now the bug won't return.