0%

0/1 Lessons

Course Introduction

• 5min

0 / 2 lessons complete

Getting Started with Windows PowerShell

• 56min

0 / 8 lessons complete

Getting Help and Finding Commands

• 39min

0 / 6 lessons complete

PowerShell Command Syntax

• 33min

0 / 5 lessons complete

PowerShell Objects and Properties

• 35min

0 / 6 lessons complete

The PowerShell Pipeline

• 24min

0 / 2 lessons complete

PowerShell Providers

• 30min

0 / 5 lessons complete

PowerShell Arrays and Variables

• 28min

0 / 4 lessons complete

PowerShell Loops

• 19min

0 / 3 lessons complete

PowerShell Conditional Statements

• 11min

0 / 1 lessons complete

On Premises Lab Setup

• 36min

0 / 8 lessons complete

Basic Domain Administration with Windows PowerShell

• 2hr 27min

0 / 10 lessons complete

Send Emails with PowerShell

• 22min

0 / 2 lessons complete

PowerShell Desired State Configuration (DSC) Basics

• 1hr 48min

0 / 6 lessons complete

PowerShell Modules

• 58min

0 / 7 lessons complete

Powershell Challenges

• 1hr 55min

0 / 23 lessons complete

Course Conclusion

• 1min

0 / 1 lessons complete

Writing automated module tests with Pester

Instructions

Q&A (0)

Notes (0)

Resources (1)

Saving Progress...

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

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.

5 1 vote
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments