Skip to content

Code Coverage page is outdated #196

Closed

Description

@fflaten

Checklist

  • Issue has a meaningful title
  • I have searched the existing issues. See issues

Link to page

https://pester.dev/docs/usage/code-coverage

What is the issue?

Docs for using code coverage is not updated for Pester 5 yet.

Examples:

  • Mentions unique PS3+ requirement
  • References Pester 3.4.0 as not supported for a feature
  • Uses hashtable-config with -CodeCoverage

Suggested fix?

Should be updated to cover Pester 5.3 status with new configuration, breakpoint vs profiler etc.

Activity

  1. Veretax commented on Jul 1, 2022

    @Veretax

    Is this related to why when I run a basic code coverage of one test to one file I no longer get the missed methods listing that this page shows?

  2. fflaten commented on Jul 2, 2022

    @fflaten
    ContributorAuthor

    Code Coverage will report functions, but the samples and parameters to configure CodeCoverage on that page isn't valid for Pester 5. Try:

    CoverageTest.ps1:

    function FunctionOne ([switch] $SwitchParam)
    {
        if ($SwitchParam)
        {
            return 'SwitchParam was set'
        }
        else
        {
            return 'SwitchParam was not set'
        }
    }
    
    function FunctionTwo
    {
        return 'I get executed'
        return 'I do not'
    }

    CoverageTest.Tests.ps1:

    BeforeAll {
        . $PSCommandPath.Replace('.Tests.ps1','.ps1')
    }
    
    Describe 'Demonstrating Code Coverage' {
        It 'Calls FunctionOne with no switch parameter set' {
            FunctionOne | Should -Be 'SwitchParam was not set'
        }
    
        It 'Calls FunctionTwo' {
            FunctionTwo | Should -Be 'I get executed'
        }
    }

    Then invoke like this:

    $c = New-PesterConfiguration
    # Just hardcoding a single test path to keep it simple. Can use wildcards etc as normal
    $c.Run.Path = "C:\Path\To\Tests\CoverageTest.Tests.ps1"
    $c.CodeCoverage.Enabled = $true
    # Just hardcoding a single test path to keep it simple. Can use wildcards etc as normal
    $c.CodeCoverage.Path = "C:\Path\To\Source\CoverageTest.ps1"
    # Not required, but will print the report automatically
    $c.Output.Verbosity = 'Detailed'
    # Not required, but will let you access the report in the result-object, see below
    $c.Run.PassThru = $true
    
    $res = Invoke-Pester -Configuration $c
    
    # Output:
    
    Pester v5.3.3
    
    Starting discovery in 1 files.
    Discovery found 2 tests in 4ms.
    Starting code coverage.
    Code Coverage preparation finished after 4 ms.
    Running tests.
    
    Running tests from 'C:\Path\To\Tests\CoverageTest.Tests.ps1'
    Describing Demonstrating Code Coverage
      [+] Calls FunctionOne with no switch parameter set 12ms (1ms|10ms)
      [+] Calls FunctionTwo 2ms (1ms|1ms)
    Tests completed in 58ms
    Tests Passed: 2, Failed: 0, Skipped: 0 NotRun: 0
    Processing code coverage result.
    Code Coverage result processed in 8 ms.
    Covered 60% / 75%. 5 analyzed Commands in 1 File.
    Missed commands:
    
    File             Class Function    Line Command
    ----             ----- --------    ---- -------
    CoverageTest.ps1       FunctionOne    5 return 'SwitchParam was set'
    CoverageTest.ps1       FunctionTwo   16 return 'I do not'
    
    
    # Access the report as a string if you want
    # $res.CodeCoverage.CoverageReport
  3. bravo-kernel commented on Jul 2, 2022

    @bravo-kernel
    Collaborator

    That's awesome man 👍

  4. Veretax commented on Jul 2, 2022

    @Veretax
  5. Veretax commented on Jul 5, 2022

    @Veretax

    Okay, finally figured out what was happening on my end.

    When I had the paths setup, I think it was looking in the wrong folders all along. I had the relative paths there, but it thought they were in C:\ by referencing the source and test folders specifically down to c drive I started getting the feedback this way.

    Thank you.

  6. mahomedalid commented on Oct 23, 2023

    @mahomedalid
    Contributor

    I had a very frustrating day trying to setup code coverage, verbosity and other stuff. I volunteer to update the code coverage page, the options mentioned there are already deprecated. You could assign this to me or not, but I will create a PR soon.

  7. fflaten commented on Oct 24, 2023

    @fflaten
    ContributorAuthor

    @mahomedalid That would be great 👏

  8. mahomedalid commented on Oct 26, 2023

    @mahomedalid
    Contributor

    @fflaten ignore the "Deprecated" section for now, I am thinking in something like this, early feedback? how much do we want to keep from the deprecated section? how much from the configuration do we want to mention, considering that if we duplicate the same information than in the config reference, we have now two places to maintain?

    https://github.com/pester/docs/blob/d348c5ed308355dc4908fe8bab8f0b2bac428b67/docs/usage/code-coverage.mdx

  9. fflaten commented on Oct 26, 2023

    @fflaten
    ContributorAuthor

    Great start! I'm not able to view it rendered on mobile, so just some quick notes:

    • Pester 5 is PSv3 minimum, not just CC
    • Duplicate introduction? First lines + Understanding... section
    • TestResult is not necessary for CC, so I would skip it in example
    • Should the steps in config example be a single code block with comments to explain why? What looks best rendered? Single line codeblocks might waste vertical space on margins.
    • We support CC without breakpoints also, so maybe remove breakpoint-reference? We'll probably have to add a section about when to use which mode later. @nohwnd knows this better than me.

    how much do we want to keep from the deprecated section?

    I personally like the structure it has, ex:

    • Why use code coverage?
    • Simple console sample (with output)
    • How to integrate in CI examples

    The last section about analyzed commands might be nice as a <details> element or a last section "How to read the results", but we could add it later.

    how much from the configuration do we want to mention, considering that if we duplicate the same information than in the config reference, we have now two places to maintain?

    The parts used in the examples at least. The hidden docs in New-PesterConfiguration is just the code help which is really limited in length and formatting (bare minimum). This Code Coverage page is the "real docs" and marketing for the feature.

  10. mahomedalid commented on Nov 2, 2023

    @mahomedalid
    Contributor

    @fflaten thanks for the comments, I will submit a PR now that I have more clear structure and we can continue discussing there.

  11. mahomedalid commented on Nov 2, 2023

    @mahomedalid
    Contributor

    @fflaten #286 was created

  12. fflaten commented on Nov 6, 2023

    @fflaten
    ContributorAuthor

    Closing as fixed by #286. 🥳
    Will create new issues to track any further improvements/ideas. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions