Chapter 20. Pestering your script
As we move into a DevOps-y world, one of the things you’ll need to start thinking about is how you’ll test your scripts. Here’s the deal: Nobody likes a broken script in production. And although you might run a few tests on your script, you—or someone else—might also modify your script at some point, necessitating a retest. Or, you might find some odd condition under which your script fails—well, you certainly don’t want to forget to test that condition again in the future, do you? In this chapter, we’ll talk about automated unit testing for PowerShell scripts.
Here’s where we want to get you:
- You write some code, or modify some old code.
- You check your code into a source control repository.
- The repository triggers a continuous integration pipeline. Usually incorporating third-party tools (TeamCity is the one used on PowerShell.org’s free build service, for example), the pipeline builds out a virtual machine to test your script. The pipeline copies your script into the virtual machine and runs several automated tests. If the tests fail, you get an email telling you what happened.
- If the tests pass, your code is deployed to a deployment repository (maybe PowerShellGallery.com, or maybe a private repo), making it available for production.