Search code examples
pythongithubpytesttravis-ci

Show Coverage in github PR


We run tests on each pull request in github.

It would be very cool if the coverage would be visible if you are looking at the changes of a pull request.

While looking at the diff in the PR review I would like to see which line was covered and which line not.

We use pytest and travis.

I found this: https://github.com/TheKevJames/coveralls-python But AFAIK this shows the coverage reports somewhere else, not in the "Files changed" tab of the PR.

This question is not about reaching a overall coverage of X percent. This question is about seeing which lines where covered and which lines where not covered.


Solution

  • You can achieve this using Github Actions and the Github Check runs API.

    Generic flow of the Github Action workflow will be:

    1. Run pytest on the changed files and then generate coverage report.
    2. Use coverage report to annotate the commit.

    The generated report looks something like this:

    PR commit annotated using coverage report

    I have created a repository : pytest-annotate-pr to demonstrate how this can be done. Note that, this is just a demonstration of how this can be done, and it's far from production ready. For example, this currently only works when a commit is pushed in a PR :) It does not work when branches are merged into main. The above screenshot is from one of the PR raised against main.

    If you work on this and make a production ready workflow, please consider making it into a Github Action.