[英]-Verbose not working with my Pester Test in PowerShell
我写了一个纠缠测试来检查某些文件夹和文件是否存在。 纠缠测试效果很好,但如果使用 -Verbose 选项调用测试,我想包括修复建议。 但我似乎无法将 -Verbose 参数用于实际测试。
文件夹/文件结构:
Custom-PowerShellModule
| Custom-PowerShellModule.psd1
| Custom-PowerShellModule.psm1
\---Tests
Module.Tests.ps1
以下只是pester测试的顶部部分:
$Here = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"
Describe "Module Minimum Requirements Tests. Use -Verbose for Suggested Fixes" -Tags Module {
Context "Test: Verify File Counts = 1" {
Write-Verbose "If you receive an error, verify there is only 'ONE' PSD1 File and only 'ONE' PSM1 File."
It "There is only one PSD1 file" { (Get-ChildItem "$Here\..\" *.psd1).count | Should be 1 }
It "There is only one PSM1 file" { (Get-ChildItem "$Here\..\" *.psm1).count | Should be 1 }
}
}
根据另一个答案,在使用Invoke-Pester
命令运行脚本时似乎不可能使用Write-Verbose
。 我认为这可能是因为使用Invoke-Pester
命令意味着您的脚本被 PowerShell 引擎解释而不是直接执行。 下一个最佳选择是添加执行与测试相同的检查的If
语句,然后使用Write-Host
或Write-Warning
给出否定的指示。 过去我偶尔会这样做。
但是,如果您直接执行脚本(例如直接运行 *.tests.ps1 文件),则可以使用-verbose
。 但是,要这样做,您需要将[cmdletbinding()]
和一个 Param 块添加到脚本的顶部:
[cmdletbinding()]
Param()
$Here = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"
Describe "Module Minimum Requirements Tests. Use -Verbose for Suggested Fixes" -Tags Module {
Context "Test: Verify File Counts = 1" {
Write-Verbose "If you receive an error, verify there is only 'ONE' PSD1 File and only 'ONE' PSM1 File."
It "There is only one PSD1 file" { (Get-ChildItem "$Here\..\" *.psd1).count | Should be 1 }
It "There is only one PSM1 file" { (Get-ChildItem "$Here\..\" *.psm1).count | Should be 1 }
}
}
Invoke-Pester
cmdlet 的-Verbose
开关在测试用例中不可用。 您必须显式地传递它才能访问测试用例。
这是基于您的脚本的示例:
Param([Bool]$Verbose)
Describe "Module Minimum Requirements Tests. Use -Verbose for Suggested Fixes" -Tags Module {
Context "Test: Verify File Counts = 1" {
Write-Verbose "If you receive an error, verify there is only 'ONE' PSD1 File and only 'ONE' PSM1 File." -Verbose:$Verbose
It "There is only one PSD1 file" { (Get-ChildItem "$Here\..\" *.psd1).count | Should be 1 }
It "There is only one PSM1 file" { (Get-ChildItem "$Here\..\" *.psm1).count | Should be 1 }
}
}
Invoke-Pester -Script @{Path='path' ; Parameters = @{ Verbose = $True }}
您还可以在范围内更改 VerbosePreference 的默认值,而不是显式地将 Verbose 标志传递给测试用例:
$VerbosePreference = $Env:MyVerbosePreference
然后你可以从外部控制它:
$Env:MyVerbosePreference= 'Continue'
Invoke-Pester ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.