【问题标题】:Powershell pass multiple test dlls to vstest.console.exePowershell 将多个测试 dll 传递给 vstest.console.exe
【发布时间】:2021-08-29 12:33:51
【问题描述】:

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

我可以通过传递以空格分隔的文件名来成功运行单元测试。 例如

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

但是当我使用 PS 脚本做类似的事情时

> $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

我得到了一些意想不到的东西......

因为 $JoinedPath 是一个带有引号的字符串,例如 "a.dll b.dll"

因此 vstest.console.exe 将始终收到单个“a.dll”(vstest.console.exe“a.dll b.dll”)

我不知道如何准确地表达我的问题......

总之,我想用powershell来模拟命令

vstest.console.exe a.dll b.dll

不是

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

我是 PowerShell 新手,不知道是否可行。

【问题讨论】:

    标签: powershell unit-testing mstest vstest vstest.console.exe


    【解决方案1】:

    您可以使用数组来帮助您处理命令行实用程序的参数,尤其是当您需要开始指定参数名称时。

    $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

    如果您必须添加其他参数,您可以在 foreach 块之后添加它们。

    $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
    

    如果参数与参数是分开的,这个实用程序似乎不是这种情况,你可以像这样将参数和参数添加在一起。

    $dotnetArgs = @()
    $dotnetArgs = "new"
    $dotnetArgs = "classlib"
    $dotnetArgs = $dotnetArgs + "--output" + "TestLib"
    $dotnetArgs = $dotnetArgs + "--name" + "TestLib"
    $dotnetArgs = $dotnetArgs + "--language" + "C#"
    & dotnet $dotnetArgs
    

    【讨论】:

    • 谢谢,对我帮助很大。我找到了一个可行的解决方案,最初是 vstest.console.exe $TestDlls.FullName,我写了 vstest.console.exe $TestDlls,但是失败了......为什么我可以这样做?是语法糖吗?
    猜你喜欢
    • 2019-11-10
    • 2020-11-08
    • 1970-01-01
    • 2017-05-08
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 2017-05-07
    相关资源
    最近更新 更多