PowerShell BDD with Pester
Today we’ll be test talking about Pester with Adam Bertram, author of multiple Pluralsight courses on PowerShell, and founder of AdamTheAutomator.com. I’m a big believer in the fact that infrastructure code — just like test automation code — should follow the same rigor as your application code.
Pester is a unit testing framework that was built for people who want to take their PowerShell scripts and modules to whole different level, and really apply professional unit testing abilities to it.
If your or your team is performing any type of PowerShell development, you won’t want to miss this episode.
About Adam Bertram
Adam Bertram is an independent consultant, technical writer, trainer, and presenter. Adam specializes in consulting and evangelizing all things IT automation, mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 powershell.org PowerShell hero, and has numerous Microsoft IT pro certifications. He authors IT pro course content for Pluralsight, is a regular contributor to numerous print, and online publications and presents at various user groups and conferences.
Quotes & Insights from this Test Talk
- Pester is, as you said, a unit testing framework specifically for the PowerShell language. It was a framework built for people who want to take their PowerShell scripts and modules and such to a different level, and really apply professional unit testing abilities to PowerShell. Traditionally, PowerShell has just been known as a scripting language, but nowadays, a lot of companies are really depending on the code that's written in PowerShell for production, mission-critical things. Pester was built as a result of that to make sure to run tests against all those scripts and modules and all the orchestration that takes place, that PowerShell does to allow you to treat PowerShell as more of, as I refer to it, a “big boy” language. Instead of a “Yeah, yeah, yeah, that's nice” scripting language, taking PowerShell to a more professional language.
- Pester is it can be essentially just built with PowerShell. You can test a number of different things. It's a unit testing framework, so you can test the code in that script, or the module, but you can also do various integration or even acceptance testing with it. In a sense, it can do infrastructure testing, as well. It can make sure that the script that you just ran placed a file in a specific spot, make sure it got all the dependencies; it goes back and test the environment that way. It can be a really good tool, not only to do your unit testing, but also your integration testing and, what I like to refer to as “infrastructure testing,” which is to make sure your infrastructure is configured to how you think it should be.
- Pester is not quite as robust as a lot of other testing frameworks. Again, I came from an Ops background; I've never actually been a software developer, per se; but, yes, Pester does have assertions, it has mocking capabilities, and it has a lot of the things that you traditionally use with other unit testing framework
- I've used mocking extensively in my unit tests. It's really easy to do. All it is in Pester mock, is just a script block, a PowerShell script block. You just define it with your mock keyword, say “Mock” and then the function name.One of the things in Pester I really like is that it understands scoping. If you have a private function in your PowerShell module, you can actually define mocks and they understand your private functions, so it understands module scopes, script scope, and a lot of that stuff. As far as I know, it has a majority of the mocking functions, so you can define mocks, you can assert mocks, or call, you can do various parameter filters. If a PowerShell function was Call with a specific set of parameters, you could define a mock, versus not define a mock, and that sort of thing.
- Since Pester comes out of the box with Windows now – I believe it comes with Windows 10, and I think it comes with server 2016, now – so you have a version of Pester already on a Windows box. In order to get it, Pester is just a PowerShell module, and if you don't have a PowerShell module, you have PowerShell v.4 or later, you can use PowerShell Git.
- Considering the majority of the audience, as far as I'm concerned, in the community that I know, a lot of people are still brand new to Pester. As a result, there's a lot of newbies out there, and there's a lot of people that I've seen are really, really scared of Pester. It's either from Ops personnel, or even developers are like “Why do we need to test PowerShell?” I would say, if somebody is not using Pester today, the best place to go would be Pluralsight – there is a really good course on unit testing with Pester by Robert Cain; mine's coming out pretty soon. I've also done some articles on www.4sysops.com . I did seven or eight articles in a series on Pester, on getting started from the ground up, where you know nothing at all about Pester, and learning the concepts of unit testing, and how to apply those concepts to Pester. You can find those at sysops.com/members/adam-bertram . You can also check out my website, www.adamtheautomator.com.
Connect with Adam Bertram
May I Ask You For a Favor?
Thanks again for listening to the show. If it has helped you in any way, shape or form, please share it using the social media buttons you see on the page.
Additionally, reviews for the podcast on iTunes are extremely helpful and greatly appreciated! They do matter in the rankings of the show and I read each and every one of them.
Test Talks is sponsored by the fantastic folks at Sauce Labs. Try it for free today!