Home About

November 27th, 2008

Setting up NUnit for C# Unit Testing with Visual Studio C# Express 2008 - 9

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:

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
    public class AccountTest
        public void TransferFunds()
            Account source = new Account();
            Account destination = new Account();

            source.TransferFunds(destination, 100.00F);
            Assert.AreEqual(250.00F, destination.Balance);
            Assert.AreEqual(100.00F, source.Balance);


        public void DepositFunds()
            Account source = new Account();
            Assert.AreEqual(200.00F, source.Balance);


namespace UnitTestDemo
    public class MyAccountingSoftware

        public static void Main()
            bank.Account DemoAccount = new bank.Account();
            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.

Be Sociable, Share!

Tags: , ,

9 Responses to “Setting up NUnit for C# Unit Testing with Visual Studio C# Express 2008”

  1. azad Says:

    I downloaded nunit software but i dont know how to install that to my system please send a documentation to install that.

  2. Martijn Says:

    Hi Azad,

    NUnit is distributed as an MSI file (Micrrosoft Installer archive) , just click it to install it. That should do the trick!


  3. Michael Says:

    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!

  4. JP Says:

    When I click “NUnit” from the Tools Menu, it follows:
    System.IO.FileNotFoundException, Could not load file or assembly
    ‘nunit.framework’ , Version=, the system can not find the file

  5. B7 Says:

    It worked! Thank you!

    Using VS 2008 Standard edition and NUnit 2.5.1.

  6. VuuRWerK Says:

    This short (and great) guide is better then the NUnit Documentation self :)
    Respect and thank you!

  7. Abera Says:

    Thank you guys, it was really helpful and clear guide for me

  8. Hakbor Says:

    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.

  9. Martijn Says:

    Each project should have its own set of unit tests.

Most popular
Recent Comments
  • ARS: great plugin! I love it! but, it will be so nice if you can add attribute ‘title’ as one of...
  • Nelson: Saved me from doing it myself. Good article.
  • andy: i am currently playing taiwanese server wow in 奈辛瓦里(PVP) and i would like to realm transfer to somewhere there...
  • berties: any english speaking playing on a taiwanese server?
  • web application development: has C# search volume really so constant over the years? really surprising.