Using Unit Test in Visual Studio 2008 Professional


  Go to Articles List    Print This Page    Submitted by Bashar Kokash    Posted on July 20th, 2008

Introduction

When testing applications, there are many test options that all aim to check the software to verify that it satisfies its requirements and also to detect defects. This article will show how to use unit tests in VS 2008 Professional.

Unit Test

Unit test is the test that validates that individual units of code are working properly. It is called "unit test" because the test is focusing on a small discrete unit of code and that unit is the method. Unit testing is used to test other source code by directly calling the methods of a class and passing the appropriate parameters and checking whether the method returns results as expected.

Why Unit Test?

• To ensure the quality of code by reducing the number of errors
• To make sure that code works as expected and meets the software requirements
• To make sure that changes to others' source code don't affect the logic of the method

Using Unit Test in Visual Studio 2008 Professional

The following is a typical Account class that has the basic methods that could be applied on a bank account:



To add a test unit for a specific method, just right-click on the method and choose "Create Unit Tests" from the menu as in the following:



Then a "Create Unit Tests" window is displayed showing the selected method to test:



By clicking OK, a new Test Project will be created. This project will contain classes for each selected class. If the selected methods are from different classes, a new Test Class will be created for each class.

Each Test Class contains test units for the selected methods. For this example, a Test Class named as "AccountTest" will be generated as shown in the next figure:



If we take a look at the AccountTest Class, we can notice the followings:

• The class is using the "Microsoft.VisualStudio.TestTools.UnitTesting" namespace
• The class is decorated with [TestClass()] directive
• The unit test is decorated with [TestMethod()] directive

We should modify DepositTest() to simulate the actual behaviors of the code:



The Assert class has many static methods that are used in unit testing. I have used the AreEqual() method which tests the actual result of the method against the expected result.

The actual result of the Deposit method is stored in the balance property, while the expected result should be the same as the amount of deposit which is 300F.

Now, we are ready to run the unit test from the Test menu. Select Run, and then All Tests in the Solution.



The Test Results window indicates that the method passed the test and there is no error message. If the method fails to pass the test, then I should modify the source code of the Deposite() method until it passes the test.

Conclusion

There are a lot to see and discover about unit testing in Visual Studio 2008 Professional, but I hope this article helps you to understand the basics so more complex scenarios would be easier.

  Go to Articles List    Print This Page    Send Feedback to Bashar Kokash

© 2007 - Some Rights Reserved. - Terms and Credits - Contact Form - Advertising - Admin Login - About - News