Search code examples
powershellpester

How can I get the numer of failed tests from Invoke-Pester?


I have a few Pester tests running fine in the console, but I would like to run the tests automatically and send a message if any test fails. I read the option -EnableExit causes Invoke-Pester to return the numer of failed tests. But whenever I use -EnableExit the powershell console closes, regardless if a test failed. It is Pester version 4.7.3. PSVersion 5.1.

Is Invoke-Pester -EnableExit supposed to close the shell?
How do I get the number of failed tests?

runs fine:
Invoke-Pester -Script D:\tmp\PowerShell\dummy1.Tests.ps1

closes the shell window:
Invoke-Pester -Script D:\tmp\PowerShell\dummy1.Tests.ps1 -EnableExit

I expect to get an integer as output, but the console window closes.


Solution

  • You can get just the number of failed tests by doing this:

    (Invoke-Pester -Path D:\tmp\PowerShell\dummy1.Tests.ps1 -PassThru -Show None).FailedCount
    

    If you want other data (passed/skipped count, test results, etc), then pass the output to a variable, then process further:

    $testResults = Invoke-Pester -Path D:\tmp\PowerShell\dummy1.Tests.ps1 -PassThru -Show None