Unit Testing Framework

11/20/2008

3 minutes to read

In this article

The Unit Testing Framework supports unit testing in Visual Studio. Use the classes and members in the Microsoft.VisualStudio.TestTools.UnitTesting namespace when you are coding unit tests. You can use them when you have written the unit test from scratch or are refining a unit test that was generated from code you are testing.

Groups of Elements

To help provide a clearer overview of the Unit Testing Framework, this section organizes the elements of the UnitTesting namespace into groups of related functionality.

Note

Attribute elements, whose names conclude with the string Attribute, can be used either with or without the string Attribute. For example, the following two code examples function identically:

The TestContext Class

The properties of the test context class store information about the current test run. For example, the TestContext.DataRow and TestContext.DataConnection properties contain information that is used by the test for data-driven unit testing.

Attributes for Identifying and Sorting Tests

The following attributes and the values assigned to them appear in the Visual Studio Properties window for a particular test method.

These attributes are not meant to be accessed through the code of the unit test. Instead, they affect the ways the unit test is used or run, either by you through the IDE of Visual Studio, or by the Team System test engine.

For example, some of these attributes appear as columns in the Test List Editor and the Test Results window, which means you can use them to group and sort tests and test results.

One such attribute is TestPropertyAttribute, which you use to add arbitrary metadata to unit tests. For example, you could use it to store the name of a test pass that this test covers, by marking the unit test with [TestProperty("TestPass", "Accessibility")]. Or to store an indicator of the kind of test it is: [TestProperty("TestKind", "Localization")]. The property you create by using this attribute, and the property value you assign, are both displayed in the Visual Studio Properties window under the heading Test specific.

Test Configuration Classes

Attributes Used for Generating Reports

The attributes in this section relate the test method that they decorate to entities in the project hierarchy of a Team Foundation Server team project. For more information, see How to: Enable Reporting of Test Results.

Classes Used with Private Accessors

As described in How to: Test a Private Method, you can generate a unit test for a private method. This generation creates a private accessor class, which instantiates an object of the PrivateObject class. The PrivateObject class is a wrapper class that uses reflection as part of the private accessor process. The PrivateType class is similar, but is used for calling private static methods instead of calling private instance methods.