Table of Contents – Designing for Testing
In this chapter, we learned that Pester is going to make some assumptions about the code that we write. Therefore, in my mind, it’s going to be best if we learn what Pester is expecting and do that — well, those of us reading The Pester Book, at least.
This means we need to work with .ps1 files and functions, send our function output to the pipeline — good info — and that the results of our functions are consistent. Makes sense, right? If we’re testing that the value should be 5, then it’s not helpful if our code sometimes produces a random 6 (and there’s no test for that). This was actually better stated by Adam, when he wrote, “…for a given input, you get the same output.” His further elaborations were quite good, as well. As someone that uses iBooks and the ePub version, I highlighted a decent amount of the additional explanation.
Moving on, the book discussed that Pester would prefer that input be via parameters. If you’re not there yet, then get there, and stop settling for hard coded values, Read-Host prompts, or a function’s ability to read from the global scope when it can’t find what it wants in its current scope. If you’re not writing in functions — and this is really what this is about — then get there too. I spent a few posts before I started The Pester Book reviews trying to provide reasons why someone would want to do this (outside the community saying you should). It really does make your PowerShell life easier.
The chapter continues on by discussing output (think objects), that actions aren’t output (it makes sense when you read it), and finally, to keep commands as scoped as possible, as they’re easiest to test that way. This can be tough to teach, because people think whole project when they should be thinking task. But seriously, write in functions only, and keep them to a single purpose. Next, we’ll be back with a chapter on Describe Blocks.
This is getting good… I’ve done some Pester authoring previously, but I’ve never been taught it, so I’m looking forward to all those things I’m doing wrong, or didn’t know, because I didn’t give myself a complete training. Well, that’s the purpose of the book. Again, Describe blocks in our next review.