{"id":12218,"date":"2016-06-17T06:00:43","date_gmt":"2016-06-17T13:00:43","guid":{"rendered":"https:\/\/www.sapien.com\/blog\/?p=12218"},"modified":"2016-07-13T11:38:00","modified_gmt":"2016-07-13T18:38:00","slug":"how-to-pass-parameters-to-a-pester-test-script","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2016\/06\/17\/how-to-pass-parameters-to-a-pester-test-script\/","title":{"rendered":"How to Pass Parameters to a Pester Test Script"},"content":{"rendered":"<p>Applies to Pester 3.4.0<\/p>\n<p>Like any Windows PowerShell script, a script that contains Pester tests can include parameters. It&#8217;s easy enough to run the script and pass parameters and values in the usual way.<\/p>\n<p>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.<\/p>\n<p>This post is the third in a series about how to run Pester tests. See also, <a href=\"https:\/\/wp.me\/p3tXTf-3aE\" target=\"_new\">How to Run Pester Tests<\/a> and <a href=\"https:\/\/wp.me\/p3tXTf-3aN\" target=\"_new\">Invoke-Pester: Run Selected Tests<\/a>.<\/p>\n<blockquote><p>\nSee the posts in this Pester series:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.sapien.com\/blog\/2016\/06\/13\/how-to-run-a-pester-test\/\" target=\"_new\">How to Run a Pester Test<\/a><\/li>\n<li><a href=\"https:\/\/www.sapien.com\/blog\/2016\/06\/15\/invoke-pester-run-selected-tests\/\" target=\"_new\">Invoke-Pester: Running Selected Tests<\/a><\/li>\n<li><a href=\"https:\/\/www.sapien.com\/blog\/2016\/06\/17\/how-to-pass-parameters-to-a-pester-test-script\/\" target=\"_new\">How to Pass Parameters to a Pester Test Script<\/a><\/li>\n<li><a href=\"https:\/\/www.sapien.com\/blog\/2016\/06\/24\/testing-pester-code-coverage\/\" target=\"_new\">Testing Pester Code Coverage<\/a><\/li>\n<\/ul>\n<\/blockquote>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>To use Invoke-Pester to pass parameters to a script, use a command like this one:<\/p>\n<pre lang=\"PowerShell\">Invoke-Pester -Script @{Path='C:\\Tests\\MyTests.Tests.ps1';Parameters=@{Name='Pester';Version='3.4.0'}}<\/pre>\n<p>Or, this one:<\/p>\n<pre lang=\"PowerShell\">Invoke-Pester -Script @{Path = 'C:\\Tests\\MyTests.Tests.ps1'; Arguments = 'Pester', '3.4.0'}<\/pre>\n<p>&nbsp;<\/p>\n<p>The <strong>Script<\/strong> parameter of <strong>Invoke-Pester<\/strong> typically takes a array of paths, either paths to a directory or to scripts that typically contain Pester tests, or both.<\/p>\n<p>But, it can also take a hash table. One hint is that Script parameter type is [System.Object[]], not [System.String[]].<\/p>\n<p>Here are hash table keys:<\/p>\n<ul>\n<li><strong>Path<\/strong> (required) &lt;string&gt;: 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.\n<pre lang=\"PowerShell\">Invoke-Pester -Script @{Path='C:\\Tests\\MyTests.Tests.ps1'}<\/pre>\n<\/li>\n<li><strong>Parameters<\/strong> &lt;hashtable&gt;:\u00a0 Each key-value pair in the (nested) hash table consists of a parameter name and its value, such as @{ComputerName = &#8216;localhost&#8217;}. Use this key to pass named parameter names and values to a script.\n<pre lang=\"PowerShell\">Invoke-Pester -Script @{Path = 'C:\\Tests\\MyTests.Tests.ps1'; Parameters = @{Name = 'Pester'; Version = '3.4.0'}}<\/pre>\n<\/li>\n<li><strong>Arguments<\/strong> &lt;array&gt;: Use this key to pass positional parameter values to a script, such as &#8216;localhost&#8217;, $true or @(&#8216;localhost&#8217;, $true).\n<pre lang=\"PowerShell\">Invoke-Pester -Script @{Path = 'C:\\Tests\\MyTests.Tests.ps1'; Arguments = 'Pester', '3.4.0'}<\/pre>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Invoke-Pester splats the parameters and arguments to the scripts as it calls them.<\/p>\n<p>Typically, the <strong>Path<\/strong> 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.<\/p>\n<p>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.<\/p>\n<pre lang=\"PowerShell\">Invoke-Pester -Script @{Path = 'C:\\Tests\\MyTests'; Parameters = @{ComputerName = 'localhost'}}<\/pre>\n<p>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&#8217;s probably not advisable.<\/p>\n<p>But, it&#8217;s easy enough to pass parameters to a Pester test. Pester really is amazing!<\/p>\n<p>&nbsp;<\/p>\n<p>Learning Pester? Check out <a title=\"Real-World Test-Driven Development with Pester\" href=\"https:\/\/www.youtube.com\/watch?v=gssAtCeMOoo\" target=\"_blank\">Real-World Test-Driven Development with Pester<\/a>. The code and slides are in Github at <a href=\"https:\/\/github.com\/juneb\/PesterTDD\">https:\/\/github.com\/juneb\/PesterTDD<\/a>.<\/p>\n<p><em>June Blender is a technology evangelist at SAPIEN Technologies, Inc. and a Windows PowerShell MVP. You can reach her at <\/em><a href=\"mailto:juneb@sapien.com\"><em>juneb@sapien.com <\/em><\/a><em>or follow her on Twitter at <\/em><a href=\"https:\/\/www.twitter.com\/juneb_get_help\"><em>@juneb_get_help<\/em><\/a>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Applies to Pester 3.4.0 Like any Windows PowerShell script, a script that contains Pester tests can include parameters. It&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1122,2,1119,1121,1120,25],"tags":[1125,934,28,1124,622,997],"class_list":["post-12218","post","type-post","status-publish","format-standard","hentry","category-behavior-driven-development-bdd","category-general","category-pester","category-test-driven-development","category-testing","category-windows-powershell","tag-bdd","tag-juneb","tag-powershell","tag-tdd","tag-testing","tag-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/12218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=12218"}],"version-history":[{"count":13,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/12218\/revisions"}],"predecessor-version":[{"id":12501,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/12218\/revisions\/12501"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=12218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=12218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=12218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}