Code coverage for PowerShell module development
Tags: powershell, powershellmodule, devops, github
Published: 2021-02-16 20:22:28

---
title: Code coverage for PowerShell module development
published: true
date: 2021-02-16 13:30:00 UTC
tags: PowerShell,PowerShellmodule,DevOps,GitHub
canonical_url: https://robstr.dev/code-coverage-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 [codecov.io](https://codecov.io), 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!

![https://robstr.dev/images/posts/310-codecov.png](https://robstr.dev/images/posts/310-codecov.png)

Example of the overview dashboard you get at Codecov. Source: [Cloud-iQ PowerShell module](https://codecov.io/gh/CrayonGroup/CloudiQ.PowerShell/)

## 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 [Codecov.io](https://docs.codecov.io/docs/quick-start#basic-usage). Set up the upload token as a secret in your GitHub repository.

The GitHub action can be set up like this.

```yaml
name: 'Code Tests'
on:
  push:
    branches: [master]
  pull_request:
    branches: [master]
jobs:
  codecov:
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v2
        
      - name: pester
        run: |
          Set-PSRepository psgallery -InstallationPolicy trusted
          Install-Module -Name Pester -RequiredVersion 5.0.4 -Force;
          $paths = @(
            '.\path01\*.ps1'
            '.\path02\*.ps1'
            )
          Invoke-Pester -Path "tests" -CodeCoverage $paths -CodeCoverageOutputFileFormat "JaCoCo";
        shell: pwsh
      
      - name: Codecov
        uses: codecov/codecov-action@v1.0.13
        with:
          name: BuildName
          token: ${{ secrets.CODECOV }}
          file: coverage.xml

```

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](https://github.com/CrayonGroup/CloudiQ.PowerShell/blob/master/.github/workflows/code-tests.yml). For details, see my blog post [Using GitHub actions to run automatic Pester tests](https://dev.to/roberthstrand/using-github-actions-to-run-automatic-pester-tests-cm0-temp-slug-6057590).