C++ test sources - scanning for quality but ignoring for coverage

I have a C++ project that is configured as follows:

sonar.exclusions=**/*.json, **/*.py, **/*.js, **/*.xml, **/*.html

src/ folder internally contains source code for several libraries and their unit tests.

I would like to enforce code quality rules on the unit test sources but to exclude them from the code coverage calculation. The reason for exclusion is that I’m not generating/collecting coverage data for the unit tests so all test sources appear with 0 coverage in SonarCloud and significantly reduce overall coverage.

From this I understand that I can not use sonar.tests to distinguish between the real project sources and the test sources.

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

How can I achieve my goal? I would like to avoid collecting coverage metrics for the test sources if possible.

I just collected and loaded coverage coverage data for test sources and realised why option is a no-go: it skews source coverage %.

For example, sources have 80% coverage, tests by their nature execute almost every line and thus have nearly 100% coverage (99.4% in my case). As a result, the overall coverage for the project becomes 90%, when the “useful” coverage of the source code is still 80%.

I hope someone else came across/thought about this issue and can share a way to exclude test sources from the code coverage calculation.


What you’re looking for is a coverage exclusion.

BTW, it’s worth noting that including your test files in your “source” scope to get normal rules run on them will artificially inflate your license lines of code.