I have a folder which has some dlls contains word "Tests" as file name
e.g "C:\Api\Myfile.Tests.dll"
I need to fetch those file which contains name "Tests" in it and pass as files to the VSTest.Console.exe using power shell script.
My code is
$DirectoryName = "C:\api";
$Parameters = "";
Get-ChildItem $DirectoryName -Filter "*Tests*" | ForEach-Object {
$Parameters = $Parameters + $DirectoryName + "\" + $_ ;
}
$TestRunner = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe";
$Parameters;
# & $TestRunner "C:\api\Base.Tests.dll" "C:\api\Model.Tests.dll";
& $TestRunner $Parameters;
The $Parameters gives all the file names which contains "Tests" in it. The string formed is with spaces like
C:\api\Base.Tests.dll C:\api\Model.Tests.dll
But still the script is not working. I guess its treating as a single path since its in a sting variable. If that so, then how to solve this.
Hopefully, this saves someone some time... but I had a load of trouble with this because of my executable path containing spaces...
So, I had to format my command as a string. Note the escaped '&' and escaped quotes. Apparently, commands with spaces are required to include '&'... I also tried invoking it directly via '&'... However, that stripped out the double quotes for the dll parameter list and threw quotes around the whole mess which isn't what vsTest.Console.exe expects... Anyhow, here was the solution that worked for me...
Function Get-AllTestDllsAsQuotedStrings {
$parameters = ""
Get-ChildItem `
-Path "C:\code\git\YourProject\Source" `
-File `
-Recurse `
-Filter *.dll |
where-object FullName -Like *bin\debug\*test*.dll |
ForEach-Object {
$parameters = $parameters + "`"" + $_.FullName + "`" ";
}
return $parameters
}
$vsTestConsoleExe = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"
$command = "`& `"$vsTestConsoleExe`" $(Get-AllTestDllsAsQuotedStrings)"
write-host $command
invoke-expression $command
I think there may be two issues:
This code worked for me:
Get-ChildItem $DirectoryName -Filter "*Tests*" |
ForEach-Object {$parameters = $parameters + "`"" + $directoryName + "\" + $_ + "`" ";}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With