Code coverage stays to 0%, what am I missing?

In a nutshell :

  • Error : SonarCloud Dashboard showing 0% Coverage
  • GitHub (private repo)
  • Github Actions
  • Python (for a data science project)

Hi folks,
I am a totally beginner with sonar source products, and I am in charge to configure SonarCloud automatic analysis on my team’s project (sic).

I have read all available documentations, everything is working fine, except that the code coverage on my project stays to 0. I have dug the documentation and some tutorials, but I am still stuck and cannot get what I am missing …

I run my unit tests using : pytest test --doctest-modules --junitxml=junit/test-results.xml --cov=jawclenching --cov-report=xml

Is there anything wrong in my configurations? my paths? my xml report format?

Let me know if you have any questions.

Thx,
Have a nice day.


Please find my project’s configurations

project/.github/workflows/build.yml :

name: Build
on:
  push:
    branches:
      - develop
  pull_request:
    types: [opened, synchronize, reopened]
jobs:
  build:
    name: Build, Test and Analyze
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.9'
      - name: Install dependencies
        run: |
         python -m pip install --upgrade pip
         pip install -r requirements.txt
      - name: Test with pytest
        run: |
         pip install pytest
         pip install pytest-mock 
         pip install pytest-cov
         pytest test --doctest-modules --junitxml=junit/test-results.xml --cov=jawclenching --cov-report=xml --cov-report=html
      - name: SonarCloud Scan
        uses: SonarSource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

project/sonar-project.properties :

sonar.projectKey= --
sonar.organization= --

sonar.projectName= --
sonar.projectVersion=1.0

sonar.sources=.
sonar.sources.exlcusions=**/test_*.py
sonar.tests=.
sonar.test.inclusions=**/test_*.py
sonar.python.coverage.reportPaths=junit/*.xml

sonar.sourceEncoding=UTF-8

project structure:

- project :
     - src
     - test
     - junit
     - ...

project/junit/test-results.xml:

<?xml version="1.0" encoding="utf-8"?>
<testsuites>
<testsuite name="pytest" errors="0" failures="0" skipped="0" tests="511" time="7.164" timestamp="2021-07-30T15:55:59.722732" hostname="xxx.local">
<testcase classname="test.test_base.TestFilter" name="test_generate_filter" time="0.003"/>
<testcase classname="test.test_base.TestFilter" name="test_get_logs" time="0.001"/>
....
</testsuite>
</testsuites>

Hi @Antoine_Lebaud , welcome to the community.

Maybe as a first investigation, could you try to turn on debug logging on the analysis (add sonar.verbose=true on the sonar-project.properties) and see if you can see anything related to your coverage file being picked up or not ?

Mickaël

Hi Mickaël,

Thanks for your quick answer!

Correct me if I am wrong, but this part seems related to my xml report :

....
5:44:15.761 INFO: Starting test sources highlighting
15:44:15.762 DEBUG: 'test/unit/preprocessing/test_transformers.py' generated metadata as test  with charset 'UTF-8'
15:44:15.776 INFO: 5 source files to be analyzed
15:44:15.796 DEBUG: 'test/integration/preprocessing/test_transformers.py' generated metadata as test  with charset 'UTF-8'
15:44:15.850 DEBUG: 'test/unit/preprocessing/test_base.py' generated metadata as test  with charset 'UTF-8'
15:44:15.895 DEBUG: 'test/unit/test_utils.py' generated metadata as test  with charset 'UTF-8'
15:44:15.933 DEBUG: 'test/unit/test_base.py' generated metadata as test  with charset 'UTF-8'
15:44:15.952 INFO: 5/5 source files have been analyzed
15:44:15.953 INFO: Sensor Python Sensor [python] (done) | time=4232ms
15:44:15.954 INFO: Sensor Cobertura Sensor for Python coverage [python]
15:44:15.956 DEBUG: Using pattern 'junit/*.xml' to find reports
15:44:15.979 INFO: Python test coverage
15:44:15.981 INFO: Parsing report '/github/workspace/junit/test-results.xml'
15:44:15.999 INFO: Sensor Cobertura Sensor for Python coverage [python] (done) | time=45ms
15:44:16.000 INFO: Sensor PythonXUnitSensor [python]
15:44:16.000 DEBUG: Using pattern 'xunit-reports/xunit-result-*.xml' to find reports
15:44:16.015 DEBUG: No report was found for sonar.python.xunit.reportPath using default pattern xunit-reports/xunit-result-*.xml
15:44:16.016 INFO: Sensor PythonXUnitSensor [python] (done) | time=16ms
15:44:16.017 INFO: Sensor JaCoCo XML Report Importer [jacoco]
15:44:16.019 INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
15:44:16.021 INFO: No report imported, no coverage information will be imported by JaCoCo XML Report Importer
15:44:16.021 INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=4ms
15:44:16.022 INFO: Sensor ThymeLeaf template sensor [securityjavafrontend]
15:44:16.023 INFO: Sensor ThymeLeaf template sensor [securityjavafrontend] (done) | time=1ms
15:44:16.024 INFO: Sensor Python HTML templates processing [securitypythonfrontend]
15:44:16.038 INFO: HTML files are not indexed : you may want to add them in the scanned files of this project to detect Python XSS vulnerabilities
....

I have the same issue with GitLab CI.
Here I have posted my problem: Sonarcloud shows 0% coverage on new code, and also shows 0% coverage on master branch with gitlab ci - Stack Overflow

I appreciate any help

Hello,

First, sorry for coming back to this thread so late. I figure you might have found the solution to your problem since you opened this thread. Should this not be the case, here is some additional information that may help.

We have published a new documentation on how to import coverage information for Python projects into SonarCloud, which you can find here.

Specifically in this case, you probably want to double check the paths in the coverage report. Those may not match with what SonarCloud is expecting, as the two steps are run in separate containers. When using Coverage.py, you can enable relative_files = True to avoid any mismatch. You can also use a sed command to fix this (as suggested in this StackOverflow thread).

Cheers,
Guillaume

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.