簡體   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