Search code examples
code-coveragechocolateypester

How do I get code coverage for all branches using Pester?


Multiple test cases have been written to test a new Chocolatey function using Pester. How do check whether all branches have been covered?


Solution

  • UPDATE 2:

    Thanks to oɔɯǝɹ for pointing out that Pester has now released a version of Pester that supporting code coverage.

    UPDATE 1:

    As of Pester Version 3.0, it is now possible to get code coverage information, using:

    Invoke-Pester -CodeCoverage <path to file>
    

    This is documented in the project wiki page:

    https://github.com/pester/Pester/wiki/Code-Coverage

    NOTE: In order to use this, you will require PowerShell version 3.0

    ORIGINAL ANSWER:

    To the best of my knowledge, Pester doesn't currently support code coverage analysis, but it is something that is being worked on.

    There is an open issue for this feature here:

    https://github.com/pester/Pester/issues/53

    You can see it being worked on here:

    http://davewyatt.wordpress.com/2014/06/29/code-coverage-analysis-for-pester-feedback-request/

    And there is a screenshot of it working here:

    https://twitter.com/nohwnd/status/485093995929157632

    So basically, hold tight, and there will hopefully be something soon.

    In terms of the actual Chocolatey code base, there is quite a good convention being used, namely that for each *.ps1 file, there "should" be a corresponding ".Tests.ps1 file. If this second file doesn't exist, then there are no unit tests for that function.