Introduction
This article shows a quick start about NUnit Testing.Background
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 :http://www.nunit.org/index.php
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;
}
}
[TestFixture]
public class TestClass
{
[Test]
public void AddingTest()
{
int result = MathHelper.Add(4, 5);
Assert.AreEqual(9, result);
}
}
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.
[Test]
public void DivisionTest()
{
int result = MathHelper.Divide(4, 0);
Assert.AreEqual(0, result);
}
As you can see, "AddingTest" succeeded but "DivisionTest" failed. Red bars mean one or more test fails in the test project.