Invoke-Pester to only run a single Assert/It block
Asked Answered
E

3

5

I am writing unit tests for my Powershell Modules, with a file for each module, and Describe blocks for each function. Context blocks organize the tests along the behavior I am trying to test for with some arrange code, and my It blocks contain minimal arrange/act code and a assert.

I can limit my tests to only run a single test file by using Invoke-Pester "Path/To/Module" I can also limit based on the Describe blocks by using Invoke-Pester "Path/To/Module" -TestName @("RunThisDescribe","AndRunThisDescribe")

As I am adding a new assertion (via a new It block) to an existing file/Describe/Context, I want to test/debug my new assertion alone, without the rest of the assertions of a given describe/context running (but with any mocks/variables that I set at the describe/context scope still available.

I have been commenting out my existing assertions while I am developing the new one, then remove the block comments and run them all after I am finished with the new test. This works, but is clunky.

Is there a way to run Invoke-Pester to only execute a given list of Its? Is there a better workflow for developing/debuging new tests other than either letting all of them run or commenting them out?

Educate answered 9/7, 2019 at 20:59 Comment(0)
D
7

I know, this question is pretty old, but it deserves an update:

Starting with Pester version 5, you can have a Tag on everything: Describe, Context, It

This makes it really easy to run specific assertions and nothing else. You can even exclude specific tags.

Please see https://pester.dev/docs/usage/tags for details.

Also check out the braking changes, if you plan to migrate from version 4 to 5! There are some deprecated parameters for Invoke-Pester such as -TagFilter and -ExcludeTagFilter. In the latest version of pester, you can specify tags through the New-PesterConfiguration

Drayage answered 17/8, 2020 at 12:39 Comment(0)
P
2

If you're using Pester v3/v4 there isn't any way to specify tests to run by the name of the It block (For Pester v5 or newer, see the accepted answer from Tofuburger).

You could split your new test in to a new Describe block and then run it via the -TestName parameter as you described, or give it a -Tag and then specify that tag via Invoke-Pester, however that doesn't seem to work for a nested Describe, it has to be at the top level.

I assume this wouldn't work for you because your Mocks and Variables would be in the other describe.

VSCode with the PowerShell extension installed allows you to run individual Describe blocks via a "Run Tests" link at the top of the Describe and this does work for nested blocks. However i'm not sure if this would result in the Mocks/Variables from the parent Describe block being invoked (my guess would be not).

Example of nested Describe, which can be individually run within VSCode:

Describe 'My-Tests' {

    It 'Does something' {
        $true | Should -Be $true
    }

    Describe 'NewTest'  {
        
        It 'Does something new' {
            $true | Should -Be $true
        }
    }
}

It's a shame you can't currently put Tags on Context blocks for filtering in/out certain sets of tests. That was requested as a feature 2 years ago but it seems is not simple to implement.

Projection answered 9/7, 2019 at 21:31 Comment(1)
The GitHub issue you link to suggests that tags on Context blocks will be supported in the upcoming version 5 (which is in the alpha stage as of this writing).Developer
P
0

To add to Tofuburger's answer, and based on Pester 5.3.1, you can also manipulate tests programmatically, in your test scripts, based on tags.

Describe 'Colour' -Tag 'Epistemology' {

    BeforeAll {
        $ParentBlockTags = $____Pester.CurrentBlock.Tag
        if ($ParentBlockTags -eq 'Epistemology')
        {
            Set-ItResult -Inconclusive
        }
    }

    BeforeEach {
        $ItTags = $____Pester.CurrentTest.Tag
        if ($ItTags -eq 'HSL')
        {
            Set-ItResult -Skipped -Because 'Not implemented'
        }
    }

    It 'Saturates' -Tag 'HSL' {
        1 | Should -Be 2
    }

    It 'Greens' -Tag 'RGB' {
        1 | Should -Be 3
    }

}
Peden answered 24/5, 2022 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.