'Sonarqube zero coverage with python tests

I am trying to run sonarqube in a gitlab pipeline and pytests, and it does not return coverage. Seems it finds the coverage file, according to the logs, but shows 0% coverage.

I am quite desperate as tried multiple solutions and combinations already.

Gitlab pipeline is (where commented out things is what I ran with/without for tests)

Unit tests:
  image: python:3.9-slim
  stage: test
  before_script:
    - python3 -V
    - pip install --upgrade setuptools
    - pip install ez_setup
    # - pip install unroll

    # - pip install -r requirements.txt
    - pip install pytest pytest-cov
    - pip install pytest
    - pip install pytest-metadata

  script:
    - export PYTHONUNBUFFERED=1
    # - python3 -m pytest
    # - coverage run -m pytest
    # - coverage report
    # - coverage run -m pytest -rap  --junitxml coverage.xml
    # - coverage xml -i
    - pytest -v  --cov --cov-report=xml --cov-report=html
    # - coverage lcov
    - python3 -V
    - ls -a
  coverage: /All\sfiles.*?\s+(\d+.\d+)/
  artifacts:
    # reports:
    #   cobertura: cobertura-coverage.xml
    paths:
      # - coverage.lcov
      - coverage.xml
      - .coverage
  only:
    - merge_requests
    - master
    - development


sonarqube-check:
  stage: analysis
  image: 
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # Defines the location of the analysis task cache
    GIT_DEPTH: "0"  # Tells git to fetch all the branches of the project, required by the analysis task
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script: 
    - ls -a
    - ls -a .coverage
    - sonar-scanner -X
  allow_failure: true
  only:
    - merge_requests
    - main
    - main

sonar-project file

sonar.projectKey=XXXXX
sonar.qualitygate.wait=true
sonar.language=py
sonar.python.version=3.9
sonar.projectVersion=1.0
sonar.core.codeCoveragePlugin=cobertura
sonar.python.coverage.reportPaths=coverage.xml
sonar.python.xunit.reportPaths=coverage.xml
sonar.verbose=true
sonar.sources=src
sonar.tests=src
sonar.test.inclusions=tests/*.py, src/*.py

Folder structure is just 2 folders tests and src, with .py files in each.

Logs are

16:08:59.221 INFO: Sensor Cobertura Sensor for Python coverage [python]
16:08:59.221 DEBUG: Using pattern 'coverage.xml' to find reports
16:08:59.251 INFO: Python test coverage
16:08:59.255 INFO: Parsing report '/correctpath/coverage.xml'
16:08:59.373 DEBUG: 'src/delta.py' generated metadata as test  with charset 'UTF-8'
16:08:59.376 DEBUG: 'src/invoice.py' generated metadata as test  with charset 'UTF-8'
16:08:59.383 DEBUG: 'src/portfolio.py' generated metadata as test  with charset 'UTF-8'
16:08:59.395 DEBUG: Saving coverage measures for file 'src/p1.py'
16:08:59.420 DEBUG: Saving coverage measures for file 'src/__init__.py'
16:08:59.424 DEBUG: 'src/__init__.py' generated metadata as test  with charset 'UTF-8'
16:08:59.425 DEBUG: Saving coverage measures for file 'src/invoice.py'
16:08:59.426 DEBUG: Saving coverage measures for file 'src/delta.py'
16:08:59.428 INFO: Sensor Cobertura Sensor for Python coverage [python] (done) | time=207ms
16:08:59.429 INFO: Sensor JaCoCo XML Report Importer [jacoco]
16:08:59.435 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
16:08:59.436 INFO: No report imported, no coverage information will be imported by JaCoCo XML Report Importer

Pipelines pass, but coverages are 0%. I tried both coverage and pytest libraries, in case one of them has wrong format of coverage.xml

Thanks for any help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source