# Setup test exclusions for correct coverage

**URL:** <https://community.sonarsource.com/t/setup-test-exclusions-for-correct-coverage/97695>\
**Category:** SonarQube Cloud\
**Tags:** scanner, coverage\
**Created:** [August 18, 2023, 3:55pm UTC](https://community.sonarsource.com/t/setup-test-exclusions-for-correct-coverage/97695 "2023-08-18T15:55:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![artemana](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@artemana](https://community.sonarsource.com/u/artemana)\
**Post date:** [August 18, 2023, 3:55pm UTC](https://community.sonarsource.com/t/setup-test-exclusions-for-correct-coverage/97695/1 "2023-08-18T15:55:20Z")

</div>

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](https://docs.sonarcloud.io/advanced-setup/analysis-scope/) but none of it works. What properties should I add to only cover the appropriate code?

My folder structure:

```auto
- 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
```

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [August 21, 2023, 7:35am UTC](https://community.sonarsource.com/t/setup-test-exclusions-for-correct-coverage/97695/2 "2023-08-21T07:35:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![artemana](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@artemana](https://community.sonarsource.com/u/artemana)\
**Post date:** [August 21, 2023, 12:04pm UTC](https://community.sonarsource.com/t/setup-test-exclusions-for-correct-coverage/97695/3 "2023-08-21T12:04:24Z")

</div>

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(

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [August 21, 2023, 1:17pm UTC](https://community.sonarsource.com/t/setup-test-exclusions-for-correct-coverage/97695/4 "2023-08-21T13:17:08Z")

</div>

You’re right. I missed the parent directory.

> [@artemana](#):
>
> Shouldn’t it be  
> `sonar.tests=MyProjectPy/test` then?

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

---

<div class="post-metadata">

**Author:** ![artemana](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@artemana](https://community.sonarsource.com/u/artemana)\
**Post date:** [August 21, 2023, 2:09pm UTC](https://community.sonarsource.com/t/setup-test-exclusions-for-correct-coverage/97695/5 "2023-08-21T14:09:15Z")

</div>

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

 ![bugged_green](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/0/d/0d0c20d4972d06611c556bfddab2272a4f2a1146.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [August 24, 2023, 11:55am UTC](https://community.sonarsource.com/t/setup-test-exclusions-for-correct-coverage/97695/6 "2023-08-24T11:55:33Z")

</div>

Well, it does detect 10 code smells 😃

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).

---

<div class="post-metadata">

**Author:** ![artemana](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@artemana](https://community.sonarsource.com/u/artemana)\
**Post date:** [August 28, 2023, 1:31pm UTC](https://community.sonarsource.com/t/setup-test-exclusions-for-correct-coverage/97695/7 "2023-08-28T13:31:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [September 4, 2023, 1:32pm UTC](https://community.sonarsource.com/t/setup-test-exclusions-for-correct-coverage/97695/8 "2023-09-04T13:32:06Z")

</div>

Closed on September 4, 2023, 1:32pm UTC

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