DEV Community

Cover image for PowerShell: Using parameters with Pester
Olivier Miossec
Olivier Miossec

Posted on • Edited on

PowerShell: Using parameters with Pester

If you have ever worked with Pester, The PowerShell test framework, you may have noticed that using arguments in your Pester scripts is a little tricky.
Passing arguments to a Pester script isn't very common for Unit testing, after all, the goal is to test functions, but if you want to write Pester scripts for other purposes, passing arguments could be a huge plus.
However, it is not possible to pass arguments to a
test with invoke-pester. But with Pester v5 it is now possible.

First, you need to check which version you run on your system

get-module -Name Pester -ListAvailable
Enter fullscreen mode Exit fullscreen mode

If you do not have the latest version you should update the module

Update-module -name Pester 
Enter fullscreen mode Exit fullscreen mode

On Windows you may find that you run the built-in version of Windows PowerShell, in this case, you can’t use “Update-Module” You will need to do a side-by-side installation.

Install-Module -Name Pester -Force -SkipPublisherCheck
Enter fullscreen mode Exit fullscreen mode

After this required step, we can build the test file.

param (
    [string]
    $firstParameter,

    [int]
    $SecondParameter 
)

describe "demo parameter" {

    it "the First Parameter should be Pester" {
        $firstParameter | Should -be "Pester"
    }

    it "the Second Parameter should be 5" {
        $SecondParameter | Should -be 5
    }

}
Enter fullscreen mode Exit fullscreen mode

To use parameters with the Pester file, just like any normal PowerShell file or function, you need to have a param section. As you can see there are 2 parameters here firstParameter and secondParameter, these two parameters are used in the Describe section of the test file.

You can even run the file with parameters, and it will work

.\beta.test.ps1 -firstParameter "pester" -SecondParameter 5
Starting discovery in 1 file.
Discovery found 2 tests in 8ms.
Running tests.
[+] C:\work\pester-parameter\beta.test.ps1 86ms (7ms|73ms)
Tests completed in 92ms
Tests Passed: 2, Failed: 0, Skipped: 0 NotRun: 0
Enter fullscreen mode Exit fullscreen mode

But there is no way to use these parameters with other Pester options using invoke-pester.

To use parameters with the invoke-pester cmdlet you need to create a containerInfo object. This object includes the path of Pester test files and a dictionary of parameters/values that will be passed during the execution of these files.

$pesterContainer = New-PesterContainer -Path .\beta.test.ps1 -Data @{ firstParameter = "pester"; secondParameter = 5 }
Invoke-Pester -Container $pesterContainer
Enter fullscreen mode Exit fullscreen mode

Using a containerInfo object is very useful when you aren’t conducting unit testing. In my daily work, I frequently test the result of deployment in a Cloud environment. In this case, you need parameters, because all deployments are different. But it can be also useful for testing functions that depend on an external environment. It can also help to reuse your pester tests.

Top comments (0)