如何使用具有多个文件名的shell脚本执行VSTest.Console.exe

如何使用具有多个文件名的shell脚本执行VSTest.Console.exe,shell,powershell-3.0,Shell,Powershell 3.0,我有一个文件夹,其中包含一些DLL,文件名为“Tests” e、 g“C:\Api\Myfile.Tests.dll” 我需要获取其中包含名称“Tests”的文件,并使用powershell脚本将其作为文件传递给VSTest.Console.exe 我的代码是 $DirectoryName = "C:\api"; $Parameters = ""; Get-ChildItem $DirectoryName -Filter "*Tests*" | ForEach-Object { $P

我有一个文件夹,其中包含一些DLL,文件名为“Tests”

e、 g“C:\Api\Myfile.Tests.dll”

我需要获取其中包含名称“Tests”的文件,并使用powershell脚本将其作为文件传递给VSTest.Console.exe

我的代码是

$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;
$Parameters给出了其中包含“Tests”的所有文件名。字符串由空格组成,如

C:\api\Base.Tests.dll C:\api\Model.Tests.dll


但脚本仍然不起作用。我猜它被视为一条单独的路径,因为它在一个sting变量中。如果是这样,那么如何解决这个问题。

我认为可能有两个问题:

  • Foreach对象连接需要附加一个中间空格
  • 文件路径需要用引号括起来。(在发现此问题之前,我在尝试调用vstest时遇到了相同的问题。)
这个代码对我有用:

    Get-ChildItem $DirectoryName  -Filter "*Tests*" |
    ForEach-Object {$parameters = $parameters + "`"" + $directoryName + "\" + $_ + "`" ";}

希望这能节省一些时间。。。但我在这方面遇到了很多麻烦,因为我的可执行路径包含空格

因此,我必须将命令格式化为字符串。注意转义的“&”和转义的引号。显然,带空格的命令需要包含“&”。。。我还尝试通过“&”直接调用它。。。然而,这去掉了dll参数列表的双引号,并在整个混乱中加上引号,这不是vsTest.Console.exe所期望的。。。无论如何,这是一个对我有效的解决方案

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

如果有帮助,这就是我的解决方案:

$Paths = Get-Childitem Test -recurse  | Where-Object {$_.PSIsContainer  -and $_.FullName -notmatch "obj" -and $_.name -like "net462"} | Select-Object -expandproperty fullname 

$Args = Get-ChildItem -Path $Paths -Filter "*Tests.dll"  | Select-Object -expandproperty fullname 

$Args = $Args + "/InIsolation" + "/Logger:trx;logfile=net462.trx" +"/Enablecodecoverage"

$exe = 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe'

echo $Args 

& $exe $Args