Search code examples
sonarqubepytestgithub-actions

Github Actions, Python Coverage and Sonar Qube


I want to create a Github workflow that does the following:

  1. test my code with pytest
  2. trigger Sonar Qube Cloud to analyze to the code and show my test coverage!

As far as I understand, SonarQ needs a file coverage.xml to display the code coverage. This can be generated with

pytest --cov=./ --cov-report=xml --doctest-modules

According to this article coverage.xml should be available under /github/workspace/coverage.xml.

Thus, I specify my sonar-project.properties in the root folder of the project:

sonar.organization=pokemate
sonar.projectKey=PokeMate_name-generator
sonar.sources=.
sonar.python.coverage.reportPath=/github/workspace/coverage.xml

my actions file build.yml:

on:
  push:
    branches:
      - master
      - develop
      - sonar-qube-setup

jobs:
  build:
    runs-on:
      - ubuntu-latest

    steps:
      # Checkout repo
      - uses: actions/checkout@v2

      # Dependencies
      - name: Set up Python 3.7
        uses: actions/setup-python@v1
        with:
          python-version: 3.7

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      # Test
      - name: Test with pytest
        run: |
          pytest --cov=./ --cov-report=xml --doctest-modules

      # Sonar Qube
      - name: SonarCloud Scan
        uses: sonarsource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

However, on SonarQ it still shows 0% test coverage, which is probably because it cannot find the coverage.xml. Any idea how to make this work?


Solution

  • The error came from the missing s in reportPaths in the sonar-project.properties file.