A unit test is a way of testing a unit - the smallest piece of code that can be logically isolated in a system.

What about unit testing on IaC, after all it’s a code. Yes we need to write unit test cases for them as well. Pester Test help us here.

In this post I will walk you through, how you can use Pester Testing framework to write unit test cases for IaC.

Pester Test

Pester is an open source unit testing framework developed specifically to test PowerShell code. It is a PowerShell module that is used to write tests for PowerShell code to confirm that what it does is what you expect. In a nutshell, Pester is the code that’s written “on top of” your code to act as quality assurance. Fortunately, Pester itself is written in PowerShell, so you don’t have to be a software developer to learn how to use it.

Since Pester is just a PowerShell module, we can easily download and install it from the PowerShell Gallery. Downloading from the PowerShell Gallery will get you the latest version.

Install Module

 Install-Module -Name Pester

You can use any PS editor to write Pester test.

Structure

Describe 'Describe level'{
    context 'context level'{
        it 'it level'{            
        }
    }
}

sample template I am using is available @ 101-webapp-custom-deployment-slots

Unit Test

## Get template location
$here = Split-Path -Parent $MyInvocation.MyCommand.Path

Describe "Template: 101-webapp-custom-deployment-slots" -Tags Unit, azuredeploy {    
    Context "azuredeploy" {        
        It "Has a valid JSON template syntax" {        
            "$here\azuredeploy.json" | Should Exist
        }        
        It "Converts from azuredeploy.JSON and has the expected template schema" {
        $expectedProperties = '$schema',
                            'contentVersion',
                            'parameters', 
                            'variables',     
                            'resources',
                            'outputs'
        $templateProperties = (get-content "$here\azuredeploy.json" | ConvertFrom-Json -ErrorAction SilentlyContinue) | Get-Member -MemberType NoteProperty | % Name
        $templateProperties | Should Be $expectedProperties
        }
        It "parameters file Exist " {        
                "$here\$parmFile" | Should Exist
        }  
        It "Converts from parameter files and has the expected template schema" {
        $expectedProperties = '$schema',
                            'contentVersion',
                            'parameters'          
        $templateProperties = (get-content "$here\$paramFile" | ConvertFrom-Json -ErrorAction SilentlyContinue) | Get-Member -MemberType NoteProperty | % Name
        $templateProperties | Should Be $expectedProperties
            } 
        }
    }
    Context "azuredeploy Template Resources" {        
        It "Creates the expected Azure resources" {
            $expectedResources='Microsoft.Web/serverfarms',                                                                             'Microsoft.Web/sites',                                                                                 'Microsoft.Web/sites/slots'  
        $templateResources = (get-content "$here\Templates\azuredeploy.json" | ConvertFrom-Json -ErrorAction SilentlyContinue).Resources.type
        $templateResources | Should Be $expectedResources
        }
    } 
}

Save this file as Filename.Test.ps1

Execute to test locally

Invoke-Pester .\Filename.Test.ps1

Integrate with VSTS CI pipeline Pester Unit Build Task

Related Posts

About Ajeet Chouksey

With a robust background spanning more than 18 years, I am an adept Azure and Azure DevOps architect and engineer, dedicated to crafting Azure-centric solutions that prioritize customer requirements and agile methodologies. My expertise encompasses steering extensive cloud migration initiatives and advocating for Azure best practices, all aimed at streamlining costs and steering multinational teams towards success. Fueled by a passion for technological innovation, I am committed to perpetual learning, constantly advancing my proficiency in Azure, AI, MLOps, and Product Management to stay at the forefront of the industry..