Setup test exclusions for correct coverage

Hello, I have a project on Azure DevOps. There, I have a pipeline to build and test my code. I added this step below + task: SonarCloudAnalyze@1.

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

It runs successfully but in coverage, my test folder shows as uncovered which bring the total coverage to 40%. I tried following this guide Analysis Scope | SonarCloud Docs but none of it works. What properties should I add to only cover the appropriate code?

My folder structure:

- MyProjectPy
    - some_folder
    - some_script.cmd
    - MyProjectPy
      - api
        - io.py
        - price.py
      - sql
        - read.py
        - write.py
      - auth.py
      - datalake.py
      - tests
        - test_api
          - test_io.py
          - test_price.py
        - test_sql
          - test_read.py
          - test_write.py
        - test_auth.py
        - test_datalake.py

You can set sonar.tests=tests, which will identify that the files are test files and automatically exclude them from code coverage.

Will that be enough? My test folder is inside the MyProjectPy/MyProjectPy folder. Shouldn’t it be
sonar.tests=MyProjectPy/test then? I tried different combinations but nothing seems to work yet(

You’re right. I missed the parent directory.

MyProjectPy/tests, since that’s the name of your folder (plural tests, not singular)

if I do it like you suggested, it doesn’t check anything. It all becomes fully green like this which is incorrect

Well, it does detect 10 code smells :smiley:

What kind of analysis is this? Is it analysis of a pull request or of your main branch? (the former will only show issues not raised in the target branch, of which these code smells might be included).

Figured it out. My problem was that I did not specify the glob pattern. I expected it to work with just, let’s say, sonar.tests=tests. After I changed it to sonar.tests=tests/**/*.py it worked as I wanted it

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