-Verbose在PowerShell中

问题描述:

不与我纠缠测试工作,我写了一个纠缠测试,以检查某些文件夹和文件存在。 pester测试很好,但如果使用-Verbose选项调用测试,我想包含修复建议。但我似乎无法获得实际测试的-Verbose参数。-Verbose在PowerShell中

文件夹/文件结构:

Custom-PowerShellModule 
    | Custom-PowerShellModule.psd1 
    | Custom-PowerShellModule.psm1 
    \---Tests 
      Module.Tests.ps1 

以下仅仅是纠缠考验的顶部:

$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 } 
    } 
} 

-Verbose switch of调用-Pester`的cmdlet是不是在侧可用的测试案例,你有明确地通过这个测试用例来访问。

下面是你的脚本的例子。

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 }} 

问候, Prasoon

每对方的回答,这似乎并不可能与Invoke-Pester命令运行脚本时要使用Write-Verbose。我认为这可能是因为使用Invoke-Pester命令意味着脚本被解释而不是直接由PowerShell引擎执行。下一个最好的替代方案是在执行相同的检查你的测试If语句添加,然后用Write-HostWrite-Warning发出指令,如果它们是消极的。过去我偶尔会这样做。

但是,您可以使用-verbose如果你是直接执行脚本(如只需直接运行* .tests.ps1文件)。但是为此,您需要将[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 } 
    } 
}