Share via

Verifying Code by Using Unit Tests

Unit tests give developers and testers a quick way to look for logic errors in the methods of classes in Visual C#, Visual Basic, and Visual C++ projects. A unit test can be created one time and run every time that source code is changed to make sure that no bugs are introduced.

Key Tasks

Use the following topics to help with understanding and creating unit tests:


Associated Topics

Learn about the structure of a unit test: Describes the parts of a unit test and the files that are added to your solution when you create a unit test with the Visual Studio testing tools.

Create and run unit tests: How to create unit tests, including data-driven unit tests and unit tests for .dll and .exe files for which you do not have the source code.

Considerations for creating specific types of unit tests: Provides links to topics that describe differences and additional information about unit tests and C++, generic methods, and Web services.

Perform stress and performance analysis by using load tests for your unit tests: You can create a load test and add your unit tests to it to help isolate performance and stress issues in your application.

Creating and using load tests requires Visual Studio 2010 Ultimate.

Use code coverage to identify what proportion of your project's code is being tested using unit tests: Learn about the code coverage feature of Visual Studio Application Lifecycle Management testing tools.

Set and enforce quality gates: You can create quality gates to enforce that tests are run before code is checked in to help ensure the quality of the code.

Extend the unit test type: You can add functionality to your tests that might not be in the Unit Test Framework. For example, you can add a test property that specifies if a test should run as a normal user or not. Or you can extend the framework to add row attributes to a method and use the data in that row inside the test.

For sample code of how to extend the unit test framework, see the following Microsoft Web site.

Reviewing Test Results

Describes test results and ways to work with them, including how to view, save, and delete them.

Running Automated Tests Using Microsoft Visual Studio

Provides links to information about using Visual Studio as opposed to using Microsoft Test Manager to run automated tests.

Power Tools for Unit Testing

  • Pex   Pex is a Visual Studio add-in to complement unit testing on .NET Framework applications. Pex finds the input and output values of your methods in the Visual Studio code editor. You can save these values as a small test suite that will have high code coverage.

  • Moles   Moles lets you replace any .NET method with a delegate. Moles supports unit testing by providing isolation by using detours and stubs. Because Moles works at the method level, it offers an alternative to mocking when the target API does not support it. SharePoint is a popular example of an API that benefits from isolation, but does not directly support mocking. Moles can also be used for fault injection, because it makes it easy to inject arbitrary behavior on your code under test.

    Pex is available only to MSDN subscribers and includes Moles. The Moles only download is available to the general public. You can read more about the tools, and download them, at Pex and Moles.


External Resources


link to videoVisual Studio Developer Center: How Do I: Easily Bind Data to .NET Unit Tests

link to videoVisual Studio Developer Center: How Do I: Data-Bind a Unit Test

link to videoVisual Studio Developer Center: How Do I: Write and Run Unit Tests Against Smart Devices


Visual Studio Unit Testing

See Also


Performing Common Development Tasks

Testing the Application