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, 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!

CodeCov Dashboard

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. Set up the upload token as a secret in your GitHub repository. The GitHub action can be set up like this.

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/[email protected]
        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. For details, see my blog post Using GitHub actions to run automatic Pester tests.