Friday, July 22, 2011

NUnit - Unit Testing Framework for .Net

Unit Test
A unit test is a piece of source code written by a programmer that is used to test the specific functionality of the project/program.
Unit Testing
This Is a method of testing individual units of code before integrated them together and produced as a big application or system. There are various unit testing frameworks which are being used by developers of .NET community, for example:
NUnit
TestDriven.NET
SharpDevelop
MonoDevelop
NUnit
NUnit is a open source unit testing framework for all the .Net language and its ported from JUnit its version 2.2 it is xUnit based unit testing tool for Microsoft .NET. It is written entirely in C# and has been completely redesigned to take advantage of many .NET language features.
NUnit comes in a Windows setup file and can be easily installed. The setup file is available at http://www.nunit.org url. This web site also contains documentation for various NUnit releases.
NUnit Usage
In order to test a functionality (e.g. database operations), create a public class and annotate it with the attribute [TestFixture]. This attribute tells the unit testing framework that this class has the test methods which can be executed.
Once the test class has been declared, we can implement the test methods. Test methods need to be annotated with the [Test] attribute.
Let’s take an example of simple unit test class:
//Class ‘operations.cs ‘
using System;
public class MathsOperations
{
public static int SumOfNumbers(int number1,int number2)
{
return number1+number2;
}
}
//OperationsTest.cs
using System;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

[TestFixture]
public class MathsOperationTest
{
[Test]
public void SumOfNumbers()
{
Assert.That(MathsOperationTest .SumOfNumbers(2,2), Is.EqualTo(4));
}
}
The name of test is same as the actual method we are testing  but Test method can have any meaningful name.
Executing Test Methods
The above two classes belong to the same .Net project (TestNUnit). Once the project is compiled test cases (test methods) can be executed by running NUnit. NUnit framework can be launched by running nunit.exe from the bin directory available under NUnit installation folder. For example, if NUnit was installed on C:\ProgramFiles then the nunit.exe can be found at C:\Program Files\NUnit 2.5.10\bin location.
NUnit tool can be launched from command line as well. But it requires adding the complete path of nunit.exe. Open command prompt and execute following command:
C:\Set “PATH=%PATH%; C:\Program Files\NUnit 2.5.10\bin”
Now run the nunit from console:
C:\nunit –console TestNUnit.exe
Classic Asserts
Following are the examples of classic assert statement.

Assert.AreEqual(expected, actual [, message])
Assert.Less(x,y)
Assert.Greater(x,y)
Assert.GreaterOrEqual(x, y)

Assert.LessOrEqual(x,y)
Assert.IsNull(object [, string message])

Assert.IsNotNull(object [, string message])
Assert.AreSame(expected, actual [, string message])
Constraint-based Asserts
NUnit 2.4 introduced a new constraint-style of assertions while still supporting the classic-style of assertions that more closely match other XUnit frameworks.

Assert.That(actual, Is.EqualTo(expected)) is equivalent to Assert.That(actual, new EqualConstraint(expected))
Assert.That(10.0/3.0, Is.EqualTo(3.33));
Assert.That(10.0/3.0, Is.EqualTo(3.33).Within(0.01f));
Assert.That(actual, Is.Not.EqualTo(expected)) is equivalent to new NotConstraint(new EqualConstraint(expected))
Assert.That(actual, Is.AtMost(expected)) - This constraint-style assert is equivalant to the Assert.LessOrEqual()
Assert.That(expected, Is.Null);
Assert.That(expected, Is.Not.Null);
Assert.That(expected, !Is.Null);
Assert.That(expected, Is.Empty); - Asserts that expected is an empty collection or string, and fails the test if it is not.

Example 2: NUnit lets you specify two methods to set up and then tear down using the attributes as shown below:

[TestFixture]
public class DataBaseTest
{
private Connection Conn;
[SetUp]                                               
public void TestSetup()
{
Conn = new Connection("oracle" , 1521, user, pw);
Conn.Connect();
}
[TearDown]
public void TestTeardown()
{
Conn.Disconnect();
Conn.Dispose();
}
}

Method annotated with SetUp attribute is called before any test method is called. It can be used to initialize any variables. Method annotated with TearDown attribute is called to finalize or dispose the objects.

0 comments: