Have Pester not collect Non-Terminating errors

Hi All, I’m running into this issue where I’m casting a variable to [int] type, but unfortunately for me the data being fed is not always in a correct format to succeed 100%. I’ve created a basic test to illustrate my issue:

Describe "Test Non-Terminating Error" {

    Context "Non-Terminating Error" {

        It "Non-Terminating Error" {

            [int]$Number = 'oc'
            $Test = 'Test'
            $obj = [PSCustomObject]@{
                Number = $Number
                Test   = $test

            $obj.Number | Should -BeNullOrEmpty
            $obj.Test   | Should -Be "Test"



When I run this test it always Fails and never processes any of the Assertions:

[-] Test Non-Terminating Error.Non-Terminating Error.Non-Terminating Error 21ms (20ms|1ms)FormatException: Input string was not in a correct format.PSInvalidCastException: Cannot convert value "oc" to type "System.Int32". Error: "Input string was not in a correct format."ArgumentTransformationMetadataException: Cannot convert value "oc" to type "System.Int32". Error: "Input string was not in a correct format."at , .\assets\_Test.Tests.ps1:11 Tests completed in 1.47s Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0
I've already tried:
  • Configuring Pester Should ErrorPreference to 'Continue'
  • Setting PowerShell Error Action Preference to 'SilentlyContinue' ($ErrorActionPreference = 'SilentlyContinue')
  • Try/Catch doesn't catch because its Non-Terminating
Is there a way to have pester ignore any non-terminating error that happens within the IT block and just execute the Assertions?

I’m running Pester 5.0.3 on PowerShell 5.1

Please Help!

Here the error will occur during the type conversion at [int]$Number = 'oc' . So you having a test for this line is better.
Its best to use [int]::TryParse() method which will not throw any error.