{"id":12192,"date":"2016-06-13T06:00:00","date_gmt":"2016-06-13T13:00:00","guid":{"rendered":"https:\/\/www.sapien.com\/blog\/?p=12192"},"modified":"2016-07-13T11:33:38","modified_gmt":"2016-07-13T18:33:38","slug":"how-to-run-a-pester-test","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2016\/06\/13\/how-to-run-a-pester-test\/","title":{"rendered":"How to Run a Pester Test"},"content":{"rendered":"<p>Applies to: Pester 3.4.0<\/p>\n<p>When you crack open the Pester module, you find the <strong>New-Fixture<\/strong> function, which creates a script and test file pair, and the <strong>Invoke-Pester<\/strong> function, which runs Pester tests.<\/p>\n<p>But New-Fixture is not the only way to create a Pester test and Invoke-Pester is not the only way to run a test.<\/p>\n<p>In this blog post, we&#8217;ll discuss the different ways to run a Pester test, including, but not limited to, the Invoke-Pester function. In the next post, I&#8217;ll rave about the cool parameters of Invoke-Pester.<\/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<h1>Where can I put Pester tests?<\/h1>\n<p>To decide where to put your Pester tests, consider how you plan to run them and when you want them to be available.<\/p>\n<p><b>The best place to put a Pester test is in a *.Tests.ps1 file. <\/b>A .Tests.ps1 script is immediately recognizable as a file of Pester tests. This is critical in a shared project, like an open source module, where collaborators might need to search for or identify tests, or verify that tests exist.<\/p>\n<p>Also, by default, Invoke-Pester runs all *.Tests.ps1 files. When you make it easy for people to run your tests, they&#8217;re more likely to run them.<\/p>\n<p>However, syntactically, a Pester test is a <b>Describe<\/b> block and its valid contents, including at least one <b>It<\/b> block that encloses a test. A <b>.Tests.ps1<\/b> script is the best place to put Pester tests, but it&#8217;s not the only valid place.<\/p>\n<p>You can put the Pester test in any one of these places (and probably in a few others):<\/p>\n<ul>\n<li><b>Tests script<\/b>: The best place to put Pester tests is in a Tests script, that is, a *<b>.Tests.ps1<\/b> file.<\/li>\n<li><b>Script<\/b>: You can place Pester tests in a standard Windows PowerShell script (.ps1), including the script that contains the code that is being tested.<\/li>\n<li><b>Command<\/b>: You can place Pester tests in a cmdlet, function, or workflow. Be sure that the code that is being tested is in a scope that the test can access.<\/li>\n<li><b>Module<\/b>: You can place Pester tests in a *.Tests.ps1, a different .ps1, or .psm1 file in a module directory. Again, be sure that the command that is being tested is available to the test, including being in an accessible scope. To mock or test commands in a function that are not exported, enclose the tests in an <b>InModuleScope<\/b> script block. To export a test into a session, place it in an exported cmdlet, function, or workflow.<\/li>\n<\/ul>\n<h1>How can I run Pester tests?<\/h1>\n<p>The standard way to run Pester tests is by using the <b>Invoke-Pester<\/b> function. Invoke-Pester is a fantastic tool &#8212; a little dynamo in your toolbox. When you use Invoke-Pester, you get it&#8217;s terrific output and parameters.<\/p>\n<p>But it&#8217;s not the only way to run a Pester test.<\/p>\n<p>A Pester test consists of valid Windows PowerShell commands. We don&#8217;t think of it as such, but Describe is a Windows PowerShell function that the Pester module exports. Its contents, including Context and It blocks and Should assertions are all valid Windows PowerShell commands.<\/p>\n<p>Therefore, you can run Pester tests&#8230;<\/p>\n<ul>\n<li><strong>At the command line<\/strong><br \/>\nYou can run a Pester test at the command line in the console. Notice that you don&#8217;t get the little test summary that Invoke-Pester adds, but it&#8217;s otherwise the same.<a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/06\/clip_image002.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.jpg\" alt=\"clip_image002\" width=\"540\" height=\"120\" border=\"0\" \/><\/a><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/06\/clip_image004.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_image004\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/06\/clip_image004_thumb.jpg\" alt=\"clip_image004\" width=\"548\" height=\"158\" border=\"0\" \/><\/a><\/li>\n<li><strong>By running a script<\/strong>When you run a script that contains a Pester test, the Pester test runs. So, any way that you can run a script runs a Pester test, including typing the path at the command line or running in your favorite script editor.In PowerShell Studio, you can run and debug Pester tests. I like to use the Run in Console command so I can see the familiar purple, green, and red colors of the test results.\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/06\/clip_image006.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_image006\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/06\/clip_image006_thumb.jpg\" alt=\"clip_image006\" width=\"646\" height=\"424\" border=\"0\" \/><\/a><\/li>\n<li><strong>By using Invoke-Pester<\/strong><br \/>\nInvoke-Pester is a terrific function. You don&#8217;t need to use it to run scripts, but once you understand it fully, you&#8217;ll use it more often and enjoy its features.By default, <b>Invoke-Pester<\/b> runs all Pester tests in all .Tests.ps1 files in the local directory and all subdirectories recursively.It&#8217;s a brilliant default because it works well for continuous integration and it doesn&#8217;t duplicate other commands that require more complex command syntax to run recursively.<\/p>\n<p>But, the power of Invoke-Pester is in it&#8217;s knockout parameters. I&#8217;ll save those for a separate post.<\/li>\n<\/ul>\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 When you crack open the Pester module, you find the New-Fixture function, which creates a script and test file pair, and the Invoke-Pester function, which runs Pester tests. But New-Fixture is not the only way to create a Pester test and Invoke-Pester is not the only way to run a test. [&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,1120,25],"tags":[1125,1127,934,1123,28,1124,622,1126],"class_list":["post-12192","post","type-post","status-publish","format-standard","hentry","category-behavior-driven-development-bdd","category-pester","category-test-driven-development","category-testing","category-windows-powershell","tag-bdd","tag-invoke-pester","tag-juneb","tag-pester","tag-powershell","tag-tdd","tag-testing","tag-unit-test"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/12192","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=12192"}],"version-history":[{"count":12,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/12192\/revisions"}],"predecessor-version":[{"id":12498,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/12192\/revisions\/12498"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=12192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=12192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=12192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}