Rabid Data Pros

Providing C# / SQL Server Expertise

Unit testing with NUnit in Visual Studio 2008

Unit Testing with NUnit

For years software development amongst smaller development shops consisted of a few simple steps.  You know the drill: Spec it out, code it, hammer on it as much as possible, deliver it.  The result – LOTS and LOTS of bugs!  Some of these should have been caught at development time.  Most, though, should have been caught during the pre-development cycle.  How?  With a simple to implement practice called Unit Testing.

What is Unit Testing?

Unit Testing is simply the process whereby code is created in small, testable blocks that are tested individually.  These blocks are usually broken down into objects, or classes. This article will attempt to show how code can be created and designed to help make it impervious to errors caused by the addition of future code.  If you’ve ever coded in the old, traditional manner, then you are well aware of the problems associated with adding code for features.  BUG and more BUGS!

Getting Started

First you will need to download and install NUnit.  This can be done at:  http://www.nunit.org 

Great you’ve installed it. 

Follow these steps to make it easily accesible in Visual Studio 2008.

  1. Add an External Tool as follows:
    1. In Visual Studio, chose Tools – External Tools from the menu
    2. Click the add button
    3. Fill in the fields with:
    4. Title: &NUnit
      command: C:\Program Files\NUnit 2.4.8\bin\nunit.exe
      Arguments: $(TargetName)$(TargetExt)
      Initial Directory: $(ProjectDir)\bin\Debug
  2. For easy accessibility, add a new tool bar as follows:
    1. Create a new c# project and migrate to editor window.  This makes the correct toolbar show up.
    2. Click the Toolbar options (see Figure 1)
    3. Click the Add or Remove buttons, Customize option
    4. Under the Toolbars tab, click the “New”.  This will cause a toolbar to start floating around.
    5. Under the Commands tab choose the Tools category.
    6. Under commands, find the External Tools section.
    7. This is the tricky part.  When you added the external tool for NUnit in step 1 above, if it was the 3rd option added, it will be External Command 3.
    8. Drag the appropriate External Command to the ToolBar you created above.  When you close the “Customize” window, the name on the Toolbar should read NUnit.
    9. Drag that ToolBar up onto the toolbars section at the top of the Visual Studio IDE so that it docks.  (see Figure 2)

If you click on the toolbar button labeled “NUnit” you should get an error message “This assembly was not built with any known testing framework”.  That’s ok.  That just means that NUnit is setup correctly, but that you don’t have a project created to use them. YET!

In my next article, I’ll discuss how to create a project and your first unit test.


Figure 1

Customize Button

Figure 2

NUnit Toolbar

November 29, 2008 - Posted by | Unit Testing | , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: