The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Using Microsoft.VisualStudio.TestTools.UnitTesting Members in Unit Tests


The Unit Testing Framework supports unit testing in Visual Studio. Use the classes and members in the N:Microsoft.VisualStudio.TestPlatform.UnitTestFramework 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.

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.


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:



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.

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.DataAccessMethod

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.DataSourceAttribute

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.DataSourceElement

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.DataSourceElementCollection

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.

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AssemblyInitializeAttribute

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AssemblyCleanupAttribute

For Classes

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

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.ClassInitializeAttribute

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.ClassCleanupAttribute

For Test Methods

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestCleanupAttribute

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.

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute

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.

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.Assert

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.CollectionAssert

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.StringAssert

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AssertFailedException

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AssertInconclusiveException

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.UnitTestAssertException

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.ExpectedExceptionAttribute

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.

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.OwnerAttribute

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.DeploymentItemAttribute

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.DescriptionAttribute

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.HostTypeAttribute

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.IgnoreAttribute

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.PriorityAttribute

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestPropertyAttribute

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.WorkItemAttribute

  • TestConfiguration

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestConfigurationSection

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.

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.CssIterationAttribute

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.CssProjectStructureAttribute

As described in , 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.2056c6a7-6672-42a7-8f53-fead33c56deb

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.PrivateObject

  • T:Microsoft.VisualStudio.TestPlatform.UnitTestFramework.PrivateType


© 2015 Microsoft