Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
LeftbarAd
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Testing » Testing project using NUnit

Testing project using NUnit

This article is for the begineers who wish to learn the basics of NUnit.

Technologies: .NET 1.0/1.1,Visual C# .NET
Total downloads : 239
Total page views :  19752
Rating :
 4.33/5
This article has been rated :  3 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
NUnitTesting.zip
 
ArticleAd
Become a Sponsor



Nunit is a testing framework for all the .NET languages.

For our note that Nunit is completely written in C#. You can download the Nunit framework from http://www.nunit.org/index.php?p=download .The latest version currently available in Nunit is Nunit 2.2.5.

Now open a new project, add a reference of NUnit.framework.dll. Now you can import the namespace of Nunit, NUnit.framework. I have created a class called NunitTest.

We also need to let the Nunit framework know that this class is a fixture, so we simple add a [TestFixture ()] attribute on top of the class name.

[TestFixture ()]
public class NUnitTest
{
}

Now let us go to method scope. Before a method, use the attribute called [Test ()]

A test in a fixture is defined as a public method that returns void and accepts no parameters, marked with the [ Test()] attribute and with one or more assertions inside.

I'm creating a method as follows...

[Test()]
public void Divide()
{
         int a=100;
         int b=0;
         int c=a/b;
}

Compile and run the application. Now go to Nunit Gui, open the dll file of your project.

You will get the one as shown below for my project

Now click on 'Run' button in the Nunit Gui. You will get the output as shown below.

Thus you could keep track of the exceptions you get in the project. But you have to note that if the above method is handled using Try and Catch statements, you wouldn't have got error in the Nunit.It is shown in the method DivideHandled.

[Test()]

public void DivideHandled()

{

      try

      {

          int a=100;

          int b=0;

          int c=a/b;

      }

      catch(DivideByZeroException ex)

      {

      }

}

Look at the picture below

 

Now we are going to use the Ignore Attribute of the Nunit. It accepts reason as parameter that will be displayed in the Nunit.

[Ignore("No code in this method and is ignored")]
[Test()]
public void IgnoreThisMethodForTest()
{
}

Look at the output below

 

Now we look at the Assert attribute of the Nunit. It contains only static methods that allow you to make sure that a value is not null, equals other values and just send in any Boolean expression you like. You can also send in messages that explain the meaning of this failure.

[Test()]
public void TestAssert()
{
    int a=10;
    int b=20;
    int c=a+b;
    Assert.AreEqual(25,c, "The numbers are'nt equal");
}

Let me show u the output in Nunit when testing the above method

Like the 'AreEqual' methods for the Assert class, there are also methods like AreNotEqual, IsNull, Fail etc.

Some of the other common attributes are

  • You can have a [Setup] and [TearDown] method inside your fixture. The [Setup] runs before each test in the current fixture is run, and the [TearDown] runs after each test.  These methods are very useful for when you want all your tests to use the same set of clean initialized data. In there you can initialize global variables, delete or create needed files and so on. This of [Setup] as an implicit contructor for each test, and have as a destructor for it. Methods that are marked by these attributes should not be marked as tests as well!
  • You can have a [ TestFixtureSetUp ] and [ TestFixtureTearDown ]  methods as well. These methods will be run only once for each test fixture tested. Use them for global initialization and cleanup of resources that can be shared by all tests in that fixture.
  • Another excellent attribute we get is the [ExpectedException] attribute. When a test method is decorated with this attribute and no exception of the type specified in the attribute is thrown inside the test, the test has failed.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Krishnan LN
Krishnan L. N. is working in Merrill Lynch, Chennai, INDIA. He has 4 years experience working with Microsoft .NET technologies.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
NUnitTesting.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
Testing VS 2005 website using NUnit.vinitha5/16/2007
Testing website created in VS 2005 using NUnit.
Reply | Email | Delete | Modify | 
Can we test .exe using NUnit?sandeep1/23/2008
I'm unable to test my application which is in .exe. Using NUnit, We can only test the .dll components or can we test .exe s as well. Pl reply.
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved