{"id":12201,"date":"2016-06-15T06:00:00","date_gmt":"2016-06-15T13:00:00","guid":{"rendered":"https:\/\/www.sapien.com\/blog\/?p=12201"},"modified":"2016-07-13T12:00:38","modified_gmt":"2016-07-13T19:00:38","slug":"invoke-pester-run-selected-tests","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2016\/06\/15\/invoke-pester-run-selected-tests\/","title":{"rendered":"Invoke-Pester: Running Selected Tests"},"content":{"rendered":"<p>Applies to: Pester 3.4.0<\/p>\n<p>In <a href=\"https:\/\/wp.me\/p3tXTf-3aE\">How to Run Pester Tests<\/a>, I talked about the different places that you can put your Pester days and the different ways to run them, including, but not limited to, the <strong>Invoke-Pester<\/strong> function. Today, I&#8217;ll talk about the parameters of Invoke-Pester function that let you determine which tests run. Next, I&#8217;ll show you how to pass parameters to a Pester test file.<\/p>\n<p>By default, Invoke-Pester runs all *.Tests.ps1 files in the local directory and its subdirectories. That&#8217;s a useful default, but the parameters of Invoke-Pester let you control the alternatives.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/06\/clip_image002-1.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"clip_image002\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/06\/clip_image002_thumb-1.jpg\" alt=\"clip_image002\" width=\"767\" height=\"153\" border=\"0\" \/><\/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<hr>\n<h1>Run all Tests.ps1 files in a path<\/h1>\n<p>To run all *.Tests.ps1 files in a particular directory or collection of directories, use the <b>Script<\/b> parameter. The <b>Script<\/b> parameter of Invoke-Pester was originally named <b>Path<\/b>, but it does so many things that they had to come up with a more generic name for it.<\/p>\n<p>When you use the <b>Script<\/b> parameter of Invoke-Pester and give it paths to one or more directories, Invoke-Pester runs all .Tests.ps1 files in all subdirectories of the directories recursively. This is terrific for testing modules and other projects in progress. It supports wildcard characters, too.<\/p>\n<p>For example, this command runs all *.Tests.ps1 files in the two specified directories and their subdirectories.<\/p>\n<pre lang=\"PowerShell\">Invoke-Pester -Script C:\\GitHub\\MyProject, D:\\Working\\*\\1.0<\/pre>\n<p>But, you can tell it to run other types of files.<\/p>\n<h1>Run tests in other files<\/h1>\n<p>To direct Invoke-Pester to run any file, enter the path and file name in the value of the <b>Script<\/b> parameter. For example, this command tells Invoke-Pester to run the elusively named Test-ScriptWithPesterTest.ps1 file.<\/p>\n<pre lang=\"PowerShell\">Invoke-Pester -Script C:\\GitHub\\MyProject\\Test-ScriptWithPesterTest.ps1<\/pre>\n<p>Invoke-Pester runs the files that you specify as the value of the Script parameter, just as though you ran them at the command line, including code that is not part of the Pester test.<\/p>\n<p>You can even use Invoke-Pester to run PowerShell scripts that contain no tests, like my PowerShell profile. It dutifully reports that no tests passed or failed.<\/p>\n<pre class=\"output\">PS C:\\&gt; Invoke-Pester C:\\Users\\JuneB\\Documents\\WindowsPowerShell\\profile.ps1\r\n\r\nTests completed in 0ms\r\nPassed: 0 Failed: 0 Skipped: 0 Pending: 0 Inconclusive: 0<\/pre>\n<p>Be sure to include the file name of non-.Tests.ps1 files. If you use a wildcard, Invoke-Pester reverts to its default, which runs only the .Tests.ps1 files (recursively).<\/p>\n<pre lang=\"PowerShell\">Invoke-Pester -Script C:\\GitHub\\MyProject\\*.ps1   # Runs only .Tests.ps1 files<\/pre>\n<p>You can specify both directories and files. Again, if you specify a directory, Invoke-Pester runs only the .Tests.ps1 files in the directory (recursively).<\/p>\n<pre lang=\"PowerShell\">Invoke-Pester -Script C:\\GitHub\\MyProject\\Project.ps1, D:\\Working\\*\\1.0<\/pre>\n<p>And, you can specify a particular .Tests.ps1 file, too.<\/p>\n<pre lang=\"PowerShell\">Invoke-Pester -Script C:\\GitHub\\MyProject\\Project.ps1, C:\\Scripts\\Module.Help.Tests.ps1<\/pre>\n<p>But, you can also select tests within files<\/p>\n<h1>Run Selected Pester Tests<\/h1>\n<p>To run only certain tests within the files specified by the <b>Script<\/b> parameter, use the <b>TestName, Tag<\/b>, and <b>ExcludeTag<\/b> parameters of Invoke-Pester. These parameters filter within the files specified by the paths in the Script parameter; they never expand its range.<\/p>\n<p>The <b>TestName<\/b> parameter runs only Describe blocks with the specified test names or name patterns. It is case-insensitive. Remember that <b>TestName<\/b> looks only at names of Describe blocks, even though the InModuleScope, Context, and It blocks have required names.<\/p>\n<p>For example, this command runs only the Describe blocks with names that include &#8220;parameter.&#8221;<\/p>\n<pre lang=\"PowerShell\">Invoke-Pester C:\\Scripts\\Module.Help.Tests.ps1 -TestName '*parameter*'\r\n\r\n#In file\r\nDescribe \"gets parameter descriptions\" {...}  # runs this one\r\nDescribe \"gets correct types of parameters\" {...} # runs this one, too\r\nDescribe \"gets mandatory\" { It \"parameter is mandatory\" {...}...} # doesn't run this one\r\n<\/pre>\n<p>If you specify multiple TestName values, Invoke-Pester runs tests that have any of the values in the Describe name (it ORs the values).<\/p>\n<pre lang=\"PowerShell\">Invoke-Pester C:\\Scripts\\Module.Help.Tests.ps1 -TestName '*correct*', \"*mandatory*\"\r\n\r\n#In file\r\nDescribe \"gets parameter descriptions\" {...}  # doesn't run this one\r\nDescribe \"gets correct parameter types\" {...} # runs this one\r\nDescribe \"gets mandatory\" { It \"parameter is mandatory\" {...}...} # runs this one\r\n<\/pre>\n<p>The <b>Tag<\/b> and <b>ExcludeTag<\/b> parameters run Describe blocks that have the specified tags, that is, the values of the Tag parameter of the Describe function. They are case-insensitive, but they don&#8217;t support wildcard characters. And, at least in Pester 3.4.0, they cannot find tags that include spaces. So they find &#8220;UnitTest,&#8221; but cannot find &#8220;Unit Test,&#8221; even if its an exact match for a tag.<\/p>\n<pre lang=\"PowerShell\">Invoke-Pester C:\\Scripts\\Module.Help.Tests.ps1 -Tag UnitTest, 'Unit test'\r\n\r\n#In file\r\nDescribe \"finds the synopsis\" -Tag UnitTest {...}         #Runs this test\r\nDescribe \"finds the return value\" -Tag 'Unit Test' {...}  #Doesn't run this test\r\n<\/pre>\n<p>When you specify multiple tags, Invoke-Pester runs tests that have any of the listed tags (it ORs the tags).<\/p>\n<pre lang=\"PowerShell\">Invoke-Pester C:\\Scripts\\Module.Help.Tests.ps1 -Tag Unit, Regression\r\n\r\n#In file\r\nDescribe \"gets parameter descriptions\" -Tag Unit       {...}     # Runs this test\r\nDescribe \"gets mandatory value\" -Tag regression {...}            # Runs this test\r\nDescribe \"gets correct parameter types\" {...}                    # Doesn't run this test\r\n<\/pre>\n<p>But, when you use the <b>TestName<\/b> and <b>Tag<\/b> parameters in the same command (at least in version 3.4.0), Invoke-Pester combines the requirements and runs only the tests that have the specified TestName and Tag (it ANDs the requirements).<\/p>\n<pre lang=\"PowerShell\">Invoke-Pester C:\\Scripts\\Module.Help.Tests.ps1 -TestName '*parameter*' -Tag UnitTest\r\n\r\n#In file\r\nDescribe \"gets parameter descriptions\" -Tag UnitTest {...} # Runs this test\r\nDescribe \"gets correct parameter types\" {...}              # Doesn't run this test\r\nDescribe \"gets mandatory value\" -Tag UnitTest {...}        # Doesn't run this test\r\n<\/pre>\n<p>Predictably, the ExcludeTag parameter runs all Describe blocks except for those that match any of the Tag values. If a test is included by the Tag parameter and excluded by the ExcludeTag parameter, ExcludeTag takes precedence over Tag, and the test does not run.<\/p>\n<p>Interestingly, tests that are filtered out by <b>TestName<\/b>, <b>Tag<\/b>, and <b>ExcludeTag<\/b> are not counted as skipped. Only tests that have the <b>Skip<\/b> parameter of the It function are listed in the Skip count value.<\/p>\n<p>For example, there are multiple tests in the file, but only one has the NullTest tag. Notice that the Skipped value is 0.<\/p>\n<pre class=\"output\">PS C:\\ &gt; C:\\Scripts\\Module.Help.Tests.ps1 -TestName '*parameter*' -Tag NullTest\r\n\r\nDescribing gets parameter descriptions\r\n\r\n[+] parameter description is not null 42ms\r\nTests completed in 0ms\r\n\r\nPassed: 1 Failed: 0 Skipped: 0 Pending: 0 Inconclusive: 0\r\n<\/pre>\n<p>That&#8217;s pretty high-resolution control. Next, we&#8217;ll talk about passing parameters to a Pester test script.<\/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","protected":false},"excerpt":{"rendered":"<p>Applies to: Pester 3.4.0 In How to Run Pester Tests, I talked about the different places that you can put your Pester days and the different ways to run them, including, but not limited to, the Invoke-Pester function. Today, I&#8217;ll talk about the parameters of Invoke-Pester function that let you determine which tests run. Next, [&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,1119,1121,25],"tags":[1127,934,1123,28],"class_list":["post-12201","post","type-post","status-publish","format-standard","hentry","category-behavior-driven-development-bdd","category-pester","category-test-driven-development","category-windows-powershell","tag-invoke-pester","tag-juneb","tag-pester","tag-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/12201","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=12201"}],"version-history":[{"count":18,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/12201\/revisions"}],"predecessor-version":[{"id":12503,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/12201\/revisions\/12503"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=12201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=12201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=12201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}