I'm using pester to test my custom cmdlets. The tests are end-to-end tests that spin up an environment; compile and load assemblies and execute tests on the dynamically compiled code.
I need to do the actual tests in a new PowerShell process (so the assemblies can be compiled and loaded each time) so I use start-job to run the tests in the back ground using start-job
and wait for the results with receive-job -wait
. Something like this:
Describe 'Run' {
It 'StartJobTest' {
start-job -script {invoke-pester .\MyTests.Tests.ps1} | receive-job -wait
}
}
Everything works fine except I would can't get any test success or failure status to be returned from the job so that the invoking pester test can be marked success or failure.
Does anyone know how to achieve this. I tried setting $global:PesterPreference.Run.Exit = $true
but this didn't make any difference.
Any help or suggestions gratefully received,
David
I think you need to use -PassThru switch:
Returns a custom object (PSCustomObject) that contains the test results. By default, Invoke-Pester writes to the host program, not to the output stream (stdout). If you try to save the result in a variable, the variable is empty unless you use the
PassThru
parameter.
Describe 'Run' {
It 'StartJobTest' {
$Results = Start-Job -Name Invoke-Pester -ScriptBlock {
Invoke-Pester -Path '.\MyTests.Tests.ps1' -PassThru
} | Receive-Job -Wait -AutoRemoveJob
$Results.FailedCount | Should -BeExactly 0
}
}
P.S. In Pester v5, this switch is replaced with ConfigurationProperty
Run.PassThru
: https://pester.dev/docs/migrations/v4-to-v5