Search code examples
powershelltestingpester

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


Solution

  • 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