Skip to content

Find VSTest

Heath Stewart edited this page Apr 30, 2019 · 5 revisions

VSTest is a good test harness when your solution uses multiple test frameworks that support VSTest discovery and execution, or when you want to have consistent code coverage and test results (when supported by test frameworks like xUnit and NUnit).

Starting with version 1.0.40, you can easily find the root path of the latest instance of VS that contains the tools. Note that we generally recommend using package IDs with "Workload" or "Component" in the name, but in this case both "Microsoft.VisualStudio.Workload.ManagedDesktop" and "Microsoft.VisualStudio.Workload.Web" install the requires test tools and vswhere does not support OR selection.

**Note: ** Starting with version 2.3 you can use the new switch parameter -requiresAny with both of the workloads below, such that command lines below would be replaced with the following - using supported and consistent workload IDs:

vswhere -latest -products * -requires Microsoft.VisualStudio.Workload.ManagedDesktop Microsoft.VisualStudio.Workload.Web -requiresAny -property installationPath

The following examples use the latest release. For examples that work with older releases, please view the history of this page.

Batch

You can find the root of VS similar to finding MSBuild.

Note below that the examples are written as if in a batch script, which requires escaping "%" with another "%" which is why you see "%%i". If you were typing this in the command prompt you would use only one "%" like "%i".

@echo off

for /f "usebackq" %%i in (`vswhere -latest -products * -requires Microsoft.VisualStudio.Workload.ManagedDesktop Microsoft.VisualStudio.Workload.Web -requiresAny -property installationPath`) do (
  set InstallDir=%%i
)

if exist "%InstallDir%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" (
  call "%InstallDir%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" %*
)

PowerShell

You can do something similar in PowerShell but without having to parse the output yourself using the for command.

function Invoke-VSTest {
  $ErrorActionPreference = 'Stop'

  $path = vswhere -latest -products * -requires Microsoft.VisualStudio.Workload.ManagedDesktop Microsoft.VisualStudio.Workload.Web -requiresAny -property installationPath
  $path = join-path $path 'Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe'

  if (test-path $path) {
    & $path $args
  }
}

This example is wrapped in a nice little function that will pass arguments through to vstest.console.exe.

You can’t perform that action at this time.