Kaizen Today I Learned by Ville Säävuori

Code Coverage Using GitHub Actions

I like using GitLab because it has so much of the everyday developer tooling integrated right into the product. Nowadays GitHub Actions are powerful enough to do many of the same things, including code coverage.

Most projects on GitHub use external services like Codecov for code coverage reporting. I don’t have the budget for these external services and I’m not interested in setting up and maintaining different solutions based on the publicity or the organization of the project so I decided to write a native solution using Github Actions instead.

Code Coverage in Github Actions

Setting Up Github Actions

There are a few different solutions for doing this. I found irongut/CodeCoverageSummary action to be most suited for my needs. It consumes standard Cobertura coverage reports and outputs a summary either as text or markdown.

To make the summary visible on a pull request I used marocchino/sticky-pull-request-comment action.

The whole configuration looks like this:

coverage:
runs-on: ubuntu-latest
steps:
    - uses: actions/checkout@v3
    - name: Setup Python
    uses: actions/setup-python@v3

    - name: Install dependencies
    run: pip install -r requirements.txt

    - name: Coverage
    run: pytest --cov=src/playlistparser --cov-report xml

    - name: Coverage Summary Report
    uses: irongut/CodeCoverageSummary@v1.2.0
    with:
        filename: coverage.xml
        badge: true
        format: 'markdown'
        output: 'both'

    - name: Add Coverage PR Comment
    uses: marocchino/sticky-pull-request-comment@v2.2.0
    if: github.event_name == 'pull_request'
    with:
        recreate: true
        path: code-coverage-results.md

Above configuration is for a dedicated job which is useful is the tests run as a matrix. If you only run one set of tests it’s easier and faster to just pop the coverage step after the tests.

Here’s an example of this in action.

Tagged with , , ,

Published . Last modified .