Duplicated lines of code in tests

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."
        }
        assert response.json() == {"schedules": [{"DRL": 5000}]}
        assert response.json() == {
            "non_field_errors": ["end_dt must be after start_dt"]
        }

This ticket might explain the cause of the problem Duplicated Lines of Code - #6 by ganncamp

Ideally I would like to keep the duplicated lines check enabled, but perhaps exclude test files from this specific check. Is that possible?

Many thanks in advance.

Hi,

Welcome to the community!

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?

 
Ann

Hi, thanks for your reply.

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.

Key: sonar.test.exclusions

Default: (SonarQube Cloud’s default)

Hi,

Can you share your project configuration, as well as, perhaps, the general outline of your project structure?

 
Ann

Hi,

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

my_project/
│
├── manage.py
├── README.md
├── requirements.txt
├── .gitignore
├── .env
├── my_project/         # Main project folder
│   ├── __init__.py
│   ├── settings.py     # Or a settings/ folder with separate files (base.py, dev.py, prod.py)
│   ├── urls.py
│   ├── wsgi.py
│   ├── asgi.py
│   └── static/         # Global static files (optional, can be managed by each app)
│
├── app1/               # First Django app
│   ├── migrations/
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── tests/          # App-specific tests
│   │   └── test_*.py
│   │
│   ├── views.py
│   ├── urls.py         # App-specific URL configuration
│   ├── static/         # App-specific static files
│   └── templates/      # App-specific templates
│
├── app2/               # Another Django app
│   └── ...
│
├── templates/          # Global templates
│   ├── base.html
│   └── ...
│
├── tests/              # Global tests
│   └── test_*.py
│
└── static/             # Global static files (e.g., CSS, JavaScript, images)
    ├── css/
    ├── js/
    └── images/

Let me know if you need more details. Many thanks.

Hi,

I was asking for your project configuration. Typically that’s either part of the pipeline or contained in a sonar-project.properties file.

 
Ann

Hi,

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

    - step: &run-sonarcloud
        name: SonarCloud Code Quality Check
        script:
          - pipe: sonarsource/sonarcloud-scan:4.0.0
          - pipe: sonarsource/sonarcloud-quality-gate:0.1.6

The token is set with the SONAR_TOKEN environment variable.
We followed this guide: SonarQube Scan Bitbucket Pipe

When I ran it manually I ran the following command (again, no configuration given)

~/Downloads/sonar-scanner-7.0.2.4839-linux-x64/bin/sonar-scanner -Dsonar.organization=<name> -Dsonar.projectKey=<project> -Dsonar.sources=. -Dsonar.host.url=https://sonarcloud.io --help

Hi,

Here we go:

You’ve said that every file in the project is a source file.

To correct this, you’ll need to provide a narrower definition of sonar.sources.

 
HTH,
Ann

Many thanks you for your help.

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.

1 Like