Show coverage in github

148 views Asked by At

I have a python project in github, and I want that with every push, github shows the coverage of the project. Is that possible? Which tool do I need? When I used to work with java, jacoco solves this, but in python I don't know what tool I can use. My .github/workflows/github-actions.yaml is that:

name: Python CI
on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.11'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Test with pytest
      run: |
        pip install pytest pytest-cov
        pytest tests/ 

Any suggestions?

1

There are 1 answers

0
Ilya Khrustalev On

It is definetaly possible. One of the possible solution is to use codecov.io service that provides functionality you need.

In order to to that you need to make the following steps:

Sign up and get Codecov token

First, you need to create an account in codecov.io service and then get the API token by going through Profile picture -> Settings -> Settings tag -> Access pane -> "Generate token" button.

Give a name for token and click another "Generate token" button.

Finally, copy your token.

Add a codecov token in Github secrets

Once, you have your token, you need to securely provide it to CI/CD pipelines. In order to do this, you need to access setings page of your repository navigate to "Secrets and variables" -> "Actions" and select "New repository secret". You can consider adding organisation-wide secret if you're going to use one token for multiple repositories in your organization.

Set the name, I personally prefer "CODECOV_TOKEN", and paste token you've got from previous step.

Update your Github action

Finally, you need to amend your github action in order to start upload coverage reports to the codecov.io.

Update "Test with pytest" step

    - name: Test with pytest
      run: |
        pip install pytest pytest-cov
        pytest --cov=./ --cov-report=xml tests/ 

NOTE: --cov=./ --cov-report=xml parameters added to the pytest command in order to instruct pytest-cov library to collect coverage and provide it in xml format.

Add "Collect coverage" step

   - name: Collect coverage
      uses: codecov/codecov-action@v3
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        fail_ci_if_error: true
        files: ./coverage.xml
        verbose: true

This is official Github action of Codecov service. It should be a good baseline that allows you to check how it works.

NOTE: token: ${{ secrets.CODECOV_TOKEN }} where you need to provide a token, so it may differs if you will give it another name.

I hope this helps you get everything set up. I encourage you to read more about configuration options codecov provides in order to tune the integration.