9

I am trying to execute from powershell the Visual Studio's tool MSTest with no success:

$testDLL = "myTest.dll"
$mstestPath = "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\mstest.exe"    
$arguments = " /testcontainer:" + $testDLL + " /test:UnitTest1"

Invoke-Expression "$mstestPath $arguments"

I get this error: "The term 'x86' is not recognized as the name of a cmdlet, function,..." Any idea? Thanks.

Edit:

Ok, the problem was solved using "&" instead "Invoke-Expression" and creating separated variables for each argument, it doesn't work for me just using both in one var:

$testDLL = "myTest.dll"
$mstestPath = "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\mstest.exe"    
$argument1 = "/testcontainer:" + $testDLL
$argument2 = "/test:UnitTest1"

& $mstestPath $argument1 
Jero Lopez
  • 398
  • 1
  • 7
  • 18
  • 2
    Related: http://stackoverflow.com/questions/3868342/running-an-exe-using-powershell-from-a-directory-with-spaces-in-it – David Brabant Jun 04 '13 at 09:56
  • "C:\Program Files (x86)" has a space so add quotes to your string `$mstestPath = '"C:\Program Files (x86)\...\IDE\mstest.exe"'` – E.V.I.L. Jun 04 '13 at 17:39

1 Answers1

9

I'd recommend using the & operator in the case (see comment David Brabant).

However, if you must use Invoke-Expression you could convert $mstestPath to its shortpath equivalent.

$testDLL = "myTest.dll"
$fs = New-Object -ComObject Scripting.FileSystemObject
$f = $fs.GetFile("C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\mstest.exe")
$mstestPath = $f.shortpath   
$arguments = " /testcontainer:" + $testDLL + " /test:UnitTest1"
Invoke-Expression "$mstestPath $arguments"
jon Z
  • 15,838
  • 1
  • 33
  • 35
  • 2
    nice answer. Do you have any idea how to parse the results? – John Demetriou Apr 05 '16 at 06:19
  • 1
    You export the results out to a trx file using the resultsfile switch: `/resultsfile:c:\temp\tests.trx` then you can parse the trx file: http://dbarrowstechblog.blogspot.com.au/2012/08/parsing-mstest-results-trx-files.html?_sm_au_=iNVjjVLrnLPWRJ0j'' or http://www.c-sharpcorner.com/UploadFile/e06010/read-trx-file-from-C-Sharp/ – Jeremy Thompson Aug 24 '17 at 01:10