调用 Pester 返回零结果

Invoke Pester returning with zero results

我有一个纠缠脚本,其中 运行 为我的 API 进行了一些冒烟测试。当我 运行 Invoke-Pester 时,我得到徽标和测试 运行 很好。但是在日志的末尾我得到 Tests Passed: 0, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0 并且当我尝试输出为 NUnitXML 时也没有结果

命令:

Invoke-Pester -Script @{Path = 'Path\testscript.ps1'; Arguments = '200', 'application/json; charset=utf-8'} -OutputFormat NUnitXml -OutputFile ".\TestsResults.xml"

脚本:

param(
    [string]$HttpCode,
    [string]$ContentType
)

Import-Module Pester -Force

Describe 'API Smoke Tests' { 
    Context "Direct Endpoint Smoke Test: $Uri" {
        It 'HTTP Code Should be equal to "200"' {
            $HttpCode | Should -Be 200
        }
        It 'Content Type Should be equal to "application/json; charset=utf-8"' {
            $ContentType | Should -Be "application/json; charset=utf-8"
        }
    }

}

控制台日志:

    ____            __
   / __ \___  _____/ /____  _____
  / /_/ / _ \/ ___/ __/ _ \/ ___/
 / ____/  __(__  ) /_/  __/ /
/_/    \___/____/\__/\___/_/
Pester v4.9.0
Executing all tests in 'Path\testscript.ps1'

Executing script Path\testscript.ps1

Describing API Smoke Tests

  Context Direct Endpoint Smoke Test: 
    [+] HTTP Code Should be equal to "200" 10ms
    [+] Content Type Should be equal to "application/json; charset=utf-8" 6ms
Tests completed in 1.59s
Tests Passed: 0, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0 

我可以使用以下脚本重现您的问题:

去.ps1

Import-Module -Name ".\Pester-4.9.0\Pester.psd1"

Invoke-Pester -Script @{
                  Path = ".\script.ps1"
                  Arguments = @( "200", "application/json; charset=utf-8" )
              } `
              -OutputFormat "NUnitXml" `
              -OutputFile   ".\Results.xml"

脚本.ps1

param
(
    [string] $HttpCode,
    [string] $ContentType
)

Import-Module -Name .\Pester-4.9.0\Pester.psd1 -Force

Describe 'API Smoke Tests' { 
    Context "Direct Endpoint Smoke Test: $Uri" {
        It 'HTTP Code Should be equal to "200"' {
            $HttpCode | Should -Be 200
        }
        It 'Content Type Should be equal to "application/json; charset=utf-8"' {
            $ContentType | Should -Be "application/json; charset=utf-8"
        }
    }
}

然后:

C:\> powershell .\go.ps1
    ____            __
   / __ \___  _____/ /____  _____
  / /_/ / _ \/ ___/ __/ _ \/ ___/
 / ____/  __(__  ) /_/  __/ /
/_/    \___/____/\__/\___/_/
Pester v4.9.0
Executing all tests in '.\script.ps1'

Executing script .\script.ps1

Describing API Smoke Tests

  Context Direct Endpoint Smoke Test:
    [+] HTTP Code Should be equal to "200" 114ms
    [+] Content Type Should be equal to "application/json; charset=utf-8" 7ms
Tests completed in 0.99s
Tests Passed: 0, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0

备注 - Tests Passed: 0

问题是您从已经 运行Pester 中的脚本中导入 Pester。答案是从脚本中删除 Import-Module。ps1 然后你得到:

C:\> powershell .\go.ps1
    ____            __
   / __ \___  _____/ /____  _____
  / /_/ / _ \/ ___/ __/ _ \/ ___/
 / ____/  __(__  ) /_/  __/ /
/_/    \___/____/\__/\___/_/
Pester v4.9.0
Executing all tests in '.\script.ps1'

Executing script .\script.ps1

  Describing API Smoke Tests

    Context Direct Endpoint Smoke Test:
      [+] HTTP Code Should be equal to "200" 106ms
      [+] Content Type Should be equal to "application/json; charset=utf-8" 5ms
Tests completed in 623ms
Tests Passed: 2, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0

备注 - Tests Passed: 2

认为 您看到的症状是 Pester 累积测试结果的一部分 - 它将它们存储在全局状态中,因此 可能 正在发生的事情实际上是 运行 在 script.ps1 Pester 模块中进行的测试(这就是您从中看到的测试输出),但最后的测试摘要来自 go.ps1 Pester 模块,其中零测试有 运行...

虽然这只是推测 - 最终,不要从您的 Pester 测试中导入 Pester,事情应该会正常工作...