RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


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. This article shows you how.

ou 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?

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 NUnit. 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.

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.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date