Search code examples
azure-devopssonarcloud

Sonarcloud coverage includes tests


I have a python project and a pipeline where I run tests and send info to sonarcloud. The issue is that Coverage gate includes my test folders which lowers the total value. How do I fix it?

My folder structure, I want only SonarTestProject/SonarTestProject without SonarTestProject/SonarTestProject/tests to be covered

  • SonarTestProject
    • sonar-project.properties
    • test_results
    • run_tests.cmd
    • SonarTestProject
      • api
        • utils.py
        • sql.py
      • mail.py
      • tests
        • test_api
          • test_utils.py
          • test_sql.py
        • test_mail.py

How I generate my coverage report:

python -m pytest SonarTestProject\tests -m "%filter%" --junitxml=tests_results\test_results.xml --cov --cov-report=xml:tests_results\coverage.xml --cov-branch --suppress-tests-failed-exit-code || exit /B 2

My pipeline SonarCloud step:

- task: SonarCloudPrepare@1
  inputs:
    SonarCloud: 'SonarCloud - abc'
    organization: 'abc-company'
    scannerMode: CLI
    configMode: manual
    cliProjectKey: 'SonarTestProject-2023'
    cliProjectName: 'SonarTestProject'
    extraProperties: |
     sonar.python.coverage.reportPaths=$(System.DefaultWorkingDirectory)/tests_results/coverage.xml

I tried combinations of the properties below from this link https://docs.sonarcloud.io/advanced-setup/analysis-scope/. I usually get can't be indexed twice or it shows the same result or it's even worse like completely showing every gate as green.

sonar.sources = 
sonar.tests = 
sonar.test.inclusions = 
sonar.exclusions = 

Screenshots of my issue, I know for a fact that coverage should be around 65%: enter image description here enter image description here


Solution

  • Can you please test the following combination of settings and inform the result inside Sonar coverage report?

    sonar.sources = SonarTestProject/
    sonar.exclusions = SonarTestProject/SonarTestProject/tests/**/*.py
    

    Parameters edited to the ones that worked.