Pester framework for Powershell testing – part 1

Today let’s talk a little bit about framework for Powershel script testing called Pester.

Pester is quite fresh framework which is user friendly and very easy in implementation.
I want to share with you basic example of using Pester commandlets.

Let’s focus on three basic commands
Describe – define name of the test group under which Context and It blocks exists.
Context – define grouping of It blocks. Usually used to group specific types of test (in my example services, disk space, RAM).
It – should be used inside of Describe or Context commands. Command included in It block will throw an error in case that test of command will fail. For that Should command is used – it compare result of code in It block with expected result.

Script:

Describe "Check workstation" {
    Context "Check service status"{
        It "VSS service status - stopped" {
            (Get-Service -Name VSS).Status| Should Be Stopped
        }

        It "Firewall service status - running" {
            (Get-Service -Name MpsSvc).Status| Should Be Running
        }
    }
    
    Context "Check free disk space"{
        It "C drive free space greater than 20 GB" {
            (Get-WmiObject win32_logicaldisk -Filter "Drivetype=3" | Where-Object {$_.DeviceID -eq "C:"}).FreeSpace/1GB | Should BeGreaterThan 20
        }

        It "D drive free space greater than 20 GB" {
            (Get-WmiObject win32_logicaldisk -Filter "Drivetype=3" | Where-Object {$_.DeviceID -eq "D:"}).FreeSpace/1GB | Should BeGreaterThan 20
        }
    }
    
    Context "Check RAM usage"{
        It "Free RAM greater than 4GB" {
            (Get-Ciminstance Win32_OperatingSystem | Select-Object FreePhysicalMemory).FreePhysicalMemory/1mb | Should BeGreaterThan 4
        }

    }
   
}

Result:

Next part of this series will include other functionality of Pester.
If you want to know more about Pester, go to github Wiki.

I hope it will be usefull for some of you 🙂
Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.