Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to all the pester test scripts

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?

like image 982
mark Avatar asked Jan 25 '23 16:01

mark


2 Answers

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

like image 175
Artem Bokov Avatar answered Jan 29 '23 15:01

Artem Bokov


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
like image 31
Dave Sexton Avatar answered Jan 29 '23 15:01

Dave Sexton