{"id":75011,"date":"2015-12-18T00:01:00","date_gmt":"2015-12-18T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/12\/18\/more-pester-feature-and-resources\/"},"modified":"2019-02-18T09:20:38","modified_gmt":"2019-02-18T16:20:38","slug":"more-pester-feature-and-resources","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/more-pester-feature-and-resources\/","title":{"rendered":"More Pester Feature and Resources"},"content":{"rendered":"<p><b>Summary<\/b>: Dave Wyatt wraps up his week teaching us about Pester with information about more resources.<\/p>\n<p><b>&nbsp; &nbsp;Note<\/b>&nbsp;&nbsp;&nbsp;This is a five-part series that includes the following posts:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/what-is-pester-and-why-should-i-care\/\">What is Pester and Why Should I Care?<\/a><br> Learn about a new test framework for PowerShell called Pester<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/getting-started-with-pester\/\">Getting Started with Pester<\/a><br> Learn how to get information back from Pester<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/unit-testing-powershell-code-with-pester\/\">Unit Testing PowerShell Code with Pester<\/a><br> Use Pester to analyze small pieces of Windows PowerShell code<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/testing-script-modules-with-pester\/\">Testing Script Modules with Pester<\/a><br>Use Pester for testing PowerShell modules<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/more-pester-feature-and-resources\/\">More Pester Feature and Resources<br><\/a>Learn about more Pester resources<\/li>\n<\/ul>\n<p>I hope that this series has convinced you that writing Pester tests for your code can be easy and valuable&mdash;even if I had to be fairly brief in my examples of the most essential parts of the module. Today, I want to show you a few of the other features that the module has to offer.<\/p>\n<p>First, coverage analysis! The term &ldquo;code coverage&rdquo; refers to how much of your code is actually tested. Pester can help you keep track of this metric by analyzing your code as it&rsquo;s being tested, and reporting on any PowerShell statements that were not executed during the test. (This doesn&rsquo;t necessarily guarantee that your tests are covering all the logic of the &ldquo;hit&rdquo; commands, but at least you can be certain that if a particular line of code didn&rsquo;t execute during your tests, you definitely didn&rsquo;t test that part.)<\/p>\n<p>To show that in action, I&rsquo;ll run the tests from yesterday&rsquo;s final script module example, using a <b>&ndash;TestName<\/b> filter to only run one of the <b>Describe<\/b> blocks:<\/p>\n<p><a href=\"\/cfs-file.ashx\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/60057.1.PNG\"><img decoding=\"async\" src=\"\/resized-image.ashx\/__size\/550x0\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/60057.1.PNG\" alt=\"Image of results\" title=\"Image of results\"><\/a><\/p>\n<p>The <b>&ndash;CodeCoverage<\/b> parameter was passed a path to the file that you wanted to analyze, and the <b>&ndash;TestName <\/b>parameter specified which <b>Describe<\/b> block to run. If you use the <b>&ndash;PassThru<\/b> switch at the same time, the object will include a <b>CodeCoverage <\/b>property. This property contains detailed information about the analysis, which you can use in your build pipeline if desired.<\/p>\n<p><a href=\"\/cfs-file.ashx\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/2804.2.PNG\"><img decoding=\"async\" src=\"\/resized-image.ashx\/__size\/550x0\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/2804.2.PNG\" alt=\"Image of command output\" title=\"Image of command output\"><\/a><\/p>\n<p>I snuck in another feature demonstration with the <b>&ndash;TestName<\/b> parameter. Another way to run a particular <b>Describe<\/b> block or a group of <b>Describe<\/b> blocks is to use tagging. Each call to <b>Describe<\/b> can optionally include the <b>&ndash;Tags<\/b> parameter to assign one or more tags to the block. <b>Invoke-Pester<\/b> can be called with the <b>&ndash;Tag<\/b> or <b>&ndash;ExcludeTag<\/b> parameters to control executing by tag, rather than by name.<\/p>\n<p>Another feature available for your convenience is a set of commands named <b>BeforeEach<\/b>, <b>AfterEach<\/b>, <b>BeforeAll<\/b>, and <b>AfterAll<\/b>. These are initialization and cleanup blocks. Pester will guarantee to run them at the begin and end of each <b>It<\/b> block (for the <b>Each<\/b> versions), or at the beginning and end of the <b>Describe<\/b> or <b>Context<\/b> block (for the <b>All<\/b> versions).<\/p>\n<p>Unlike most PowerShell code, these commands work a bit of magic. You can place them into your code in any order, and they&rsquo;ll still work, for example:<\/p>\n<p><a href=\"\/cfs-file.ashx\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/2728.3.PNG\"><img decoding=\"async\" src=\"\/resized-image.ashx\/__size\/550x0\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/2728.3.PNG\" alt=\"Image of command output\" title=\"Image of command output\"><\/a><\/p>\n<p>If your script needs to create temporary files on the hard disk, Pester provides a means for that with a built-in test drive. This is a folder created under your TEMP directory at the beginning of each <b>Describe<\/b> block. At the end of the <b>Describe<\/b>, the folder is deleted. You can access it via a variable named <b>$TestDrive<\/b>, or with a PSDrive named <b>TestDrive:\\<\/b>. Both options refer to the same path. But don&rsquo;t take my word for it&mdash;prove it with tests!<\/p>\n<p><a href=\"\/cfs-file.ashx\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/2627.4.PNG\"><img decoding=\"async\" src=\"\/resized-image.ashx\/__size\/550x0\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/2627.4.PNG\" alt=\"Image of command output\" title=\"Image of command output\"><\/a><\/p>\n<p>By the way, many of the ideas and code for these cool features came from the community! If you have a feature that you think would make your tests more valuable or easier to write, please head to <a href=\"https:\/\/github.com\/pester\/Pester\">Pester GitHub<\/a> site and open an <b>Issue<\/b> (or, if you&rsquo;ve already written code, open a <b>Pull Request<\/b>) to strike up a conversation about it!<\/p>\n<p>Speaking of community contributions, here are some of the neat features we&rsquo;ve got coming up when version 4.0 of Pester gets released:<\/p>\n<h4><b>Gherkin<\/b><\/h4>\n<p>Gherkin is a language for defining software requirements entirely in human-readable form, with no executable code whatsoever. It has a minimal amount of syntax rules, similar to YAML in its structure. The advantage is that non-programmers can easily write and maintain the spec for your code, and the spec can then be parsed and used by developers to directly control your automated tests. <\/p>\n<p> If you&rsquo;re interested in the original Gherkin (part of the Cucumber test framework for Ruby), check out <a href=\"https:\/\/cucumber.io\/\" target=\"_blank\">Cucumber: Simple, human collaboration<\/a>. The credit for bringing Gherkin support to Pester goes to Joel &ldquo;Jaykul&rdquo; Bennett, the man behind PoshCode, PowerShell.slack.com, and many other cool PowerShell community tools.<\/p>\n<h4><b>Extensible Should operators<\/b><\/h4>\n<p>Now the operators for the <b>Should<\/b> command are rather difficult to extend, due to how the arguments passed to <b>Should<\/b> are parsed. Everything&rsquo;s limited to this basic syntax:<\/p>\n<p style=\"margin-left:60px\">Should [Not] &lt;Operator&gt; [Value]<\/p>\n<p>There&rsquo;s no way to add switches or other additional parameters to individual operators.<\/p>\n<p>In Pester v4, we&rsquo;ll be generating dynamic parameter sets for each operator on the <b>Should<\/b> command. This will allow the command to be called like this (in the basic case):<\/p>\n<p style=\"margin-left:60px\">$true | Should &ndash;Not &ndash;Be $false<\/p>\n<p>It looks just like the current syntax, with the exception of the dashes on the <b>Not<\/b> and <b>Be<\/b> parameter names. However, this opens up a whole world of options to us for expanding the individual operators. For example, now <b>Should Throw<\/b> only allows you to match the _message_ of the error that was thrown. But with the new enhancements, we could easily add support for something like this:<\/p>\n<p style=\"margin-left:60px\">scriptBlock | Should &ndash;Throw &ndash;ExceptionOfType ([MyExceptionType])<\/p>\n<h4><b>Array comparisons<\/b><\/h4>\n<p>This is another enhancement to parts of the <b>Should<\/b> command. Now, arrays and the <b>Should<\/b> command have a weird relationship. Arrays piped to the <b>Should<\/b> command pass their elements down the pipeline one at a time (because that&rsquo;s how PowerShell works). In many cases, the operators behave strangely (again, because that&rsquo;s how PowerShell&rsquo;s comparison operators work when used on arrays.) Here&rsquo;s one example of those oddities:<\/p>\n<p style=\"margin-left:30px\"><a href=\"\/cfs-file.ashx\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/28606.5.PNG\"><img decoding=\"async\" src=\"\/resized-image.ashx\/__size\/550x0\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/28606.5.PNG\" alt=\"Image of command output\" title=\"Image of command output\"><\/a><\/p>\n<p>In the code we&rsquo;re preparing for v 4.0, the <b>Should<\/b> command and its operators are smarter about how they handle arrays, which will cause the previous example to fail as you would expect:<\/p>\n<p style=\"margin-left:30px\"><a href=\"\/cfs-file.ashx\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/6862.6.PNG\"><img decoding=\"async\" src=\"\/resized-image.ashx\/__size\/550x0\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/6862.6.PNG\" alt=\"Image of results\" title=\"Image of results\"><\/a><\/p>\n<p>Because this is already going to be a major release (meaning it will include some breaking changes that might require updates to existing Tests.ps1 files), this is a great time to suggest features or fixes to the project. The best way to do that is on the <a href=\"https:\/\/github.com\/pester\/Pester\" target=\"_blank\">Pester GitHub<\/a> site. We can&rsquo;t promise that all suggestions will be included, but for v 4.0, we&rsquo;ll be less reluctant to make types of changes that we might normally put on hold in a minor release (when _not_ breaking existing test code would be important.)<\/p>\n<p>~Dave<\/p>\n<p>Thanks for this informative series, Dave!<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Dave Wyatt wraps up his week teaching us about Pester with information about more resources. &nbsp; &nbsp;Note&nbsp;&nbsp;&nbsp;This is a five-part series that includes the following posts: What is Pester and Why Should I Care? Learn about a new test framework for PowerShell called Pester Getting Started with Pester Learn how to get information back [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[501,56,636,45],"class_list":["post-75011","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dave-wyatt","tag-guest-blogger","tag-pester","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Dave Wyatt wraps up his week teaching us about Pester with information about more resources. &nbsp; &nbsp;Note&nbsp;&nbsp;&nbsp;This is a five-part series that includes the following posts: What is Pester and Why Should I Care? Learn about a new test framework for PowerShell called Pester Getting Started with Pester Learn how to get information back [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=75011"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75011\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=75011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=75011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=75011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}