0

https://learn.microsoft.com/en-us/visualstudio/test/vstest-console-options?view=vs-2019#general-command-line-options

I can successfully run unit tests by passing file names separated by space. e.g.

>vstest.console.exe a.dll b.dll 

But when I use PS script to do something like

> $TestDlls = Get-ChildItem -Path "Folder" -Filter "Test.*.dll" -Recurse -File
> $JoinedPath = $TestDlls -join " " #Try to join the paths by ' ' ??? Is it a wrong command?
> vstest.console.exe $JoinedPath

I got something unexpected...

Since $JoinedPath is a string with quotations like "a.dll b.dll"

So the vstest.console.exe will always receive a single "a.dll" (vstest.console.exe "a.dll b.dll")

I don't know how to express my problem precisely...

In short, I want to use powershell to simulate the command

vstest.console.exe a.dll b.dll

NOT

vstest.console.exe "a.dll b.dll"

I'm new to PowerShell I don't know if it is possible.

user588477
  • 165
  • 11

1 Answers1

1

You can use an array to help you with arguments for command line utilities, especially when you need to start specifying parameter names.

$TestDlls = Get-ChildItem -Path $Folder -Filter "Test.*.dll" -Recurse  # -File is not needed unless you have folders also named Test.*.dll
$VSTestArgs = @()
foreach ($TestDll in $TestDlls) {
    $VSTestArgs = $VSTestArgs + $TestDll.FullName
}
& vstest.console.exe $VSTestArgs  # & is the call operator.

Call Operator

If you have to add other arguments, you can do so by adding them after the foreach block.

$TestDlls = Get-ChildItem -Path $Folder -Filter "Test.*.dll" -Recurse  # -File is not needed unless you have folders also named Test.*.dll
$VSTestArgs = @()
foreach ($TestDll in $TestDlls) {
    $VSTestArgs = $VSTestArgs + $TestDll.FullName
}
$VSTestArgs = $VSTestArgs + "/Settings:local.runsettings"
$VSTestArgs = $VSTestArgs + "/Tests:TestMethod1,testMethod2"
$VSTestArgs = $VSTestArgs + "/EnableCodeCoverage"
& vstest.console.exe $VSTestArgs

If the argument is separate to the parameter, which doesn't seem to be the case with this utility, you add the parameter and argument together like this.

$dotnetArgs = @()
$dotnetArgs = "new"
$dotnetArgs = "classlib"
$dotnetArgs = $dotnetArgs + "--output" + "TestLib"
$dotnetArgs = $dotnetArgs + "--name" + "TestLib"
$dotnetArgs = $dotnetArgs + "--language" + "C#"
& dotnet $dotnetArgs
Ash
  • 3,030
  • 3
  • 15
  • 33
  • 2
    Thank you, it helps me a lot. I found a workable solution, vstest.console.exe $TestDlls.FullName originally, I wrote vstest.console.exe $TestDlls, but failed....why I can do this? Is it a syntax sugar? – user588477 Jun 14 '21 at 16:49