Level 1
0 / 100 XP

Writing automated module tests with Pester

Click the toggle below to view the completed source code for this lesson:

GreetMe.Tests.ps1 Full Source Code

Text
# If you don't have Pester installed, run this command # Install-Module -Name Pester -Force # This test code is written for PowerShell 5.1, if you're using Version 7, then the syntax will be wrong and you will receive errors # Output the $PSVersionTable to verify your version of PowerShell # Unload and reload the module so it will use the latest code Remove-Module GreetMe -ErrorAction SilentlyContinue Import-Module GreetMe -Verbose # Test the module manifest Describe "Module Manifest" { $moduleManifestPath = Join-Path $RootDir "..\\GreetMe.psd1" # Does the manifest file exist? It "Module Manifest Exists" { Test-Path $moduleManifestPath | Should Be $true } # Make sure the manifest file contains the information we want It "Is the module manifest valid?" { Test-ModuleManifest -Path $moduleManifestPath -ErrorVariable ManifestInvalid -ErrorAction SilentlyContinue $ManifestInvalid | Should Be $Null } } # Test the Get-GreetingMessage function Describe "Get-GreetingMessage" { It "Returns a valid greeting message" { $name = "John" $greeting = Get-GreetingMessage -Name $name $expectedGreeting = "Hello, $name!*" $greeting | Should Match $expectedGreeting } }

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 ahe…