Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Getting Started with Unit Testing : Page 2

Learn unit testing now and find the problems in your code before things get out of control.


advertisement
Automated Unit Testing with .NET
.NET provides a flexible, dynamic development environment that makes automated unit testing easy to implement. In this section, I'll present a simple example of a unit test. I'll show you how to develop a class to remove duplicate elements from an ArrayList. Keep in mind that this is meant to be an educational and informative example; therefore such things as performance will not be taken into account.
.NET provides a flexible, dynamic development environment that makes automated unit testing easy to implement.
The first step is to choose a testing framework.

Choosing a Testing Framework
Before you can begin to write unit tests, you must choose a framework that you will write your tests in. There are several free, open source options for the .NET programmer to choose from: NUnit. A .NET implementation of the popular xUnit framework, originally written by Erich Gamma and Kent Beck, has become a popular choice among developers because of its simplicity, robustness, and availability. It is currently available for most languages, including Java, Smalltalk, and C++. The current version, at the time of this writing, is 2.1, although 2.2 is in beta.

csUnit. Another implementation of the xUnit framework, csUnit is very similar to NUnit. However, at the time of writing, the last release is dated November 09, 2003. I am unsure how active the development on this project is. MbUnit. A recent addition to the .NET testing scene. Among other features, MbUnit provides the ability to simplify your unit tests by relying on graphs and model-based testing. An in-depth overview of MbUnit and its features is a topic for a more advanced article, and will not be discussed here.



Custom. The last framework option for a team is the possibility of building their own testing framework. I advise you and your team to thoroughly evaluate one or more of the above frameworks before you decide to build your own framework. Building your own framework is a complex process and unless there are special circumstances, one of the above frameworks will provide all of the functionality you will need. Another reason to choose an established framework is that it has third-party support. Many of the frameworks above come with infrastructure support, including VS.NET add-ins, NAnt tasks, and GUI test runners. Given its popularity and simplicity, I'll use the NUnit framework throughout this article.

Getting to Know NUnit
Now I will take you though a simple example using NUnit and Visual Studio .NET. First, download the latest copy of NUnit. Go to http://nunit.sourceforge.net and download the latest working release, which at the time of writing is version 2.2. Download the Microsoft Windows Installer .msi version of the release. Once you've downloaded the file, double-click the .msi file to install the NUnit framework. The installer offers you a few directory options, so be sure you remember which directory you install it into. Later you will need to set a reference to the main NUnit assembly.

 
Figure 1: The testing framework is available as a new project.
With NUnit installed, start Visual Studio .NET. From the start page, choose New Project and decide which language you would like to create the sample in. I will provide both C# and VB .NET examples. You are welcome to attempt to use another .NET language, such as J# or Managed C++, as an advanced exercise, however, I recommend following along using C# or VB .NET. In whichever language you choose, select the Class Library option, as shown in Figure 1. Next, set a reference to the main NUnit assembly, nunit.framework.dll. This assembly contains the core NUnit attributes you will use to markup and create your test classes.

Right-click on References in the project you created and select Add Reference. With the .NET tab selected, click the Browse button. Navigate to the Bin directory where you installed NUnit to. Select the nunit.framework.dll to add a reference to it. Once you've completed the Add Reference dialog box, nunit.framework.dll should be listed in the Selected Components section. Click OK. You now have a reference to the NUnit assembly and you can use it in the project.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap