首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用返回为零结果的Pester

调用返回为零结果的Pester
EN

Stack Overflow用户
提问于 2019-11-25 10:23:51
回答 1查看 574关注 0票数 1

我有一个Pester脚本,它正在为我的API运行一些冒烟测试。当我运行Invoke-Pester时,我得到了Logo,测试运行良好。然而,在日志的末尾,我得到了Tests Passed: 0, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0,当我试图作为一个NUnitXML输出时,那里也没有结果。

命令:

代码语言:javascript
复制
Invoke-Pester -Script @{Path = 'Path\testscript.ps1'; Arguments = '200', 'application/json; charset=utf-8'} -OutputFormat NUnitXml -OutputFile ".\TestsResults.xml"

脚本:

代码语言:javascript
复制
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"
        }
    }

}

控制台日志:

代码语言:javascript
复制
    ____            __
   / __ \___  _____/ /____  _____
  / /_/ / _ \/ ___/ __/ _ \/ ___/
 / ____/  __(__  ) /_/  __/ /
/_/    \___/____/\__/\___/_/
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 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-25 21:13:05

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

go.ps1

代码语言:javascript
复制
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"

script.ps1

代码语言:javascript
复制
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"
        }
    }
}

然后:

代码语言:javascript
复制
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中删除script.ps1,然后得到:

代码语言:javascript
复制
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,一切都应该正常工作.

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59029653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档