DEV Community

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

Posted on • Edited on

5

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.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay