【问题标题】:-Verbose not working with my Pester Test in PowerShell-Verbose 不适用于我在 PowerShell 中的 Pester 测试
【发布时间】:2017-09-08 17:33:03
【问题描述】:

我编写了一个纠缠测试来检查某些文件夹和文件是否存在。纠缠测试效果很好,但如果使用 -Verbose 选项调用测试,我想包括修复建议。但我似乎无法将 -Verbose 参数用于实际测试。

文件夹/文件结构:

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

【问题讨论】:

    标签: powershell powershell-3.0 verbose pester


    【解决方案1】:

    根据其他答案,使用 Invoke-Pester 命令运行脚本时似乎无法使用 Write-Verbose。我认为这可能是因为使用Invoke-Pester 命令意味着您的脚本是由PowerShell 引擎解释而不是直接执行的。下一个最佳选择是添加执行与您的测试相同的检查的If 语句,然后使用Write-HostWrite-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 }
      }
    }
    

    【讨论】:

      【解决方案2】:

      除了将 Verbose 标志显式传递给测试用例之外,您还可以在范围内更改 VerbosePreference 的默认值:

      $VerbosePreference = $Env:MyVerbosePreference
      

      然后你就可以从外部控制它了:

      $Env:MyVerbosePreference= 'Continue'
      Invoke-Pester ...
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多