Keith
Keith

Reputation: 791

-Verbose not working with my Pester Test in PowerShell

I wrote a pester test to check that certain folders and files exist. The pester test works great but I wanted to include suggestions for fixes if the test is called with the -Verbose option. But I can't seem to get the -Verbose parameter to the actual test.

Folder/File structure:

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

Below is just the top part of 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 }
  }
}

Upvotes: 2

Views: 2310

Answers (3)

Prasoon Karunan V
Prasoon Karunan V

Reputation: 3063

The -Verbose switch of the Invoke-Pester cmdlet is not available inside the test cases. You have to explicitly pass this for the test case to access.

Here is an example based on your script:

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

Upvotes: 1

Will I Am
Will I Am

Reputation: 2682

Instead of explictly passing the Verbose flag to test cases, you can also change the default value of VerbosePreference in scope:

$VerbosePreference = $Env:MyVerbosePreference

Then you can control it from outside:

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

Upvotes: 1

Mark Wragg
Mark Wragg

Reputation: 23395

Per the other answer, it doesn't seem to be possible to use Write-Verbose when running the script with the Invoke-Pester command. I think this may be because using the Invoke-Pester command means that you script is interpreted rather than directly executed by the PowerShell engine. The next best alternative would be to add in If statements that perform the same checks as your tests and then use Write-Host or Write-Warning to give instructions if they are negative. I have done that occasionally in the past.

You can however use -verbose if you are executing the script directly (e.g just directly running the *.tests.ps1 file). However to do so you need to add [cmdletbinding()] and a Param block to the top of your script:

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

Upvotes: 3

Related Questions