Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Configuration option to display tags automatically as part of the Describe, Context or It titles

Open markwragg opened this issue 2 years ago • 6 comments

Summary of the feature request

After executing a set of tests, I sometimes what to rerun a subset of the tests (i.e where they have failed). I use tags for this, but I can sometimes have a hard time remembering the Tag name I set to various blocks. As a result I've started to append "[Tag: mytag ]" into the names of my Context blocks, but it occurred to me it might be a nice feature if there was a configuration option that appended the tags into the names automatically in a similar way.

How should it work?

When using New-PesterConfiguration a new configuration setting called Output.Tags could be set to $true and would then automatically append the name of any tags into the title of the relevant block such as "Title of block [Tags: tag1, tag2, tag3.]".

markwragg avatar Nov 01 '21 13:11 markwragg

Sounds good.

nohwnd avatar Nov 01 '21 15:11 nohwnd

@markwragg, @nohwnd This sounds useful. Would we include this as part of Detailed and Diagnostic view only?

ArmaanMcleod avatar Nov 27 '21 07:11 ArmaanMcleod

Is this something we would aim for?

BeforeAll {
    # your function
    function Get-Planet ([string]$Name = '*') {
        $planets = @(
            @{ Name = 'Mercury' }
            @{ Name = 'Venus' }
            @{ Name = 'Earth' }
            @{ Name = 'Mars' }
            @{ Name = 'Jupiter' }
            @{ Name = 'Saturn' }
            @{ Name = 'Uranus' }
            @{ Name = 'Neptune' }
        ) | foreach { [PSCustomObject]$_ }

        $planets | where { $_.Name -like $Name }
    }
}

# Pester tests
Describe 'Get-Planet' -Tag 'Tag1' {
    It "Given no parameters, it lists all 8 planets" -Tag 'Tag2', 'Tag3' {
        $allPlanets = Get-Planet
        $allPlanets.Count | Should -Be 8
    }

    Context "Filtering by Name" -Tag 'Tag4' {
        It "Given valid -Name '<Filter>', it returns '<Expected>'" -TestCases @(
            @{ Filter = 'Earth'; Expected = 'Earth' }
            @{ Filter = 'ne*'  ; Expected = 'Neptune' }
            @{ Filter = 'ur*'  ; Expected = 'Uranus' }
            @{ Filter = 'm*'   ; Expected = 'Mercury', 'Mars' }
        ) -Tag 'Tag5' {
            param ($Filter, $Expected)

            $planets = Get-Planet -Name $Filter
            $planets.Name | Should -Be $Expected
        }

        It "Given invalid parameter -Name 'Alpha Centauri', it returns `$null" -Tag 'Tag6' {
            $planets = Get-Planet -Name 'Alpha Centauri'
            $planets | Should -Be $null
        }
    }
}

Output:

Describing Get-Planet [Tags: Tag1]
  [+] Given no parameters, it lists all 8 planets [Tags: Tag2, Tag3] 51ms (47ms|4ms)
 Context Filtering by Name [Tags: Tag4]
   [+] Given valid -Name 'Earth', it returns 'Earth' [Tags: Tag5] 23ms (17ms|6ms)
   [+] Given valid -Name 'ne*', it returns 'Neptune' [Tags: Tag5] 5ms (4ms|1ms)
   [+] Given valid -Name 'ur*', it returns 'Uranus' [Tags: Tag5] 3ms (2ms|1ms)
   [+] Given valid -Name 'm*', it returns 'Mercury Mars' [Tags: Tag5] 6ms (5ms|1ms)
   [+] Given invalid parameter -Name 'Alpha Centauri', it returns $null [Tags: Tag6] 18ms (17ms|1ms)

Or would we put the Tags in a different position like after the duration bit of the title?

ArmaanMcleod avatar Nov 27 '21 07:11 ArmaanMcleod

Missed this, but yeah that output looks okay, I would like to see it in the real output though.

nohwnd avatar Apr 22 '22 20:04 nohwnd

I would like to see it in the real output though.

Real output?

fflaten avatar Aug 09 '22 20:08 fflaten

I think I meant real world output of a suite that has more than 1 short tag per test. Especially when you run with a tag filter, then printing all tags seems unnecessary / confusing.

nohwnd avatar Aug 11 '22 07:08 nohwnd