0
votes

I am trying to run MSTest in Powershell. One of the options to pass is /testcontainer and it can be passed multiple times.

In my script I need to build the option string as I can have many test containers passed to the script, thus this is what I do:

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;

When I run the script I get:

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"

What to do?

1
Your second argument to & should be a string array as well: $TestContainersOptions = $TestContainers|%{"/testcontainer:'$_'"}; & $MSTestPath $TestContainersOptions - Mathias R. Jessen
@MathiasR.Jessen: I thins your comment is worth an answer... I will try that! - Andry
@MathiasR.Jessen: Well it gets better, but I get: The given path's format is not supported. But if I run the command manually it works! - Andry
I'd imagine it having a problem with ' vs. ". You can escape " inside a double-quoted string by entering in twice: "/testcontainer:""$TestContainer""" - Mathias R. Jessen
Oh yes! That was it! Man thanks, please post it as an answer, I'll mark it! - Andry

1 Answers

1
votes

Enclose each path in " (escape sequence for double-quotes is just two in a row, ""):

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

If you assign the output from the foreach loop directly to the variable (as shown above), you don't have to declare it as an array beforehand