Go to DotNetFunda.com
 Online : 623 |  Welcome, Guest!   Login
 
Home > Articles > .NET Framework > 5 simple steps to execute unit testing using NUNIT

  • Download the OOPS, ASP.NET and ADO.NET online training sessions videos and related document for FREE, click here.

Submit Article | Articles Home | Search Articles |

5 simple steps to execute unit testing using NUNIT

 Posted on: 11/22/2009 1:05:15 AM by Questpond | Views: 5278 | Category: .NET Framework | Level: Beginner | Print Article
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.

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

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.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:0 year(s)
Home page:http://www.questpond.com
Member since:Wednesday, September 03, 2008
Level:HonoraryPlatinum
Status: [PanelMember] [Member] [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. You can visit about my organization at http://www.questpond.com and also enjoy the videos uploaded for Design pattern, FPA , UML ,Share Point,WCF,WPF,WWF,LINQ, Project and lot. I am also actively involved in RFC which is a financial open source madei in C#. It has modules like accounting , invoicing , purchase , stocks etc.
 Latest post(s) from Questpond

   ◘ 4 steps to consume web services using Ajax (Includes Video tutorial) posted on 2/20/2010 9:16:32 AM
   ◘ 7 simple steps to run your first Azure Queue program posted on 2/11/2010 12:48:49 AM
   ◘ Simple 5 steps to expose WCF services using REST style posted on 2/1/2010 5:50:38 AM
   ◘ SQL Query Optimization FAQ Part 1 (With video explanation) posted on 1/28/2010 5:34:05 AM
   ◘ Simple 7 steps to run your first Azure Blob Program posted on 1/24/2010 7:08:28 AM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)