简体   繁体   中英

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

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 It s? Is there a better workflow for developing/debuging new tests other than either letting all of them run or commenting them out?

It doesn't look like there's any way to specify tests to run by the name of the It block.

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.

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 code with -ExcludeTag .

Please see https://github.com/pester/Pester#tags for details.

Also check out the braking changes, if you plan to migrate from version 4 to 5!

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
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM