Table of Contents

In my previous posts, I explored how to develop, test, and publish Bicep modules for Azure Bicep Registry, and how to enhance compliance checks with PSRule Bicep. Now, we’re diving into using Pester - a versatile PowerShell testing framework - to write custom unit tests for Azure Bicep modules, complementing PSRule’s best-practice checks and giving more granular insights into your module configurations.

Why Add Pester Tests?

While PSRule excels at validating compliance and best practices, Pester provides the flexibility to:

  • Target specific Bicep components.
  • Validate default configurations.
  • Test parameter handling.

By adding Pester to your testing toolkit, you gain detailed insights and ensure your Bicep modules are reliable, adaptable, and maintainable.

Key Benefits of Pester in Bicep Module Testing:

  • Granular Testing: Test individual properties like SKU types, parameter validation, and output values.
  • Immediate Feedback: Quickly detect issues during development and maintain high-quality modules.
  • Custom Validation: Tailor tests to your organization’s unique standards and requirements.

Setting Up Pester in Your DevOps Pipeline

  1. Create Pester Test Files: Place .ps1 files in your module’s test folder (/tests/pester) with specific tests for each module.

  2. Sample Pester Test Structure:

    Describe "Storage Account Module Tests" {
        Context "Parameter Validation" {
            It "Should use Standard_LRS for SKU" {
                $params = @{
                    skuName = 'Standard_LRS'
                    location = 'eastus'
                }
                $result = Test-BicepModule -Path './modules/storageAccount/main.bicep' -Params $params
                $result | Should -Contain 'Standard_LRS'
            }
        }
    
        Context "Output Verification" {
            It "Should return correct storage account ID" {
                $result = Get-BicepModuleOutput -Path './modules/storageAccount/main.bicep' -Params $params
                $result | Should -MatchRegex "^/subscriptions/.+/resourceGroups/.+/providers/Microsoft.Storage/storageAccounts/"
            }
        }
    }
    
  3. Add a Pester Job in Your Azure DevOps Pipeline: Incorporate the Pester job into your pipeline with these steps:

    - job: RunPesterTests
      displayName: "Run Pester Unit Tests for Bicep Module"
      pool:
        vmImage: 'windows-latest'
      steps:
        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            script: |
              # Install Pester if not available
              if (-not (Get-Module -ListAvailable -Name Pester)) {
                Install-Module Pester -Force -Scope CurrentUser
              }
              Invoke-Pester -Script './modules/**/tests/pester' -OutputFormat NUnitXml -OutputFile './pester-results.xml'
    
  4. Integrate PSRule for Full Validation: Run PSRule checks after Pester tests to ensure that all best-practice and compliance guidelines are also met.

Conclusion

Combining Pester for detailed unit tests with PSRule for compliance checks provides a robust testing suite for your Azure Bicep modules. This integration ensures your modules are reliable, properly configured, and adhere to industry and organizational standards.

By taking this approach, you’ll not only boost the quality of your Bicep modules but also create confidence in their deployment and performance.