Trying to achieve:
Trying to scan source and test files as part of the process to add code coverage.
If I navigate to the project report I see that the test files are not scanned by SonarQube.
My project follows the exact structure as in the simple example on the page Narrowing the Focus
(root)
- src
- test
The following is set in our sonar-project.properties file
sonar.sources=src/
sonar.tests=test/
When I run the analysis cli I do not see that the files in the test folder gets analyzed.
When I build using the build-wrapper, I do see these test files getting compiled and the test executables are created.
What I have tried
If I change the properties as follow:
sonar.sources=./
I see the test files getting scanned.
If I change it to:
sonar.sources=./
sonar.tests=test/
I get the following error when trying to run the cli:
Error during SonarScanner execution
ERROR: File test/CMakeLists.txt can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files
Could you clarify what you’re trying to accomplish? Is it that you want issues raised on your test code? Because the fact that you’re not seeing many (any?) issues raised on your test code does not mean that it’s not being included in the analysis. We just don’t have that many rules that run on test code.
Regarding your unit test count, are you passing it in as part of your test execution data?
Thus my conclusion is that with the original setup the tests are not getting scanned:
sonar.sources = src/
sonar.tests = test/
Unit test count:
Regarding your unit test count, are you passing it in as part of your test execution data?
Thanks for the information. Since it is unrelated to the issue (test code not getting scanned) we can ignore this aspect in this thread. When I get to work on this and there are issues I will log a new issue.
I guess you’re basing this on the fact that no issues or Lines of Code are detected for your test files with the original setup? The fact is that we just don’t have many (any?) rules for tests in CFamily languages. It’s perfectly normal that we woudln’t detect “lines of code” in test files because… they’re lines of tests, not production code. Here’s a screenshot from a part of SonarQube itself:
You’ll see that there are no Lines of Code for these tests either. It’s in Java, so there are a few specialized rules that run on tests, hence the issues.
If you want normal rules run on your tests, then you’ll need to include them in the sonar.sources configuration. Note that this will affect your license LOC usage.