0

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

Vitamin C
  • 135
  • 11

1 Answers1

1

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.

  • Thanks! At first, it returned an error "The folder 'SonarTestProject/SonarTestProject' does not exist for 'SonarTestProject-2023' (base directory = E:\repos\SonarTestProject)". I removed the first folder to make it look like this - "sonar.sources = SonarTestProject/" and it works. I am gonna put it as solved but I now see that code smells, security etc do not check the tests. Is there a way to not include them in coverage gate but into others? – Vitamin C Aug 22 '23 at 11:38
  • @VitaminC never tried to cover the tests itself, but looking for this question I found this one here and maybe it's the same thing that you are trying to do: https://stackoverflow.com/questions/50516502/how-do-i-get-sonarqube-to-analyse-code-smells-in-unit-tests-but-not-count-them-f – Herivelton Andreassa Aug 22 '23 at 13:00
  • Hmmm, "sonar.coverage.exclusions = SonarTestProject/tests/**/*.py" seems to work exactly as I want. For reference, I thought that I don't need a specific glob pattern ie "**/*.py", I thought that just the name of the folder would exclude everything within which was not the case. – Vitamin C Aug 22 '23 at 13:15