Udostępnij za pośrednictwem


Coding Custom Unit Tests Using the Unit Testing Framework

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:

[TestClass()]

[TestClassAttribute()]

Elements Used for Data-Driven Testing

Use the following elements to set up data-driven unit tests. For more information, see How to: Create a Data-Driven Unit Test and Walkthrough: Using a Configuration File to Define a Data Source.

Attributes Used to Establish a Calling Order

A code element decorated with one of the following attributes is called at the moment you specify. For more information, see Anatomy of a Unit Test.

For Assemblies

AssemblyInitialize and AssemblyCleanup are called right after your assembly is loaded and right before your assembly is unloaded.

For Classes

ClassInitialize and ClassCleanup are called right after your class is loaded and right before your class is unloaded.

For Test Methods

Attributes Used to Identify Test Classes and Methods

Every test class must have the TestClass attribute, and every test method must have the TestMethod attribute. For more information, see Anatomy of a Unit Test.

Unit tests can verify specific application behavior by their use of various kinds of Assert statements, exceptions, and attributes. For more information, see Using the Assert Classes.

The TestContext Class

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 Visual Studio test engine.For example, some of these attributes appear as columns in the Test Manager window and Test Results window, which means that 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 you could use it 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.

Classes Used with Private Accessors

As described in Using Publicize to Create a Private Accessor, 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.

See Also

Reference

Microsoft.VisualStudio.TestTools.UnitTesting