Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Writing Unit Tests, a simple multi-step process to getting started

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:

  1. 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.
  2. Code the smells of bad code
    • Lacks comments
    • Overly complex
    • Contains may magic strings, hard coded literals, etc
  3. 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.
  4. 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.

  1. Utility/Helper classes
  2. you wrote, completely understand
  3. Other simple classes
  4. 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
Filed under: ,

[Advertisement]

Comments

photoz wrote re: Writing Unit Tests, a simple multi-step process to getting started
on 04-04-2007 8:01 AM

So what does an ASP.NET developer start writing tests with?

Can you provide some links as to how to get started writing tests for web apps? I do both extensive html programming and business logic, so I would really need to test both ends of the spectrum.

Thanks for the help, always looking to better my skills.

Billy McCafferty wrote re: Writing Unit Tests, a simple multi-step process to getting started
on 04-04-2007 12:35 PM

Use NUnit for your business logic and NUnitAsp to make sure your ASPX pages aren't breaking.  I wouldn't recommend using NUnitAsp for more than running very simple ASPX "smoke tests" since NUnitAsp since it would end up creating a lot of test-code maintenance.  To isolate business logic out of your code-behind pages use Model-View-Presenter.  (Separated Controller and Passive View are two specializations of this pattern.)  With MVP, you can unit test your code that would otherwise be in the code-behind even before you write the ASPX page.

Corey Grusden wrote re: Writing Unit Tests, a simple multi-step process to getting started
on 04-24-2007 10:50 PM

Your article is great for how to plan on attacking existing code with tests, but what about an existing code base that is completely mangled with dependencies?  Where the open/close principle doesn't apply and there are dependencies within almost every function -- what then?  This is something that I've been plagued with on my new project.

The only remedy I had come up with was to write new tests for new classes and extract the algorithms already in the existing code base  -- since that code ,works but isn't easily testable.  After having an object with the smallest unit that works like it should , try to use the new objects in the existing code ASAP and go from there... Is that something that you would concur on?

(This might have been covered under Step #3 with "Simple Classes")

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)