Search code examples
powershellazureazure-powershellpester

Dealing with Arrays in Powershell (testing with Pester)


I have a little trouble understanding the way to implement this process. I want to achieve a total count in the score so that if a test successfully passes or fails it can be added into an array. That array will be counted in the length.

This is my code as an example:

#This stores the array of the number of passed and failed test
$passed = @()
$failed = @() 

Describe "Template Syntax" {

    It "Has a JSON template" {        
       $fileLocation = "$here\azuredeploy.json" 
       $fileCheck = $fileLocation | Test-Path

        if ($fileCheck -eq $true) {  $passed = $passed + 1
        Write-Host "1st file exist " }
        if ($fileCheck -eq $false) { $failed = $failed + 1
        Write-Host "1st file does exist" }

        }

        It "Has a parameters file" {        
     $fileLocation ="$here\azuredeploy.parameters*.json"

      $fileCheck = $fileLocation | Test-Path

        if ($fileCheck -eq $true) {  $passed = $passed + 1; 
        Write-Host "2nd file exist "}
        if ($fileCheck -eq $false) {  $failed = $failed + 1
        Write-Host "2nd file does exist" }

        } 

        PrintArgs

        }

function PrintArgs(){
Write-Host -ForegroundColor yellow "Passed: $($passed.Length) Failed: $($failed.Length)"
   }

Is there a different way or another approach that I can do to achieve this? I know that pester does it automatically, however, I want to use a Powershell script to test.


Solution

  • Your Pester tests aren't really Pester tests unless you include a Should assertion. I think you should rewrite your tests as follows:

    Describe "Template Syntax" {
        $fileLocation = "$here\azuredeploy.json" 
        $fileCheck = $fileLocation | Test-Path
    
        It "Has a JSON template" {        
            $fileCheck | Should -Be $true
        }
    
        $fileLocation ="$here\azuredeploy.parameters*.json"
        $fileCheck = $fileLocation | Test-Path
    
        It "Has a parameters file" {        
            $fileCheck | Should -Be $true
        } 
    }
    

    If you then run this with Invoke-Pester you get a summary of passed and failed test counts at the end automatically. If you need to access these values, you can use -PassThru to return them to a variable. For example:

    $Results = Invoke-Pester .\your.tests.ps1 -PassThru
    

    Then you can get the number of passed and failed tests as follows:

    $Results.PassedCount
    $Results.FailedCount
    

    If you genuinely want to use your own counters (which would mean maintaining lots of unnecessary logic) you could do as follows:

    $Passed = 0
    $Failed = 0
    
    Describe "Template Syntax" {
        $fileLocation = "$here\azuredeploy.json" 
        $fileCheck = $fileLocation | Test-Path
    
        It "Has a JSON template" {        
            $fileCheck | Should -Be $true
        }
    
        if ($fileCheck -eq $true) {  $Passed++ } else { $Failed++ }
    
        $fileLocation ="$here\azuredeploy.parameters*.json"
        $fileCheck = $fileLocation | Test-Path
    
        It "Has a parameters file" {        
            $fileCheck | Should -Be $true
        } 
    
        if ($fileCheck -eq $true) {  $Passed++ } else { $Failed++ }
    
        Write-Host -ForegroundColor yellow "Passed: $Passed Failed: $Failed"
    }
    

    Note that the Describe block acts as a kind of script scope, so you have to print the values of $Passed and $Failed within the Describe to get at the values.