2

我有一个 PS 脚本,我已成功使用它来运行和处理 NUnit 2.6.3 的结果。我最近升级到 NUnit 3.2.1 并且在将参数传递给 NUnit 可执行文件时遇到问题。该脚本是从 Octopus deploy 调用的,如下所示:

RunTests.ps1 -NunitArgs "--where cat==QuickTests" -OutputPath "workdir"

参数在这里处理:

param(
    [string]$OutputPath = "testResults",
    [string]$NunitArgs = "nunitOptions"
)

调用 NUnit(用一行来回显命令):

function GetNUnitConsolePath
{
    return join-path -path ${env:ProgramFiles(x86)} -childpath "NUnit-3.2.1\bin\nunit3-console.exe"
}

...

Write-Host "Executing: $(GetNUnitConsolePath) Test.dll $NunitArgs --work==$OutputPath"
& $(GetNUnitConsolePath) Test.dll $NunitArgs --work==$OutputPath

结果:

Executing: C:\Program Files (x86)\NUnit-3.2.1\bin\nunit3-console.exe Test.dll --where cat==QuickTest
s --work=workdir
NUnit Console Runner 3.2.1
Copyright (C) 2016 Charlie Poole

Invalid argument: --where cat==QuickTests

以下代码行按 PS 脚本的预期工作:

& $(GetNUnitConsolePath) Test.dll --where cat==QuickTests --work=workdir

测试运行,类别过滤器和工作目录按预期设置:

NUnit Console Runner 3.2.1
Copyright (C) 2016 Charlie Poole

Runtime Environment
   OS Version: Microsoft Windows NT 6.3.9600.0
  CLR Version: 4.0.30319.34209

Test Files
    Test.dll

Test Filters
    Where: cat==QuickTests


Run Settings
    WorkDirectory: workdir
    ImageRuntimeVersion: 4.0.30319
    ImageTargetFrameworkName: .NETFramework,Version=v4.5
    ImageRequiresX86: False
    ImageRequiresDefaultAppDomainAssemblyResolver: False
    NumberOfTestWorkers: 8

Test Run Summary
  Overall result: Passed

我也试过:

$arg1 = 'Test.dll'
$arg2 = '--where cat==QuickTests'
$arg3 = '--work=workdir'
Write-Host "Executing: $(GetNUnitConsolePath) $arg1 $arg2 $arg3"
& $(GetNUnitConsolePath) $arg1 $arg2 $arg3

这导致来自 NUnit 的相同输出:

Executing: C:\Program Files (x86)\NUnit-3.2.1\bin\nunit3-console.exe Test.dll --where cat==QuickTest
s --work=workdir
NUnit Console Runner 3.2.1
Copyright (C) 2016 Charlie Poole

Invalid argument: --where cat==QuickTests

我是一个 PS 菜鸟,所以希望这是我在脚本中忽略的明显内容。我还尝试了使用 -ArgumentList 的 Invoke-Command 的各种迭代,但没有得到任何结果。感谢所有建设性的建议 - 谢谢!

4

2 回答 2

0

谢谢@Kai - 是的,空间确实是障碍(也感谢您的参考。我希望避免使用 Invoke-Expression ,因为这个应用程序似乎有点矫枉过正)。这里的一位开发人员也注意到了这一点并提出了以下建议,这似乎适用于 RunTests.ps1 -NunitArgs "--where cat==QuickTests" -OutputPath "workdir" (这是您参考文章中的#5):

function CreateTestParameters($timeString)
{
    $optArr = $nunitArgs -split ' '
    $testParameters = @()
    $testParameters += 'Test.dll'
    $testParameters += $optArr[0]
    $testParameters += $optArr[1]
    $resultsParentPath = CreateResultsParentPath($timeString)
    $testParameters += "--work=" + $resultsParentPath

    return $testParameters
}

并调用 NUnit:

Write-Host "Executing: $(GetNUnitConsolePath)  $testParameters"
& $(GetNUnitConsolePath) $testParameters
于 2016-05-05T13:58:16.590 回答
0

过去,当我尝试在 powershell 中执行程序时,我在本文中的练习 #5 中取得了很大的成功。但是,我想几天前我遇到了类似的问题。我不得不将一个论点--deployat="yyyy-MM-dd HH:mm:ss"作为一个单一的输入来处理,而两者之间的空间dd HH让我很生气。我最终成功地使用Invoke-Expression了文章中提到的#2 选项。正如文章中所说,请小心,这是一种危险的做法。

于 2016-05-05T01:23:30.937 回答