Writing automated module tests with Pester
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Click the toggle below to view the completed source code for this lesson:
GreetMe.Tests.ps1 Full Source Code
In this lesson, we're going to write an automated test that's going to validate our PowerShell module and make sure that everything is working. So that we know that we haven't broken the module in any of the places. So it's going to be really useful when we make changes to our module that we know that we haven't broken anything.
So what we're going to do here is go ahead and get rid of this to do. And we're going to start by removing. And reimporting the module so that it gets the latest set of code, because if we don't do this, what's going to happen is we're going to end up with a stale version of the code, even if we make changes to the files, because the module is already loaded.
It doesn't grab the new versions of the scripts. It just kind of caches them into their memory. So we have to remove the module and reimport it in order to get the latest set of code. So we're going to go and do that. And we'll make a comment, say, unload and reload the module. So it will use the latest code.
Oops, it is code. Alright, we'll go down the line and we're just going to say remove dash module and remove greet me.
And I'm going to say error action silently continue. And the reason why we want to silently continue is because if we run this for the first time, there's a chance it could throw an error, and we don't want the script to show errors if it's the first time we're running the test, just because it hasn't been imported yet.
After we've done that, we'll say import module,
and we'll say greet me. And I'm going to say dash verbose just so we get that extra information. So if I execute the script here, we have importing function, get greeting message and it removes the module and then here's our PSD one manifest file and our module file. Okay, next, what we're going to do is create our first test.
So test the module manifest, and we could just say. Test module manifest, but what we want to use is something called pester and this is something that should be installed by default on a PowerShell, but if you're not sure if you have pester installed, what you can do is just say import dash module and say pester just like that. And if it runs without an error, you're good to go. If it doesn't, you'll need to run install dash module. And install pester, but I already have that.
If we want to find more information, we can say find dash module. And we can format that as a list and hit star, and that'll give us all the information about Pester, as well as the description so we can learn a little bit more about it. But it's a framework for running tests to execute and validate PowerShell commands inside of PowerShell.
Alright, so this is something that we're going to use, and it uses a pretty similar framework to most automated testing that you'll encounter. If you want to see more about GitHub, we can visit the URL here. And we'll just take a look at that really quick, might as well.
So this is a GitHub page. And it just kind of gives us information about Pester and its current state and things like that. So I'm going to go ahead and close this and back to our PowerShell script. We're going to use the describe block. So describe, and then test, module, manifest. And I want to capitalize that.
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.