Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 30036 |  Welcome, Guest!   Register  Login
Home > Articles > .NET Framework > 5 simple steps to execute unit testing using NUNIT

5 simple steps to execute unit testing using NUNIT

1 vote(s)
Rating: 1 out of 5
Article posted by Questpond on 11/22/2009 | Views: 25112 | Category: .NET Framework | Level: Beginner red flag


Unit testing is validation and verification methodology where the developers test the individual units of source code. In this tutorial we will try to understand how we can do unit testing using NUNIT framework. This tutorial we will try to understand the 5 important steps to do unit testing using NUNIT.

5 simple steps to execute unit testing using NUNIT

 

Introduction and Goal
 

Unit testing is validation and verification methodology where the developers test the individual units of source code. In this tutorial we will try to understand how we can do unit testing using NUNIT framework. This tutorial we will try to understand the 5 important steps to do unit testing using NUNIT.

Please feel free to download by 400 .NET interview question free EBook from http://www.questpond.com  which covers sections like basic .NET, Design patterns,3.5,SQL Server,UML and lot of other sections.
 

The example which we will test
 

Below is a simple class ‘clsInvoiceCalculation’ which calculates total cost by taking per product cost and number of products as input.
 

public class clsInvoiceCalculation
{
public int CalculateCost(int intPerProductCost, int intNumberOfProducts)
{
return intPerProductCost * intNumberOfProducts;
}
}

Let’s say we want to execute the below test case on the above class using NUNIT.
 

Per product cost Number of products  Expected output
 
10  20 200 ( test case passed)


The 5 basic steps to execute the unit test using NUNIT
 

Step 1:- The first step is to download to the NUNIT software from http://nunit.org/index.php?p=download 

Step 2:- Create a new C# class project and add reference to “C:\Program Files\NUnit 2.5.2\bin\net-2.0\framework\nunit.framework.dll”. We also need to add reference to the class project which we are testing i.e. the invoice project.
 

Step 3:- Add reference to the NUNIT and your invoice project in your code.
 

using NUnit.Framework;
using Invoice;

Step 4 :- We need to create a simple class with ‘TestFixture’ as attribute. We then to create a method attributed with ‘Test’ which will have our test.
 

You can see in the above figure how the ‘TestInvoiceCalculation’ method creates an object of ‘clsInvoiceCalculation’ , passes the values and checks if the returned values have the expected results. ‘Assert’ command is used to check if the expected output and the results return match.

Step 5:- Once we are finished with our test case we need to compile the same to create a DLL. Once the DLL compilation is done go to program files – nunit and click on nunit. Now the nunit user interface will open. Click on file – open project and select your test DLL.

Once you select your DLL you will get a screen as shown below. On the right hand side you can see your test class with test function which has the test case. Check the test function and hit run. Now if the test case passes you will see a full green color or else you will see a red color with details of why the test case fails.
 

Screenshot when the test case fails.



 

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:0 year(s)
Home page:http://www.questpond.com
Member since:Wednesday, September 03, 2008
Level:Starter
Status: [PanelMember] [Member] [Microsoft_MVP] [MVP] [Administrator]
Biography:

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers

>> Write Response - Respond to this post and get points
Related Posts

N.B, This article is for newbies. I remember my early days of my programming,The most battle i used to have was how to display my data on my form, yes i used GridViews, but find limitations when dealing with data.

Steps to create Application Pool

This article describes how to create XML file and also shows various ways of reading XML File in .NET using XmlTextReader, XmlDocumenet, XPathDocument, DataSet and XmlDataSource control.

Value types are derived implicitly from the System.ValueType base type. Value types are variables that contain their data directly instead of containing a reference to the data stored in memory. Instances of value types are stored in memory called the stack, where the runtime can create, read, update, and remove them quickly.

The purpose of this article is to create a simple Task Management System(TMS) that will help the user to create a task, edit the task and to view the same by using MEF 4.0, WCF , Entity Framework 4.0 with some architectural patterns etc.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/28/2012 11:59:16 AM