Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Architecture
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
Bing
BizTalk
Book Excerpts
Build and Deploy
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Design Patterns
Development Process
Display Technologies
Distributed Computing
DotNetNuke
DSL
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
Internet Explorer 8.0
Interviews
iPhone
Iron Ruby
Java
Java Script
jQuery
LINQ
Linux
Mac OS X
MDX
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
Network
NHibernate
Object Oriented Development
Odata
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Python
Q&A
Rails
Rake
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Search
Security
Services
SharePoint
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
Tips
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WF
Whitepapers
Windows 7
Windows Azure
Windows Live
Windows Server
Windows Vista
WinForms
Workflow
WPF
XAML
XML
XNA
XSLT



Free Webinar


 


VFPConversion.com

Reader rating:
Click here to read 14 comments about this article.
Article source: CoDe (2004 - November/December)


Article Pages:  1  2 3 4 - Next >


Unit Testing in .NET

You have been given the task of creating some business objects for a new .NET project. The UI has not been created (or designed) yet, so you start coding right away. After creating the first few objects, you decide that maybe you should do some unit testing. How?

You could create a new Windows Forms application using a default form. Then you could add several controls, a lot of code to instantiate your objects and call methods, and then populate your controls to see what was returned. Oh, but wait: what if you call a method with different parameters? What will be returned then?

"
The NUnit framework is a unit-testing framework that works with all .NET languages
"

OK, let's just change some code in the Form and try again. As you can imagine, this could be an ugly cycle. Do you have to create a new Form for each business object? Do you have to change code in the Form for several scenarios? This can turn into a big time consumer.

Is there a better way to test business objects? YES! It comes in a tool called NUnit. It is available (for free) from http://www.nunit.org. NUnit is a unit-testing framework that works with all .NET languages.

This article covers how to perform unit testing using the NUnit framework.

Getting Started

If you want to follow along with the samples presented in this article, go to http://www.nunit.org and download and install the application.

Creating a Business Object

Let's start with a simple bank account business object. It will have a field to store the current balance along with methods to deposit money into the account, withdraw from the account, and transfer money from one account to another. Listing 1 shows the beginning of the code for the Account class. Notice that at this point, only the Deposit method is implemented.

Creating the Test Class

Now you need to test the Deposit method. To do this, add another class to the project called AccountTest. Listing 2 shows the beginning code for the test class.

Here is where the NUnit framework is used to perform unit testing. Notice that the class and methods have attributes. These attributes are part of the NUnit framework as well as the Assert.AreEqual method. To use these, a reference to NUnit.Framework needs to be added to the project and the NUnit.Framework statement added to the class.

The TestFixture attribute is used to flag the class as containing test code. Any class using this attribute must have a default constructor.

The Test attribute is used to flag specific methods of a TestFixture class as a test method. These are the methods that will be executed by the NUnit testing process. These methods cannot have any parameters and cannot return any values. The class will compile, but any methods with the wrong signature will not run during the testing process.

The code in the TestDeposit method is pretty straightforward. It creates a new Account object and then deposits $100 into the account. After that, a call to the Assert.AreEqual method is made. The Assert class in the NUnit framework has several methods for testing values. These are described in more detail later. For now, just look at the AreEqual method. There are multiple overloads for this method. The one you are using compares two float values. The first parameter is the expected value and the second parameter is actual value (or test value). I'll describe the other overloads for the AreEqual method later in this article.

After the initial $100 deposit, deposit another $150 into the account. Then test the balance again to make sure it is $250. Now you are ready to run the test.

&

By: Dan Jurden

Dan Jurden is a Senior Application Developer for EPS-Software Corp. located in Houston, Texas. He is a Microsoft Certified Professional. Dan co-authored the book Creating Visual FoxPro Applications using Visual FoxExpress with Bob Archer, published by Hentzenwerke Publishing. Dan was also the Technical Editor for CrysDev: A Developer's Guide to Integrating Crystal Reports, also published by Hentzenwerke Publishing. He has authored articles published in CoDe Magazine, Fox Talk, MSDN Brazil, SDGN Magazine, and Universal Thread Magazine, dealing with SQL Server, .NET, MySQL, VFP, and other topics. Dan has presented topics at the German DevCon, Essential Fox, SQL Server Live!, SDC Netherlands, and GLGDW conferences. He has been developing client/server applications using SQL Server Crystal Reports for over eight years.

</bio

djurden@cebridge.net

Fast Facts

The NUnit framework is an excellent tool for testing your business objects. It is very easy to construct test cases to test calling methods on a business object with several different parameter values to make sure the methods return the expected results each time.



Listing 1: The Account class to be tested
using System;

namespace Bank
{
   public class Account
   {
      private float _balance = 0.00f;

      public float Balance
      {
         get {return this._balance;}
      }

      public void Deposit(float amount)
      {
         this._balance += amount;
      }

      public void Withdraw(float amount)
      {
      }

      public void Transfer(Account account, float amount)
      {
      }
   }
}


Listing 2: The Test class
using System;
using NUnit.Framework;

namespace Bank
{
   [TestFixture]
   public class AccountTest
   {
      [Test]
      public void TestDeposit()
      {
         // Create a new account and deposit $100
         Account account = new Account();
         account.Deposit(100.00f);
            
         // Make sure we have $100
         Assert.AreEqual(100.00f, account.Balance);

         // Deposit $150
         account.Deposit(150.00f);

         // Make sure we have $250
         Assert.AreEqual(250.00f, account.Balance);
      }
   }
}


Article Pages:  1  2 3 4 - Next Page: 'Performing the Tests' >>

Page 1: Unit Testing in .NET
Page 2: Performing the Tests
Page 3: Condition Tests
Page 4: SetUp/TearDown

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
3.8 out of 5

41 people have rated this article.

      Hacker Halted

 

Devscovery