One of the things I often hear when I am preaching the
gospel of writing Unit Test's is that I have so much existing code, I don’t
know where to start.
In order to help others that have this dilemma I thought
I would put together a simple multi-step plan to help you become one of the
converted.
The key is to start slow, like most every thing else, you don’t
want to jump in the deep end head first before you know how to swim.
Step 1) Survey your code base:
Start off by look over your existing code base. You will want to grab a note pad and a pen,
you are going to want to take notes.
Here are a few tips for what type of code to look for:
- Code
you wrote, completely understand
- Means
you should be familiar with this code and understand it and this should help you write effective and simple unit tests.
- Code
the smells of bad code
- Lacks
comments
- Overly
complex
- Contains
may magic strings, hard coded literals, etc
- Code
that is of high value and maybe has little to no comments
- Business
value that is core to your application
- Data
access/translation methods
- General
code that is of value, but is either hard to read or has no comments.
- Code
that is of high value and maybe has little to no comments
- Could
be utility/helper classes
- Could
be factories
- Could
be simple validation methods
Step 2) Create an action plan:
Now that you have finished surveying your code, it is time
to create a plan. I am assuming that if you
are reading this, it is because you are not 100% comfortable with Unit Tests or
are new to the concept. With this being
said, I would prioritize my action plan in the following order.
- Utility/Helper
classes
- you wrote, completely understand
- Other
simple classes
- High
Value code (with decent comments) (not likely to be executed in the first
iteration)
For your first go around I would start with the simple
stuff. The goal here is to learn and to
build confidence.
Step 3) Execute your action plan:
Now that you have an action plan, it is time to execute the
plan. This is the most important step in
the process. In order to not overload
yourself I would suggest that you start slow.
Set aside a slice of time each day (10 minutes, an 1 hour, you decide) to
write a few tests. This will allow you
to get the ball rolling slowly and build up your confidence. Now don’t get discouraged if it is slow going
at first. The key is you are writing
your tests. YOU ARE MAKING YOUR
APPLICATEION BETTER.
Step 4) Reflect on what you have accomplished:
After about 3-4 weeks, it is time to step back and take a
look at what you have accomplished. By
this time you should have a better understanding of your code base, where your
code smells and most importantly a better understanding of how to write unit
tests. You should feel good right now, you
should feel like you are growing both in terms of the quality of your
application and your knowledge as a developer empowered with unit tests.
By the way, how many bugs (or potential) bugs have you fixed
along the way :)
Step 5) Start the process over:
Ok, so you laid out your plan, you executed your plan and
you have reflected on your accomplishments.
It is time to start the whole process over. Remember, you said you have a large code base
that needs to be tested.
This time when you create your action plan attempt to write
test for more difficult code or code that is of high value.
Hope this helps, let me know.
Posted
04-03-2007 7:48 PM
by
Derik Whittaker