November 27th, 2008
Setting up NUnit for C# Unit Testing with Visual Studio C# Express 2008 - 15
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Unit testing helps you verify that each individual part of your code is working as expected and keeps doing that as you change your software. You do this by adding small bits of testing code and have the unit testing frame work execute them in order. I am currently writing some code that needs to have a basic unit testing framework and wanted to install the NUnit framework. This post is about how you can install NUnit, use it and run it with / install it for Visual Studio C# 2008 Express. I will leave the nuts and bolts of unit testing to a future post but this post should get you up and running.
Installing NUnit itself is straightforward and only requires a few steps:
- Download the NUnit framework from the NUnit website (download NUnit-2.4.8-net-2.0.msi)
- Install the software on your Windows XP / Vista machine
Creating your first NUnit test
Now we need to create a new project to test if this works, and we need to add a reference to NUnit to be able to use the code.
Click “Project > Add Reference…”

Select “Browse” and navigate to “C:\Program Files\NUnit 2.4.8\bin” and select the nunit.framework.dll.

This allows you to use NUnit.Framework in your programs.
Testing the NUnit Demo code
Below I have included a little bit of demo code that tests if this works properly. The code is mostly borrowed from the NUnit website demo, I have just for the sake of it included a class containing “Main” so that the code actually compiles and runs.
using System;
using NUnit.Framework;
namespace bank
{
public class Account
{
private float balance;
public void Deposit(float amount)
{
balance += amount;
}
public void Withdraw(float amount)
{
balance -= amount;
}
public void TransferFunds(Account destination, float amount)
{
}
public float Balance
{
get { return balance; }
}
}
}
namespace bank
{
[TestFixture]
public class AccountTest
{
[Test]
public void TransferFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Account destination = new Account();
destination.Deposit(150.00F);
source.TransferFunds(destination, 100.00F);
Assert.AreEqual(250.00F, destination.Balance);
Assert.AreEqual(100.00F, source.Balance);
}
[Test]
public void DepositFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Assert.AreEqual(200.00F, source.Balance);
}
}
}
namespace UnitTestDemo
{
public class MyAccountingSoftware
{
public static void Main()
{
bank.Account DemoAccount = new bank.Account();
DemoAccount.Deposit(1000.00F);
DemoAccount.Withdraw(500.50F);
Console.WriteLine("Our account balance is {0}", DemoAccount.Balance);
}
}
}
In the above code the actual unit testing is done by the “AccountTest” class, for simplicity we keep this in the same file but usually you would keep a separate directory with all of your unit tests, separate from the actual code.
Note that because the “WithDraw” function in the code above is poorly implemented (eg. its empty) the Unit test will hopefully (!!) fail. The second unit test in DepositFunds should succeed.
How to run the NUnit GUI runner from within Visual Studio C# Express 2008
We can run NUnit from the Menu option it installed under [Start/Programs/NUnit] but it is more convenient to do so from Visual Studio Express itself.
Visual Studio C# Express allows you to define outside “Tools” which can be run from the menu. This is exactly what we would like to do with the NUnit GUI Runner. The GUI Runner is a tool which loads our program and executes all the unit tests it finds and reports the results in a seperate window.
Click on “Tools > External Tools…” to add NUnit’s GUI Runner.

The following definition allows us to run the tests in our current project:

To run the unit test, simply click “NUnit” from the Tools Menu.
[Click the picture for a larger image]
If everything went correctly you should see NUnit run 2 tests, one of which will fail.










Except where otherwise noted, content on this site is
December 31st, 2008 at 1:14 pm
I downloaded nunit software but i dont know how to install that to my system please send a documentation to install that.
December 31st, 2008 at 1:19 pm
Hi Azad,
NUnit is distributed as an MSI file (Micrrosoft Installer archive) , just click it to install it. That should do the trick!
Cheers,
Martijn
April 6th, 2009 at 10:08 pm
Thank you very much for this quick guide!
I’m working on a ASP.NET project at university and this article has been very helpful. Keep it up!
July 3rd, 2009 at 6:44 pm
When I click “NUnit” from the Tools Menu, it follows:
System.IO.FileNotFoundException, Could not load file or assembly
‘nunit.framework’ , Version=2.5.0.9122, the system can not find the file
specified.
August 10th, 2009 at 11:33 pm
It worked! Thank you!
Using VS 2008 Standard edition and NUnit 2.5.1.
September 22nd, 2009 at 4:42 am
This short (and great) guide is better then the NUnit Documentation self
Respect and thank you!
November 29th, 2009 at 11:54 am
Thank you guys, it was really helpful and clear guide for me
December 16th, 2009 at 6:47 pm
This seems great, but one question is if there is a good idea to have one nunit project per C# project, or if there should be one nunit project for all C# projects.
December 17th, 2009 at 8:55 am
Each project should have its own set of unit tests.
February 22nd, 2010 at 12:48 am
This works fine.
But to get the NUnit to use my current tests (and not the old ones) , it is not sufficient to Build (F6) the test project; I have to Run it (F5).
Is this the best practice, or have I missed something ?
March 1st, 2010 at 2:14 pm
It works for me . Thanks !!
July 13th, 2010 at 12:40 am
Great work, also works with 2010 Express when turning off “Basic settings” ( http://www.nuclex.org/blog/2-gamedev/98-visual-studio-2010 )
August 11th, 2010 at 10:44 pm
Very helpful. Thank you.
It worked: Using VS 2010 Express edition and NUnit 2.5.7
August 30th, 2010 at 3:00 am
Hey, thanks Martijn! I couldn’t figure out how to get this working in VS 2008 Standard, but it turns out I was just referencing the wrong DLL. You saved me a lot of money, as I was about to go by the Professional edition *just* for unit testing. Again, thanks!
March 10th, 2011 at 7:00 am
Hey , thanks very much , it was very much clear. I followed step by step and achieved what i want to be.Once again thanks.