2

I have a PS script which I've been successfully using to run and process results from NUnit 2.6.3. I've recently upgraded to NUnit 3.2.1 and am having problems with passing arguments to the NUnit executable. The script is called from Octopus deploy like this:

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

The arguments are handled here:

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

NUnit is called (with a line to echo the command):

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

with the result:

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

The following line of code works as expected from the PS script:

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

The tests run, and the category filter & work directory are set as expected:

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

I've also tried:

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

This results in the same output from 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

I'm a PS noob, so hopefully it's something obvious I've overlooked in the script. I've also tried various iterations of Invoke-Command with -ArgumentList, but didn't get anywhere with that. All constructive suggestions are appreciated - thanks!

johng14
  • 21
  • 1
  • 3

2 Answers2

0

I had a lot of success with practice #5 in this article when trying to execute programs in powershell in the past. However, I think I had a similar problem couple of days ago. I had to deal with an argument --deployat="yyyy-MM-dd HH:mm:ss" as a single input, and the space between dd HH is killing me. I ended up success using Invoke-Expression, #2 option refereed in the article. Just be careful as it said in the article, this is a dangerous practice.

Kai Zhao
  • 995
  • 7
  • 14
0

Thanks @Kai - yes, the space was indeed the roadblock (thanks also for the reference. I was hoping to avoid using Invoke-Expression as for this application it seemed overkill). One of the devs here also picked up on this and suggested the following, which seems to work with RunTests.ps1 -NunitArgs "--where cat==QuickTests" -OutputPath "workdir" (this is #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
}

And calling NUnit:

Write-Host "Executing: $(GetNUnitConsolePath)  $testParameters"
& $(GetNUnitConsolePath) $testParameters
johng14
  • 21
  • 1
  • 3