What is Nunit and How we can use the NUnit for Unit testing in .NET

No.of Views1364
Bookmarked0 times
By  gurumatrix2004   On  16 Feb 2010 03:02:51
Tag : ADO.NET , How to
What is Nunit and How we can use the NUnit for Unit testing in .NET
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com



•    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

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

Image Loading

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 

using NUnit.Framework;


create class TestNuint
above class TestNuint add attribute [TestFixture]

class TestNuint


Step 6
Create TestMethod to test businesslogic

public void TestInvoice()

Above this method Add attribute [Test]

public void TestInvoice()


Step 7
Create object for bussinessLogic

clsInvoice objInvoice = new clsInvoice();

create your own expected result variable

//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


//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


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

Image Loading

Select your Build Solution .dll file from C# Project


Image Loading

Finally Click on Run Button to test your c# project

Test Result

If test runs successfully then it will display

Image Loading


Assert.AreEqual(_expectedInvoice, _actualInvoice);

_expectedInvoice and _actualInvoice values are equal. If Test is unsuccessfull having some errors then it will display

Image Loading

This test fails due to,

Assert.AreEqual(_expectedInvoice, _actualInvoice);

_expectedInvoice and _actualInvoice values are unequal. 

Sample Project Source

Download source files -216 kb

Sign Up to vote for this article
About Author
Occupation-Software Engineer
Member Type-Fresh
Joined date-11 Aug 2009
Home Page-http://www.onlinebuff.com
Blog Page-http://gurumatrix.wordpress.com
Gurunatha dogi is a software engineer by profession and an Information Technology Graduate from Mumbai University. He is well known to different programming languages like VBScript, Oracle, ASP, XML, LINQ, WPF, WCF, Crystal Reporting, Silver-Light, Ajax, PHP/MYSQL, MYSQL, CSharp, DOT.NET, Design Patterns. Gurunatha Dogi is also a technical writer on Onlinebuff.com. The website covers all the technical interview related topics. After going through long journey of coding in weekend he likes to play cricket, carrom, football with their close friends in the neighborhood. Happy Coding..!
Other popularSectionarticles
There is no comments for this articles.
Leave a Reply
Display Name:
(not display in page for the security purphase)
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top