What is Nunit?
Please feel free to download my free 500 question and answer e-Book which covers .NET , ASP.NET , SQL Server , Share point, WCF , WPF , WWF, LINQ, Silverlight, .Net Best Practise@ http://www.questpond.com .
Definition
• NUnit is an automated unit-testing framework for all .Net languages.
• Initially ported from JUnit, which is a unit testing framework for Java applications.
• It is written entirely in C# and has been completely redesigned to take advantage of many .NET language features.
• NUnit brings xUnit to all test are written in .NET supported language e.g. C#, VC, VB.NET, J# etc.
• NUnit is an open source free to use with your .NET projects.
• NUnit provides a class library which includes some classes, features and methods to help you write test scripts.
• NUnit provides user interface application to execute the test scripts and displays result
• NUnit does not create any test scripts by itself. We have to write test scripts and use NUnit tools and classes to make the unit testing easier and display the result as a success or failure.
Features of NUnit
• Assertions
• Attributes
1. Assertions
Definition
Assertions are central to unit testing in any of the xUnit frameworks, and NUnit is no exception. NUnit provides a rich set of assertions as static methods of the Assert class.
The most commonly used assertions.
• Equality Asserts
• Identity Asserts
• Comparison Asserts
• Type Asserts
• Condition tests
• Utility methods
• StringAssert
• CollectionAssert
• FileAssert
• Equal Constraint (Available NUnit 2.4)
• Same As Constraint (Available NUnit 2.4)
• Condition Constraints (Available NUnit 2.4)
• Comparison Constraints (Available NUnit 2.4)
• Type Constraints (Available NUnit 2.4)
• String Constraints (Available NUnit 2.4)
• Collection Constraints (Available NUnit 2.4)
• Compound Constraints (Available NUnit 2.4)
• Custom Constraints (Available NUnit 2.4)
2. Attributes
• Category
• Description
• ExpectedException
• Explicit
• Ignore
• Platform
• Property
• SetUp
• SetUpFixture
• Suite
• TearDown
• Test
• TestFixture
• TestFixtureSetUp
• TestFixtureTearDown
Getting Started with Nuint
You can download Nuint from
http://www.nunit.org
Here you will find documentation, downloads, tutorial sample on Nuint
• Install Nuint in your PC
• Go to start all programs nuint

Figure: GUI of Nuint
Working of Nuint with C#
Step 1
Install Download Nuint which is an open source .
Step 2
Install Nuint in your system
Step 3
Open your c# project and Create c# project then write a bussinessLogic in Class.cs file
For e.g.:
Create Invoice class for customer placing an order
Step 4
Add References in Project
Browse references from
C:\Program Files\NUnit 2.5.2\bin\net-2.0\framework
i.e. nunit.framework.dll
Step 5
Add another cs file in same project.
Add namespace of Nuint
i.e. using NUnit.Framework;
create class TestNuint
above class TestNuint add attribute
[TestFixture]
i.e.
[TestFixture]
class TestNuint
{
}
Step 6
Create TestMethod to test businesslogic
i.e
public void TestInvoice()
{
}
Above this method Add attribute [Test]
i.e
[Test]
public void TestInvoice()
{
}
Step 7
Create object for bussinessLogic
i.e
clsInvoice objInvoice = new clsInvoice();
create your own expected result variable
i.e
//Expected Result
double _expectedTotalCost;
string _expectedProductDetails;
string _expectedInvoice;
string _CustomerName = "";
string _ProductName = "";
double _ProductCost = 0;
double _Quantity = 0;
Assign same values to both your expected result variable and objInvoice varibles
i.e
//Set Expected Result Variables
_CustomerName = "Shyam";
_ProductName = "L G Color TV";
_ProductCost = 22000;
_Quantity = 2;
_expectedTotalCost = _ProductCost * _Quantity;
_expectedProductDetails = _ProductName + " Cost of " + _ProductCost;
_expectedInvoice = _CustomerName + " For product " + _ProductName + " Quantity " + _Quantity + " Total Cost " + _expectedTotalCost;
//Set same values to object of class businesslogic
clsInvoice objInvoice = new clsInvoice();
objInvoice.CustomerName = "Shyam";
objInvoice.ProductName = "L G Color TV";
objInvoice.ProductCost = 22000;
objInvoice.Quantity = 2;
_actualTotalCost = objInvoice.ProductCost * objInvoice.Quantity;
_actualProductDetails = objInvoice.DisplayProductDetails();
_actualInvoice = objInvoice.DisplayInvoice();
//Now to Compare two result
//add
Assert.AreEqual(_expectedInvoice, _actualInvoice);
Assert.AreEqual = It compares two objects makes special provisions so that numeric values of different types compare as expected.
Finally Build Solution
Step 8
Open your Nuint GUI Click File Create Nuint Project
then add assembly

Select your Build Solution .dll file from C# Project
i.e
E:\nunit\Nuint2\Nuint2\bin\Release

Finally Click on Run Button to test your c# project
Test Result
If test runs successfully then it will display

This test successful
Assert.AreEqual(_expectedInvoice, _actualInvoice);
_expectedInvoice and _actualInvoice values are equal
If Test is unsuccessfull having some errors then it will display

This test fails due to
Assert.AreEqual(_expectedInvoice, _actualInvoice);
_expectedInvoice and _actualInvoice values are unequal
Source Code
YOu can download the Soure code from top of this article.