简体   繁体   中英

Return test result from pester test run start-job

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
    }
}

PS In Pester v5, this switch is replaced with ConfigurationProperty Run.PassThru : https://pester.dev/docs/migrations/v4-to-v5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM