Applies to Pester 3.4.0
Like any Windows PowerShell script, a script that contains Pester tests can include parameters. It’s easy enough to run the script and pass parameters and values in the usual way.
But, when you use Invoke-Pester to run the script, you need to pass the parameters in a hash table. This blog explains how to do it.
This post is the third in a series about how to run Pester tests. See also, How to Run Pester Tests and Invoke-Pester: Run Selected Tests.
————————–
To use Invoke-Pester to pass parameters to a script, use a command like this one:
Invoke-Pester -Script @{Path='C:\Tests\MyTests.Tests.ps1';Parameters=@{Name='Pester';Version='3.4.0'}} |
Or, this one:
Invoke-Pester -Script @{Path = 'C:\Tests\MyTests.Tests.ps1'; Arguments = 'Pester', '3.4.0'} |
The Script parameter of Invoke-Pester typically takes a array of paths, either paths to a directory or to scripts that typically contain Pester tests, or both.
But, it can also take a hash table. One hint is that Script parameter type is [System.Object[]], not [System.String[]].
Here are hash table keys:
- Path (required) <string>: The value of the Path key must be a single string, presumably (but not required) the path to directories or files. Wildcards are supported. A hash table with only the Path key is equivalent to submitting a string value to the Script parameter, except that this key is limited to one string.
Invoke-Pester -Script @{Path='C:\Tests\MyTests.Tests.ps1'}
- Parameters <hashtable>: Each key-value pair in the (nested) hash table consists of a parameter name and its value, such as @{ComputerName = ‘localhost’}. Use this key to pass named parameter names and values to a script.
Invoke-Pester -Script @{Path = 'C:\Tests\MyTests.Tests.ps1'; Parameters = @{Name = 'Pester'; Version = '3.4.0'}}
- Arguments <array>: Use this key to pass positional parameter values to a script, such as ‘localhost’, $true or @(‘localhost’, $true).
Invoke-Pester -Script @{Path = 'C:\Tests\MyTests.Tests.ps1'; Arguments = 'Pester', '3.4.0'}
Invoke-Pester splats the parameters and arguments to the scripts as it calls them.
Typically, the Path value in the hash table resolves to a particular script that takes the specified named and/or positional parameters. But, when you pass parameters to a script that has no parameters, the extraneous parameters are ignored, so you can use a directory value effectively.
For example, this command passes the ComputerName to one .Tests.ps1 file, but the command runs all .Tests.ps1 files in the directory and its subdirectories.
Invoke-Pester -Script @{Path = 'C:\Tests\MyTests'; Parameters = @{ComputerName = 'localhost'}} |
Of course, this strategy is error-prone and will not work when more than one of the scripts being run takes different parameters, so it’s probably not advisable.
But, it’s easy enough to pass parameters to a Pester test. Pester really is amazing!
Learning Pester? Check out Real-World Test-Driven Development with Pester. The code and slides are in Github at https://github.com/juneb/PesterTDD.
June Blender is a technology evangelist at SAPIEN Technologies, Inc. and a Windows PowerShell MVP. You can reach her at juneb@sapien.com or follow her on Twitter at @juneb_get_help.
Follow @SAPIENTech
![[Google+]](http://i0.wp.com/www.sapien.com/blog/wp-content/uploads/GooglePlus.gif?resize=16%2C16&ssl=1)