Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » Testing » Introduction to Unit Testing Framework of VS 2008

Introduction to Unit Testing Framework of VS 2008

This article gives an introduction to in-built Unit Testing Framework of Visual Studio 2008.

Author Rank :
Page Views : 30676
Downloads : 461
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
Sample Code.zip
 
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Unit Testing is common activity in our software development. Before release of Visual Studio 2008, most of us prefer to use NUnit for writing unit test cases.

NUnit Framework provides lot of features that will make writing test cases easier.
But, still it is having some drawbacks in it. Here, I will list some of the drawbacks of NUnit Framework followed by an introduction to new built-in testing framework of VS 2008.

Drawbacks of NUnit Framework:

  • Installation of NUnit comes in a separate MSI.
  • No Integration with Visual Studio.
  • Requires writing test cases manually. 
  • No auto generation of code.
  • Requires opening separate window (NUnit Console or GUI) to execute test cases.
  • Ordering of test cases execution not available.
  • No inbuilt feature to debug test cases.
  • No inbuilt feature to enable/disable test cases.
  • No inbuilt feature to give additional information of test cases like stack trace, Trace Information etc.
  • No inbuilt feature to sort test cases based on Computer Name, Class Name and Host Type etc.

Now, let's drill into new unit testing framework of VS 2008. Open your VS 2008, Create a New Project:

Project Type: Class Library
Project Name: TargetApplication

Next, add some sample methods to be tested. For simplicity sake, I am adding simple methods to Class1:

            public int Square(int a1)

                   {

                             return a1 * a1;

                   }

                   public int AddNumbers(int a1, int b1)

                   {

                             return a1 + b1;

                   }

So,we are going to test this methods by using Testing Framework of VS 2008. Now,right click on solution and select Add New Project with

Project Type : Test Project present in tree node Test.
Project Name : TargetApplicationTest.

We are ready with a Test Project.Than, right click on Test Project and select Add New Test.It will show a dialog box to select template for the test cases.As a beginner, we will use Unit Test Wizard and click Ok.

It will popup a selection window,where we can add Assembly or class for which test cases should be generated.Check TargetApplication.Class1 and click Ok to generate test cases automatically for all methods present in Class1.

Now, you can see Class1Test.cs in Test Project.In that,it already added all test methods.It will set default values to parameters.We can change those values and test.After removing unwanted code (Assert.Inconclusive stmt and comments) and changing default values, the test methods will be like this:

            [TestMethod()]

                   public void SquareTest()

                   {

                             Class1 target = new Class1();

                             int a1 = 5;

                             int expected = 25;

                             int actual;

                             actual = target.Square(a1);

                             Assert.AreEqual(expected, actual);

                   }

 

            [TestMethod()]

                   public void AddNumbersTest()

                   {

                             Class1 target = new Class1();

                             int a1 = 10;

                             int b1 = 15;

                             int expected = 25;

                             int actual;

                             actual = target.AddNumbers(a1, b1);

                             Assert.AreEqual(expected, actual);

                   }

We are ready with test cases.Inorder to run those, goto Test menu --> Run --> All Tests in Solution or use shortcut Ctrl+R,A.

Now,it will show result of test cases execution in Test Results window.If you are unable to see this window, just goto Test --> Windows --> click on Test Results.

Inorder to debug any test cases, just check that test case in Test Results Window.Keep a breakpoint in that test case.Now, click on Debug --> Debug Checked Tests in Test Results window.

We can group the test cases by selecting Group By combobox in Test Results window based on any criteria like Class Name,Duration etc.

We can see the history of test cases executed sofar by selecting All in the combobox next to Run Details button.

In this article, I given a small introduction to VS 2008 Testing Framework.In coming artciles, I will explain core features of this Framework.

I am attaching code for reference. I hope this code will be useful for all.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Sateesh Arveti
I hold Bachelors degree in Computer Science along with MCSD,MCTS and MVP for the year 2009. Areas of Interest: C#, WPF, WF, silverlight, ASP.NET, Oracle and SQL Server.

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.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
Good job by Bechir On August 13, 2008
Good job
Reply | Email | Modify 
Good job by Bechir On August 13, 2008
Good job
Reply | Email | Modify 
Nice Kick Start!! by Rohit On September 10, 2008
Hi Sateesh, Thanks for a nice article on intro to VS 2008 testing. Please send me the links for more detailed information on how to adopt this framework for a application. Thanks Rohit
Reply | Email | Modify 
Introduction to Unit Testing Framework of VS 2008 by Ana On December 7, 2010
Thank you!
This is one of the best explanations of unit testing.
But what is next?
Do you post something else? Please, send me a mail.
Best
Reply | Email | Modify 
thanks by du On April 4, 2011
thanks very much
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.