Click here to Skip to main content
13,598,183 members
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 21 Feb 2008
Licenced CPOL

A Quick Start : NUnit

, 21 Feb 2008
Rate this:
Please Sign up or sign in to vote.
This article is about creating a simple NUnit Test Application


This article shows a quick start about NUnit Testing.


Before getting started, the latest version of NUnit should be installed on your computer. To download the latest version of NUnit, click the link below :

Also, on NUnit's official site, there are some samples and guides to NUnit.

Using the code

To use NUnit in your project, install NUnit to your computer. Then in Visual Studio 2005, create a new Class Library project. After project opens, add new reference.


Browse and find NUnit directory. (As default, it is under "Program Files") Select "nunit.framework.dll" and click "OK". This allows you to add NUnit functionality to your project.

Create a new class, name it as you wish. (I named it "MathHelper") This is the class we want to test. Here you can write some methods. Below is the code I wrote.

public class MathHelper
    public static int Add(int a, int b)
        return a + b;

    public static int Subtract(int a, int b)
        return a - b;

    public static int Multiply(int a, int b)
        return a * b;

    public static int Divide(int a, int b)
        if (b == 0)
            throw new DivideByZeroException();
        return a / b;

Then add another class which will be used by NUnit. You must use some tags, like "[TestFixture]", "[Test]"...etc. You can find more information about these tags on NUnit web site.

public class TestClass
    public void AddingTest()
        int result = MathHelper.Add(4, 5);
        Assert.AreEqual(9, result);

Now, build the project and add it into NUnit GUI to test. After adding, run the test.


The green blocks mean the test succeeded. But the story is not always so happy. Let's look what happens if a test fails. Add some more code which causes test to fail into Test class.

public void DivisionTest()
    int result = MathHelper.Divide(4, 0);
    Assert.AreEqual(0, result);

The code above throws an exception due to attempt for dividing by zero. So, this snapshot shows what happens after running the test.(Don't forget to build project again after adding some code)


As you can see, "AddingTest" succeeded but "DivisionTest" failed. Red bars mean one or more test fails in the test project.

Points of Interest

This quick sample shows how to write simple tests for a bunch of code. Make sure you add reference and also write "using nunit.framework;" row in your .NET project.


This is the first version of the article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Olgun Cengiz
Turkey Turkey
Software Developer

Ege University
Computer Engineering Department

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
pravinpatkar3-Nov-09 5:26
memberpravinpatkar3-Nov-09 5:26 
QuestionNHibernate? Pin
Ravi Bhavnani21-Feb-08 6:28
memberRavi Bhavnani21-Feb-08 6:28 
GeneralRe: NHibernate? Pin
Olgun Cengiz21-Feb-08 7:07
memberOlgun Cengiz21-Feb-08 7:07 
Generalwrong title Pin
quicoli21-Feb-08 6:27
memberquicoli21-Feb-08 6:27 
GeneralRe: wrong title Pin
Olgun Cengiz21-Feb-08 7:09
memberOlgun Cengiz21-Feb-08 7:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.180621.3 | Last Updated 21 Feb 2008
Article Copyright 2008 by Olgun Cengiz
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid