How to pass parameters to all the pester test scripts
Asked Answered
O

2

6

The Invoke-Pester command makes it possible to invoke a single test script with explicit parameters using the -Script parameter. But what if I want to pass the same parameters to all of the test scripts? I do not want to invoke pester in a loop, because I want it to produce a single test result file.

So, how do we do it?

Omor answered 25/10, 2019 at 15:9 Comment(0)
C
2

You can do it by passing an array of hashes to the -Script parameter. Something like this:

$a = @()
$params = @{param1 = 'xx'; param2 = 'wuauserv'}
$a += @{Path =  '.\test1.Tests.ps1'; Parameters = $params}
$a += @{Path =  '.\test2.Tests.ps1'; Parameters = $params}

Invoke-Pester -Script $a
Cuprite answered 25/10, 2019 at 21:25 Comment(2)
Oh, I missed that. Is it documented anywhere?Omor
The Pester docs only give an example showing a Path/Parameters hash passed to the -Script parameter, but most parameters and commands in Powershell seem happy to except arrays and the -Script excepts wildcards so I tried it and it worked.Cuprite
G
11

Starting from Pester 5.1 you can use New-PesterContainer -Data @{} to pass all required parameters to Invoke-Pester. You can pass now path to both a single test file or a test directory to Invoke-Pester -Path.

For instance, you have a test file:

param($param1, $param2)

Describe '' {
  It '' { 
    $param1 | Should -Be '...'
    $param2 | Should -Be '...'
  }
}

Then you run it like this:

$container = New-PesterContainer -Path <tests_directory> -Data @{ param1='...'; param2='...' }
Invoke-Pester -Container $container

The official documentation is here: https://pester.dev/docs/usage/data-driven-tests#providing-external-data-to-tests

The list of new features you can find here: https://github.com/pester/Pester/releases/tag/5.1.0

Giamo answered 19/1, 2021 at 11:38 Comment(1)
Alas, we are still on Pester 3, which is the default on our Windows. But +1Omor
C
2

You can do it by passing an array of hashes to the -Script parameter. Something like this:

$a = @()
$params = @{param1 = 'xx'; param2 = 'wuauserv'}
$a += @{Path =  '.\test1.Tests.ps1'; Parameters = $params}
$a += @{Path =  '.\test2.Tests.ps1'; Parameters = $params}

Invoke-Pester -Script $a
Cuprite answered 25/10, 2019 at 21:25 Comment(2)
Oh, I missed that. Is it documented anywhere?Omor
The Pester docs only give an example showing a Path/Parameters hash passed to the -Script parameter, but most parameters and commands in Powershell seem happy to except arrays and the -Script excepts wildcards so I tried it and it worked.Cuprite

© 2022 - 2024 — McMap. All rights reserved.