Home About

November 27th, 2008

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

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
{
    [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.

Be Sociable, Share!

Tags: , ,

15 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!

    Cheers,
    Martijn

  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=2.5.0.9122, the system can not find the file
    specified.

  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.

  10. Hakbor Says:

    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 ?

  11. Niranjan Kumar Says:

    It works for me . Thanks !!

  12. tommy w Says:

    Great work, also works with 2010 Express when turning off “Basic settings” ( http://www.nuclex.org/blog/2-gamedev/98-visual-studio-2010 )

  13. Raul Zuluaga Says:

    Very helpful. Thank you.

    It worked: Using VS 2010 Express edition and NUnit 2.5.7

  14. Eric Says:

    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!

  15. gov Says:

    Hey , thanks very much , it was very much clear. I followed step by step and achieved what i want to be.Once again thanks.


Most popular

    Sorry. No data so far.

Recent Comments
  • Juan Romero: Hi there, it’s a neat little class, but I believe you could do the same thing with the WebClient...
  • anthosh: Hey, THank you very much for your tutorial. It was awesome. But i have a problem that i am not able to...
  • bian: how to get passphase if i have encrypt and decrypt string?? Thanks alot
  • Michael: Hi, I really like your post, thanks a lot, it really helped clear up a few things I could not remember how...
  • Bharat Prajapati: i was trying to import keyword dictionary to this plugin which is in csv format, but i get an error...