Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute VSTest.Console.exe using a shell script with multiple filenames

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.

like image 324
shanmugharaj Avatar asked May 10 '26 12:05

shanmugharaj


2 Answers

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
like image 67
Nick Avatar answered May 12 '26 13:05

Nick


I think there may be two issues:

  • The Foreach-Object concatenation needs to append with an intervening space.
  • The file paths need to be surrounded by quotes. (I had the same issue attempting to call vstest until I discovered this.)

This code worked for me:

    Get-ChildItem $DirectoryName  -Filter "*Tests*" |
    ForEach-Object {$parameters = $parameters + "`"" + $directoryName + "\" + $_ + "`" ";}
like image 38
ket Avatar answered May 12 '26 12:05

ket



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!