繁体   English   中英

调用 Pester 返回零结果

[英]Invoke Pester returning with zero results

我有一个 Pester 脚本,正在为我的 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。 答案是从 script.ps1 中删除Import-Module ,然后你会得到:

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,事情应该可以正常工作......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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