Use MSBuild and/or VSTest from the command line to run unit tests

  1. Open up the Developer Command Prompt for VS.
  2. Type where vstest.console
  3. Type where msbuild
  4. Add the directory paths of each to the PATH environmental variable.

Now we can run both VSTest and MSBuild from a normal shell. Here is an example with PowerShell:

cd MyTests  
msbuild MyTests.csproj  
vstest.console bin\Debug\MyTests.dll  

We can run tests by wildcard...

vstest.console bin\Debug\MyTests.dll  
    /TestCaseFilter:FullyQualifiedName~Foobar

...or by name

vstest.console bin\Debug\MyTests.dll  
    /Tests:TestMethod1,TestMethod2

... or by TestCategoryAttribute.

vstest.console bin\Debug\MyTests.dll  
    /TestCaseFilter:"TestCategory=Dev"

See also: https://msdn.microsoft.com/en-us/library/jj155800.aspx

Questions? Ping me @shaunluttin on Twitter.