0
0
PowerShellscripting~20 mins

Pester testing framework basics in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Pester Testing Framework Basics
📖 Scenario: You are a PowerShell script writer who wants to make sure your functions work correctly. You will learn how to write simple tests using the Pester testing framework. Pester helps you check if your code behaves as expected, just like checking if a recipe turns out well every time you cook.
🎯 Goal: Build a basic Pester test script that tests a simple PowerShell function. You will create a function, write a test for it, and run the test to see the result.
📋 What You'll Learn
Create a PowerShell function named Get-Greeting that returns the string 'Hello, World!'.
Write a Pester test block that checks if Get-Greeting returns 'Hello, World!'.
Run the Pester test and display the test result.
💡 Why This Matters
🌍 Real World
Pester is widely used by system administrators and developers to automatically check that their PowerShell scripts work as expected before running them in production.
💼 Career
Knowing how to write and run Pester tests is a valuable skill for IT professionals who write PowerShell scripts, as it helps prevent errors and improves script reliability.
Progress0 / 4 steps
1
Create the Get-Greeting function
Write a PowerShell function named Get-Greeting that returns the string 'Hello, World!'. Use the return keyword inside the function.
PowerShell
Need a hint?

Use the function keyword to define a function. Inside it, use return 'Hello, World!' to send back the greeting.

2
Write a Pester test for Get-Greeting
Add a Pester Describe block named 'Get-Greeting Tests'. Inside it, add a It block named 'returns Hello, World!' that uses Get-Greeting and checks if the output equals 'Hello, World!' using Should -Be 'Hello, World!'.
PowerShell
Need a hint?

Use Describe to group tests and It to define a test case. Use the pipeline | to send the function output to Should -Be for checking.

3
Run the Pester test
Run the Pester test by calling Invoke-Pester in the script. This will execute the test block and show the test results.
PowerShell
Need a hint?

Simply add Invoke-Pester at the end of your script to run all tests defined in the file.

4
Display the test result output
Run the script and observe the output. The test result should show that the test passed with a green check mark and a message like 1 Passed.
PowerShell
Need a hint?

Look for the line that says 1 Passed in green color in the output. This means your test worked!