Search code examples
powershellpester

Invoke Pester returning with zero results


I have a Pester Script with is running a few smoke tests for my API. When I run the Invoke-Pester, I get the Logo and the tests run fine. However at the end of the log I get Tests Passed: 0, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0 and when I try to output as a NUnitXML there are not results in there either

Command:

Invoke-Pester -Script @{Path = 'Path\testscript.ps1'; Arguments = '200', 'application/json; charset=utf-8'} -OutputFormat NUnitXml -OutputFile ".\TestsResults.xml"

Script:

param(
    [string]$HttpCode,
    [string]$ContentType
)

Import-Module Pester -Force

Describe 'API Smoke Tests' { 
    Context "Direct Endpoint Smoke Test: $Uri" {
        It 'HTTP Code Should be equal to "200"' {
            $HttpCode | Should -Be 200
        }
        It 'Content Type Should be equal to "application/json; charset=utf-8"' {
            $ContentType | Should -Be "application/json; charset=utf-8"
        }
    }

}

Console Log:

    ____            __
   / __ \___  _____/ /____  _____
  / /_/ / _ \/ ___/ __/ _ \/ ___/
 / ____/  __(__  ) /_/  __/ /
/_/    \___/____/\__/\___/_/
Pester v4.9.0
Executing all tests in 'Path\testscript.ps1'

Executing script Path\testscript.ps1

Describing API Smoke Tests

  Context Direct Endpoint Smoke Test: 
    [+] HTTP Code Should be equal to "200" 10ms
    [+] Content Type Should be equal to "application/json; charset=utf-8" 6ms
Tests completed in 1.59s
Tests Passed: 0, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0 

Solution

  • I can reproduce your issue with the following scripts:

    go.ps1

    Import-Module -Name ".\Pester-4.9.0\Pester.psd1"
    
    Invoke-Pester -Script @{
                      Path = ".\script.ps1"
                      Arguments = @( "200", "application/json; charset=utf-8" )
                  } `
                  -OutputFormat "NUnitXml" `
                  -OutputFile   ".\Results.xml"
    

    script.ps1

    param
    (
        [string] $HttpCode,
        [string] $ContentType
    )
    
    Import-Module -Name .\Pester-4.9.0\Pester.psd1 -Force
    
    Describe 'API Smoke Tests' { 
        Context "Direct Endpoint Smoke Test: $Uri" {
            It 'HTTP Code Should be equal to "200"' {
                $HttpCode | Should -Be 200
            }
            It 'Content Type Should be equal to "application/json; charset=utf-8"' {
                $ContentType | Should -Be "application/json; charset=utf-8"
            }
        }
    }
    

    and then:

    C:\> powershell .\go.ps1
        ____            __
       / __ \___  _____/ /____  _____
      / /_/ / _ \/ ___/ __/ _ \/ ___/
     / ____/  __(__  ) /_/  __/ /
    /_/    \___/____/\__/\___/_/
    Pester v4.9.0
    Executing all tests in '.\script.ps1'
    
    Executing script .\script.ps1
    
    Describing API Smoke Tests
    
      Context Direct Endpoint Smoke Test:
        [+] HTTP Code Should be equal to "200" 114ms
        [+] Content Type Should be equal to "application/json; charset=utf-8" 7ms
    Tests completed in 0.99s
    Tests Passed: 0, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0
    

    Note - Tests Passed: 0

    The problem is that you're importing Pester from within a script that's already running in Pester. The answer is to remove the Import-Module from script.ps1 and then you get:

    C:\> powershell .\go.ps1
        ____            __
       / __ \___  _____/ /____  _____
      / /_/ / _ \/ ___/ __/ _ \/ ___/
     / ____/  __(__  ) /_/  __/ /
    /_/    \___/____/\__/\___/_/
    Pester v4.9.0
    Executing all tests in '.\script.ps1'
    
    Executing script .\script.ps1
    
      Describing API Smoke Tests
    
        Context Direct Endpoint Smoke Test:
          [+] HTTP Code Should be equal to "200" 106ms
          [+] Content Type Should be equal to "application/json; charset=utf-8" 5ms
    Tests completed in 623ms
    Tests Passed: 2, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0
    

    Note - Tests Passed: 2

    I think the symptom you're seeing is part of how Pester accumulates test results - it stores them in a global state, so maybe what's happening is something along the lines of the tests are actually running in the script.ps1 Pester module (and that's what you're seeing the test output from), but the test summary at the end is coming from the go.ps1 Pester module where zero tests have run...

    That's just speculation though - ultimately, don't import Pester from within your Pester tests and things should just work...