Tuesday, July 26, 2005
NUnit - quick and easy
I searched for a quick and easy introduction to NUnit. There are some nice articles, but they all go into more detail than I want. I just want to take a look at something simple to see what all the fuss is about.
So, here's the smallest introduction to NUnit 2.2 you'll find. Hopefully after you've gone through this procedure you'll be able to see where you can use NUnit in your own projects. You'll then be ready to look at the more complex articles referenced at the end of this one.
Assumptions
- You are using Visual Studio.NET. The version of VS.NET shouldn't matter
- You're using C#. The code is so simple that programmers of other .NET languanges should be able to understand it
- You install NUnit in it's default directory.
The Procedure
- Download and install NUnit. This article was based on NUnit-2.2.0.
- Start Visual Studio.NET
- Create a blank solution.
- Within your blank solution, create a new project called "NUnitTesting".
- Within the "NUnitTesting" project, right click "References", then click on "Add Reference" in the context menu.
- Click the Browse button.
- Navigate to C:\Program Files\NUnit 2.2\bin
- Doubleclick nunit.framework.dll
- Click OK. You have just added the NUnit framework to your project.
- Modify the Class1.cs file so that it looks like this:
using System;
using NUnit.Framework;
namespace NUnitTesting{
public class CalcInt{
public int add(int a,int b){
return a+b;
}
}
[TestFixture]
public class CalcIntTestFixture{
CalcInt testCalcInt;
[SetUp] public void Init(){
testCalcInt = new CalcInt();
}
[Test] public void AddSuccess(){
Assert.AreEqual(2,testCalcInt.add(1,1));
}
[Test] public void AddFails(){
Assert.AreEqual(3,testCalcInt.add(1,1)); // FAILS, 1+1 != 3
}
}
} - Press Shift-Control-B to save/compile the dll
- Start->All Programs->NUnit 2.2->NUnit-GUI
- File->Open
- Navigate to the DLL that was created when you compiled 3 steps ago, and open it.
- Click on the Run button.
- One test should succeed, and one should fail -- hopefully the tests are simple enough to not need explanation.
- That's it! There is a lot more to learn about NUnit, but this article should give you some tools to get started.
Resources
- NUnit documentation - http://www.nunit.org/documentation.html
- Test Driven Development using NUnit in C# - http://aspnet.4guysfromrolla.com/articles/011905-1.aspx