Search code examples
powershellpester

How can I use pester v5 configuration or container with four standard arguments?


I'm trying to invoke a pester script, moving from pester V4.6.0 to V5.3.1

The V4 arguments I used to use, when invoking pester were:

-supplying custom parameters
-OutputFormat NUnitXML
-OutputFile $xmlpath
-EnableExit

However, it seems for v5, I need to invoke Pester using either a configuration OR a container, neither of which cater fully for the arguments I used in V4. The limitations seem to be:

Invoke-Pester -Configuration $pesterConfig where $pesterConfig doesn't like me specifying custom data params in it, eg: Data = @{dataFactoryName = $dataFactoryName;}

or

Invoke-Pester -Container $container -ErrorAction Stop -Output Detailed -CI where $container doesn't like me specifying: -OutputFormat NUnitXML -OutputFile $xmlpath -EnableExit

How can I more efficiently use either container or configuration, to allow me to specify the following 4 things:

-supplying custom parameters
-OutputFormat NUnitXML
-OutputFile $xmlpath
-EnableExit

One work around, would be to settle for using configuration, which caters for 3 of my 4 arguments, and pass custom data to a file or env variable and read it in within the invoked ps script. Probably not ideal though.


Solution

  • Found a way to use configuration and container together:

            $container = New-PesterContainer -Path "..." -Data @{
                customParamA= "value"; 
                customParamB= "value"; 
            }
    
            $pesterConfig = [PesterConfiguration]@{
                Run = @{
                    Exit = $true
                    Container = $container
                }
                Output = @{
                    Verbosity = 'Detailed'
                }
                TestResult = @{
                    Enabled      = $true
                    OutputFormat = "NUnitXml"
                    OutputPath   = $xmlpath
                }
                Should = @{
                    ErrorAction = 'Stop'
                }
            }
    
            Invoke-Pester -Configuration $pesterConfig