I was using NUnit today, and wrote a very simple quick start sample for all of those interested in using NUnit ASAP.
NUnit according to nunit.org is a “unit-testing framework for all .Net languages”. In simple words, you can test your code, and make sure it’s doing what it’s supposed to do. My sample is going to make sure a class called “Addition” adds correctly.
If you don’t have NUnit in your machine visit nunit.org.
I created in Visual Studio two different C# projects with different class libraries (.DLL) in each project. The first project is a simple Addition class:
Addition.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace AdditionDLL
{
public class Addition
{
public int Add(int number1, int number2)
{
return number1 + number2;
}
}
}
This only takes two numbers and adds them. Don’t forget to build your project, so you .DLL gets created.
Our first DLL needs to be in the reference of our second project and also the NUnit.Framework.dll which in my computer is in “D:\Program Files\NUnit 2.4\bin\nunit.framework.dll”
Here’s how the references of my second project looks:

The second class library contains the code that is going to make sure that my Add method in the Addition Class , adds correctly. Here’s the code for my other class
Test.cs
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using AdditionDLL;
namespace NUnitTest
{
[TestFixture()]
public class Test
{
[Test]
public void Scenario()
{
Addition add = new Addition();
int result = add.Add(1,2);
int expectedResult = 4;
Assert.IsTrue(result == expectedResult, "The result needs to be 3");
}
}
}
There are interesting things about this class.
- Before the class starts you will notice this statement: [TestFixture()]. This basically means that this class contains test cases.
- Before the scenario method you will notice this statement: [Test]. This basically means that this method is going to be tested.
- Inside the scenario method, there’s the following line: Assert.IsTrue(result == expectedResult, "The result needs to be 3");. This verifies if result is equal to the expected result. If the assertion fails it displays a message which in this case is that “the result needs to be 3″.
Now I’m going to open NUnit,which should be somewhere in your Start-> All Programs -> NUnit
When you open NUnit create a new project, name it and save it.
After you create a project, under Files click “Open Project” and in here look for NUnitTest.dll, which should be in the Debug folder of your NUnitTest Project (assuming you are following this example ).
After this step you should see something like this:
If you press “Run” you should see something like this:
As you can see my test failed. This is because 1+ 2 = 3, and it’s expecting 4 as the result of the sum.
If you change line 17 in Test.cs to:
int expectedResult = 3;
save the file in visual studio, rebuild solution, open project in NUnit, and choose NUnitTest.dll, and run it again, you should see something like this
Hope this can get you started!