无法在 Powershell 中使用参数正确 运行 MSTest

Cannot properly run MSTest with arguments in Powershell

我正尝试在 Powershell 中 运行 MSTest。传递的选项之一是/testcontainer,它可以传递多次。

在我的脚本中,我需要构建选项字符串,因为我可以将许多测试容器传递给脚本,因此这就是我所做的:

param(
  [string[]] $TestContainers = @(
    'C:\Users\myuser\MyProject\test\Test1\bin\Debug\Test1.dll', 
    'C:\Users\myuser\MyProject\test\Test1\bin\Debug\Test2.dll', 
    'C:\Users\myuser\MyProject\test\Test1\bin\Debug\Test3.dll', 
    'C:\Users\myuser\MyProject\test\Test1\bin\Debug\Test4.dll'
  )
);

$TestContainersOptions = @();
foreach ($TestContainer in $TestContainers)
{
  $TestContainersOptions += "/testcontainer:'$TestContainer'";
}

$MSTestPath = "C:\Program...\MsTest.exe"

& $MSTestPath $TestContainersOptions;

当我 运行 脚本时,我得到:

Microsoft (R) Test Execution Command Line Tool Version 14.0.23107.0 Copyright (c) Microsoft Corporation. All rights reserved.

The given path's format is not supported. For switch syntax, type "MSTest /help"

怎么办?

将每个路径括在"中(双引号的转义序列只是连续两个,""):

$TestContainersOptions = foreach ($TestContainer in $TestContainers)
{
    "/testcontainer:""$TestContainer"""
}

如果将foreach循环的输出直接赋给变量(如上所示),则不必事先将其声明为数组