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!
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.
- Add an External Tool as follows:
- In Visual Studio, chose Tools – External Tools from the menu
- Click the add button
- Fill in the fields with:
- Title: &NUnit
command: C:\Program Files\NUnit 2.4.8\bin\nunit.exe
Initial Directory: $(ProjectDir)\bin\Debug
- For easy accessibility, add a new tool bar as follows:
- Create a new c# project and migrate to editor window. This makes the correct toolbar show up.
- Click the Toolbar options (see Figure 1)
- Click the Add or Remove buttons, Customize option
- Under the Toolbars tab, click the “New”. This will cause a toolbar to start floating around.
- Under the Commands tab choose the Tools category.
- Under commands, find the External Tools section.
- 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.
- 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.
- 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.
No comments yet.