gpt4 book ai didi

powershell - 调用 Pester 返回零结果

转载 作者:行者123 更新时间:2023-12-03 01:30:11 24 4
gpt4 key购买 nike

我有一个 Pester 脚本,正在为我的 API 运行一些冒烟测试。当我运行 Invoke-Pester 时,我得到了 Logo 并且测试运行良好。但是在日志的末尾我得到 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

最佳答案

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

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

关于powershell - 调用 Pester 返回零结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59029653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com