4

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?

MatthewG
  • 8,583
  • 2
  • 25
  • 27
mark
  • 59,016
  • 79
  • 296
  • 580

2 Answers2

9

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

Artem Bokov
  • 162
  • 1
  • 10
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
Dave Sexton
  • 10,768
  • 3
  • 42
  • 56
  • Oh, I missed that. Is it documented anywhere? – mark Oct 25 '19 at 21:26
  • 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. – Dave Sexton Oct 25 '19 at 21:36