Powershell Pester tests stopping after the first assertion failure

121 views Asked by At

I am trying to run Pester tests containing multiple Should assertions. I want all failing assertions to be collected and reported at the end of the test, just as described here in the Pester Documentation.

Snippet of my test setup:

Describe 'GitHelperMethods' {

    BeforeAll{
        # Git setup
        git config --global init.defaultBranch master
        git config --global user.email "[email protected]"
        git config --global user.name "testgit"

        # Using local pwd
        $validGitPath = "$PWD/tmp_git_dir/"

        # Initialize git with a file and new commit
        New-Item -Path $validGitPath\test_file.txt -type file -Force
        git init $validGitPath
        git -C $validGitPath add $validGitPath\test_file.txt
        git -C $validGitPath commit -m "initial commit"
        . "$PSScriptRoot/../GitHelperMethods.ps1"
    }

    It 'Has one file change in the working tree' {
        # arrange
        Write-Output "change file contents" > $validGitPath\test_file.txt
        
        # act
        $gitHelper = [GitHelper]::new($validGitPath)
        
        # assert
        $changedFiles = $gitHelper.GetChangedFiles()
        $changedFiles.Length | Should -Be 1000 -Because "Only 1 file was changed and not expecting a value more than 1"
        $changedFiles | Should -Contain "test_fileeeee.txt" 
    }

    ...
}

But instead, the behaviour I am seeing is it only displays the first failing assertion error and the remaining assertions are not triggered. Pester test output

Using the example provided on PesterConfigurations documentation, I tried explicitly setting the Should.ErrorAction = 'Continue' and then calling Invoke-Pester with the custom Pester configuration like this:

$config = [PesterConfiguration]@{
    Run = @{
        Path = 'C:\pester_tests\my_tests.Tests.ps1'
    }
    Should = @{
        ErrorAction = 'Continue'
    }
    Output = @{
        Verbosity = 'Detailed'
    }
}

invoke-Pester -Configuration $config

It still fails on the first test assertion and ignores the remaining assertions. Why?

  • PSVersion - 7.4.0
  • Pester - 5.5.0
0

There are 0 answers