Chun-Yen Wang
Chun-Yen Wang

Reputation: 578

List existing test cases in Pester script

Folks,

I'd like to use Pester to automate the cmdlet testing. Is there a way to list available test cases (such as describe blocks) without actual executing? Is there a command like this? Thanks.

Upvotes: 0

Views: 355

Answers (1)

mclayton
mclayton

Reputation: 10075

In general, no.

The Describe, Context and It keywords might look like a static DSL, but they're actually just PowerShell cmdlets that take a string and a script block as a parameter - the tests themselves are only "materialized" (for want of a better word) when the script runs.

As an extreme example, consider this test script. How many tests are there in it?

Describe 'Weird Test Cases' {

    Context 'Context' {

        for( $i = 0; $i -lt (Get-Random -Maximum 25); $i++ )
        {
            It "Test #$($i)" {
                $true | Should -Be $true;
            }
        }

        if( $false )
        {
            It "Should never execute" {
                $true | Should -Be $true;
            }
        }

    }

}

In a more simple example you could potentially use the built-in PowerShell Parser Class to parse your script and look for invocations of cmdlets called It, but that would be very brittle and likely to give wrong results in a lot of cases. It would also be a lot of work to consider test filters like the Tag and ExcludeTag parameters on Invoke-Pester.

However, if you want to know how many tests were run afterwards you can use the PassThru switch wth Invoke-Pester which will return an xml report on the tests that ran and their results, and you could process that to find out how many were executed.

Upvotes: 1

Related Questions