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 Visual Studio 2008 Professional.

Unit Testing

Unit tests validate that individual units of code are working correctly. 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 appropriate parameters and checking whether the method returns results as expected.

Why use unit tests?

  • 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 do not 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 shown in the figure below:

Then a “Create Unit Test” window is displayed showing the selected method to test, of course, we can choose additional methods, this will add additional unit tests.

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 then a 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 “AccountTest” will be generated, as shown in the next figure.

Let us take a look at the AccountTest Class. We can notice the following:

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

We should modify the DepositTest() as follows to simulate the actual behaviors of the code.

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

So 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, then All Tests in Solution.

The Test Result window indicates that the method passed the test, and there is no error message, if the method failed to pass the test then we might have made some mistake and I should modify the source code of the Deposite() method until it passes the test.

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