DEV Community

Roberth Strand
Roberth Strand

Posted on • Originally published at on

Code coverage for PowerShell module development

Code coverage can be a controversial topic, if you let it, but I feel that it is one of the many tools one can use to make sure that you’re on the right track. For those not aware, code coverage (or test coverage) is a measurement of how much of your source code you’re testing. The though being that the more of your code you are testing, the better. I agree, and I try to write tests for every single function that I write in a module.

In this particular blog, we’re going to explore how to create code coverage metrics and automatically send the results to the service, which in turn will present the results so that we can see change over time. Codecov has a generous free tier, which gives you unlimited repositories, and all the bells and whistles!

Example of the overview dashboard you get at Codecov. Source: Cloud-iQ PowerShell module

The setup, ready Codecov and the GitHub actions

This is actually a pretty easy process. What we want is to get registered on Codecov, and find the upload token for our repository. You can follow the few step instruction found in the quick start section over at Set up the upload token as a secret in your GitHub repository.

The GitHub action can be set up like this.

name: 'Code Tests'
    branches: [master]
    branches: [master]
    runs-on: ubuntu-latest
      - name: checkout
        uses: actions/checkout@v2

      - name: pester
        run: |
          Set-PSRepository psgallery -InstallationPolicy trusted
          Install-Module -Name Pester -RequiredVersion 5.0.4 -Force;
          $paths = @(
          Invoke-Pester -Path "tests" -CodeCoverage $paths -CodeCoverageOutputFileFormat "JaCoCo";
        shell: pwsh

      - name: Codecov
        uses: codecov/codecov-action@v1.0.13
          name: BuildName
          token: ${{ secrets.CODECOV }}
          file: coverage.xml

Enter fullscreen mode Exit fullscreen mode

Here we define what paths we want Pester to create a code coverage report for, and that the format should be JaCoCo which is something that Codecov understands. Next, we upload the xml file to Codecov using their own GitHub action, supplying the upload token which we set as a repository secret.

Feel free to combine the codecov job with other pester tests. For instance, take a look at how I have set up a multi-platform test in one job and the code coverage job in another here on GitHub. For details, see my blog post Using GitHub actions to run automatic Pester tests.

Top comments (0)