pester / Pester

Pester is the ubiquitous test and mock framework for PowerShell.
https://pester.dev/
Other
3.11k stars 473 forks source link

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

Open markwragg opened 3 years ago

markwragg commented 3 years ago

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.]".

nohwnd commented 3 years ago

Sounds good.

ArmaanMcleod commented 2 years ago

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

ArmaanMcleod commented 2 years ago

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?

nohwnd commented 2 years ago

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

fflaten commented 2 years ago

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

Real output?

nohwnd commented 2 years ago

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.