Exclude tests from static code analysis

Hi,

I want to exclude the folder with the tests from the static code analysis. I tried to define the following in the pom.xml. I also tested with the path src/test/**. However, the tests folder is still analyzed.

I use SonarQube Cloud.

<properties>
     <sonar.coverage.exclusions>**src/test/**</sonar.coverage.exclusions>
</properties>
1 Like

Hi,

Welcome to the community!

** means 0-n directories. So **src doesn’t make sense to the analyzer as a path. On the other hand, **/src would.

That said, test files are automatically excluded from most calculations including both coverage and license-LoC. So a better approach would be to get those files properly identified by analysis as test files. Since the tests appear to live side-by-side with your source files, that’s a multi-step operation (or rather, a multi-parameter operation). It would look something like this:

sonar.sources=src
sonar.tests=src/test
sonar.exclusions=src/test

That last line keeps the test files from being double-counted as both tests and source files. And of course, you’ll want to adjust those paths to match your project.

 
HTH,
Ann

1 Like

This is now my config. I removed the config from the pom.xml.

// Spring Boot Project
<ProjectDir>/src/main
<ProjectDir>/src/test

<ProjectDir>/sonar-project.properties

# https://docs.sonarcloud.io/advanced-setup/analysis-scope/
sonar.sources=src
sonar.tests=src/test
sonar.exclusions=src/test
1 Like

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