How to: Run Tests in the Visual Studio Process

By default, tests are hosted, or run, in the VSTestHost.exe process. The Host Adapter sample demonstrates how to extend Visual Studio Team System testing tools by creating a custom host adapter that runs tests in a different environment, such as in the Visual Studio process. 

This topic describes how to run tests in the Visual Studio process by using the sample host adapter that you have built.

Notes on Running Tests

  • You can open the solution UnitTest.sln to see examples of unit tests that run in the Visual Studio IDE.

  • When you build the VsIdeTestHost sample, the assemblies are not strongly signed. This means that the add-in provided by the sample will not load when you start Visual Studio. To work around this, you can disable strong name verification by running sn –Vr <assembly name> for the assemblies Microsoft.Samples.VisualStudio.TestTools.HostAdapters.VsIde and Microsoft.Samples.VisualStudio.TestTools.VsIdeTestHostFramework, or you can modify the sample by strongly signing the assemblies.

  • The sample includes a pre-built version of VsIdeTestHost.msi, which contains strongly signed assemblies. You can find it in the solution folder.

  • The host side of the sample host adapter is implemented as a Visual Studio add-in. After you install the sample, the host side of the sample host adapter is loaded when you start Visual Studio. For more information about the locations of the components of the host adapter, see Overview of Host Adapters.

  • For more details, see the file ReadMe.doc located under the sample folder.

To run tests in the Visual Studio process using the sample host adapter

  1. Install the VsIdeTestHost.msi.

  2. Restart Visual Studio.

  3. Create a test project.

  4. From the .NET page of the Add Reference dialog box, add a reference to the Microsoft.Samples.VisualStudio.TestTools.VsIdeTestHostFramework assembly.

  5. Create a test method.

  6. To specify the VS IDE host adapter for an individual test, set the HostType() attribute on the test method to HostType("VS IDE"). To specify the VS IDE host adapter for an entire test run, open the Hosts page of the Test Run Configuration editor and select VS IDE for the Host Type. For more information, see How to: Specify Tests to Run in a Host Adapter and Configuring Test Execution.

  7. Inside your test method, use Microsoft.Samples.VisualStudio.TestTools.VsIdeTesting.VsIdeTestHostContext.Dte to obtain the Visual Studio DTE object. You can also use Microsoft.Samples.VisualStudio.TestTools.VsIdeTesting.VsIdeTestHostContext.ServiceProvider to obtain a Visual Studio service.

  8. Run the test.

See Also

Concepts

Overview of Host Adapters

Overview of the Host Adapter Sample

Other Resources

Configuring Test Execution