We are using the default “Sonar Way” quality gate however are coming across problems with the duplicated lines metric. In our main code base this appears to work fine but it is very problematic in our tests resulting in 10+% duplication and therefore our builds are failing the quality gate.
As an example, we have some tests that assert the contents of JSON blobs which all get marked as duplicate lines even when the content is slightly different.
assert response.json() == {
"detail": "Authentication credentials were not provided."
}
This leads me to believe that your tests are being analyzed as code rather than as tests. Are regular rules being applied to your tests? Have you configured that on purpose?
I don’t believe we have changed many of the settings from the default, and we definitely have not deliberately tried to change the behaviour with relation to the tests specifically.
I have seen a case of regular rules being ran on tests yes (one example was that a security issue was detected in the test as it was connecting using HTTP).
This specific file is located in the directory: <appname>/web/tests/test_<modulename>.py
Would that file path mean SonarQube Cloud detects it as a “test”?
I found this setting in the project administration “General Settings” which has not been touched so is left at the default - do I need to change this perhaps?
Test File Exclusions
Patterns used to exclude some test files from analysis.
I have ran the sonar-scanner manually (normally it runs as part of the CI/CD) and as part of the output log it has given me this which I hope is what you need?
13:29:22.249 INFO Project configuration:
13:29:22.250 INFO Excluded sources: **/build-wrapper-dump.json, **/migrations/**
Further down in the logs it says this
13:29:46.101 INFO The property "sonar.tests" is not set. To improve the analysis accuracy, we categorize a file as a test file if any of the following is true:
* The filename starts with "test"
* The filename contains "test." or "tests."
* Any directory in the file path is named: "doc", "docs", "test" or "tests"
* Any directory in the file path has a name ending in "test" or "tests"
The application is created using the Django framework so it follows the standard project layout which looks like this
I haven’t set any file based project configuration - the only place we have modified anything is in the web based settings page here https://sonarcloud.io/project/settings?id=<project name>
This is what we use to trigger the checks in our Bitbucket pipeline
I have solved my problem by setting sonar.test.inclusions to **/tests/** in the Web UI. This works better for me as it would have resulted in a lot effort to identify all the different directories to include in the sonar.sources configuration option.