2017-04-12 2 views
1

特定のフォルダとファイルが存在するかどうかを確認するpesterテストを作成しました。 pesterテストは素晴らしいですが、-Verboseオプションを指定してテストを呼び出すと、修正候補を追加したかったのです。しかし、私は実際のテストに-Verboseパラメータを取得することはできません。-VerboseがPowerShellのPester Testで動作しない

フォルダ/ファイル構造:

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

答えて

0

-Verbose switch of起動-Pester`コマンドレットは、あなたが持っている、テストケース側では使用できません。以下は

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

はせびるテストのちょうど上の部分ですテストケースを明示的に渡してアクセスします。

以下は、あなたのスクリプトの例です。

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

よろしく、他の答えは1 Prasoon

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 } 
    } 
} 
関連する問題