【问题标题】:Running NUnit 3 from Powershell从 Powershell 运行 NUnit 3
【发布时间】:2016-05-04 21:10:57
【问题描述】:

我有一个 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 的各种迭代,但没有得到任何结果。感谢所有建设性的建议 - 谢谢!

【问题讨论】:

    标签: powershell nunit-console nunit-3.0


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-19
        • 2011-05-05
        相关资源
        最近更新 更多