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


Improve Code Quality with Unit Testing in Visual Studio Team Edition

Learn to use Visual Studio Team Edition's built-in unit test generation and code coverage analysis capabilities to ensure all your code gets tested.

any Agile development methodologies require testing source code early and often. Because inefficient software testing methods result in code that is not tested often, it is critical to adopt a testing strategy that is both as automated as possible and also lets developers author tests easily and quickly. Visual Studio Team Edition provides software developers with an integrated tool that works well not only with Visual Studio but also with the supporting Team Foundation Server system of tools.

Unit tests provide the most basic way to automatically test software. Development teams using Test Driven Development (TDD) write unit tests before writing the code, which helps to define when the software is complete—when the software passes all the tests, it's complete. Taking this paradigm a step further, code that is not exercised by the unit tests is superfluous, because it's not necessary to satisfy the software requirements. Other development teams write unit tests after completing development, using the tests as a way to verify that the code still operates properly after future changes to the software.

Other than just testing the software, another key feature of a good unit testing framework is code coverage analysis. After running a battery of unit tests against a piece of software, you should be able to view the original source and see which lines of code were exercised during the test. If you find code that was not tested, you should consider either adding unit tests to exercise the untested code—or if you're using TDD, removing it.

There are many unit testing frameworks available for Visual Studio. Most notably, the widely used NUnit works with all .NET languages. You write NUnit tests in much the same way as Visual Studio unit tests. Other commercially available unit testing solutions available include tools that supplement the features of Visual Studio's unit testing tools. While not best of breed, the unit testing tools provided by Visual Studio are a great way to get started with unit testing your .NET code.

Figure 1. The DevXLibrary Project in Solution Explorer: The figure shows the DevXLibrary project containing the ProjectLibrary class, used in this article as an example for the unit tests.
The Sample Project
As an example, I have provided a simple project named DevXLibrarycontaining a single class named ProjectLibrary (see Figure 1). The ProjectLibrary class contains three methods, each of which is intended to show how different areas of the unit testing features work. Each method could be used to validate a person's social security number using rules and information published by the Social Security Administration.

Author's Note: If you are interested, the information used to code these examples was found on two Web pages: the Wikipedia entry for Social Security Numbers and the Social Security Administration's Web page that maps social security numbers to their likely state of origin.

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