Analyze only test files specified in `sonar.test.inclusions`

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
  1. SonarQube Enterprise Edition Version 9.5 (build 56709)
  2. SonarScanner
  • what are you trying to achieve

    Perform static code analysis only for a few test files (specified in sonar.test.inclusions).

  • what have you tried so far to achieve this

    Having a that looks like:


    And running sonar-scanner like:
    sonar-scanner -define --define sonar.c.file.suffixes=.c --define sonar.cpp.file.suffixes=.h,.cpp --define sonar.inclusions=ignored-file.txt --define sonar.test.inclusions=test/foo.cpp

    I get the execution failure java.lang.IllegalStateException: The Compilation Database JSON file was found but 0 C/C++/Objective-C files were analyzed.. I explicitly specified sonar.inclusions=ignored-file.txt because I’m not interested in a full codebase analysis, but only in the specified test file. How could I achieve that?


Hey there.

Test files are typically excluded from analysis, so what you actually want to do is force it to be recognized as a source file (include it as sonar.sources)

Thanks @Colin.

I tried to include them in the analysis (in sonar.sources) and also to specify that they are test files (in sonar.tests). sonar-scanner failed with the message: ERROR: File Test/CMakeLists.txt can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files.

The message is quite explicit.
After reading the Narrowing the focus page, I don’t quite understand the point

Different analysis rules are applied to the two categories.

What are the analysis rules that apply to Tests? How could I explicitly analyse them for errors using the CFamily plugin, but still treat them as Test code (in order to exclude them from Code Coverage metrics)?

Hey there.

I’ve gone back and reviewed the behavior of C/C++ analysis and had overlooked something specific about how we handle analysis.

Specifically, from the docs:

Unsupported Properties


sonar.tests property is used to identify the directories containing test source files. Identifying test files helps the analyzers to tune their rules. For example, the analyzers can enable test-specific rules and disable rules that don’t make sense in the context of testing.

The C/C++/Objective-C analyzer currently analyze main and test source files the same way. Consequently, sonar.tests is not yet supported; the analyzer ignores it.

If you wish to analyze test source files, you should include them in the sonar.sources property.

So if you’re still facing an issue when only specifying sonar.sources, it would be a good idea to share your build wrapper output and analysis logs. If it only concerns a single file, you might be able to do it publicly.

Thanks @Colin,

It works fine if I include the test files in sonar.sources. What I still can’t figure out how to do: exclude the Tests code from Code Coverage metrics. Is that possible?