Return pester 测试的测试结果 运行 start-job

Return test result from pester test run start-job

我正在使用 pester 来测试我的自定义 cmdlet。这些测试是启动环境的端到端测试;编译和加载程序集并对动态编译的代码执行测试。

我需要在新的 PowerShell 进程中进行实际测试(这样每次都可以编译和加载程序集)所以我使用 start-job 运行 在后台使用 start-job 并使用 receive-job -wait 等待结果。像这样:

Describe 'Run' {
        It 'StartJobTest'  {
            start-job -script {invoke-pester .\MyTests.Tests.ps1} | receive-job -wait
        }
}

一切正常,除了我无法从作业返回任何测试成功或失败状态,以便调用 pester 测试可以标记为成功或失败。

有谁知道如何实现这个。我尝试设置 $global:PesterPreference.Run.Exit = $true 但这没有任何区别。

非常感谢收到的任何帮助或建议,

大卫

我认为你需要使用 -PassThru 开关:

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。在 Pester v5 中,此开关被替换为 ConfigurationProperty Run.PassThruhttps://pester.dev/docs/migrations/v4-to-v5