Alex Little
Alex Little

Reputation: 544

SonarQube - Is there basic example/explanation of how coverage issues can be resolved?

I'm very new to using SonarQube/Cloud (so please be gentle!) and am trying to work out how to improve the '0% coverage on new code' that my code has.

Here's an example:

I added the code:

    validation_errors = []

    for field in required:
        if field not in request.POST:
            validation_errors.append("field '{0}' missing".format(field))

    if len(validation_errors) > 0:
        return JsonResponse({'errors': validation_errors}, status=400 )

and I have a (Django) test for this:

def test_required_params(self):
        # no username
        response = self.client.post(self.url, { 'password': 'secret', 'media_file': self.video_file })
        self.assertRaises(forms.ValidationError)
        self.assertEqual(response.status_code, 400)

But when I run the sonar-scanner, in the online report, I get the message that these lines are not covered (see: https://sonarcloud.io/component_measures?id=django_oppia&metric=new_coverage&selected=django_oppia%3Aapi%2Fmedia.py)

I'm sure I must have some very basic mis/non-understanding of what the coverage metric actually means.

I'd really appreciate if someone could explain to me what I need to add/update in my code (just the specific example above), so the SonarCloud analysis doesn't continue to flag this as not covered.

Thanks for your help - if you need any extra info on code/platform/versions etc, just let me know.

Edit The sonar-scanner command that I'm running:

sonar-scanner \
  -Dsonar.projectKey=django_oppia \
  -Dsonar.organization=alexlittle-github \
  -Dsonar.sources=. \
  -Dsonar.host.url=https://sonarcloud.io \
  -Dsonar.login=xxxxxxxxxx \
  -Dsonar.exclusions=docs/_build/**/*

I run this from the root of my source code, and I haven't specified/edited anything in the sonar/conf/sonar-scanner.properties file (it's just the default installed version)

Upvotes: 0

Views: 3383

Answers (1)

Alex Little
Alex Little

Reputation: 544

Thanks to the comments post above by @metalisticpain, I've now got this working. The issue was due to me missing out creating the coverage report.

For anyone else having the same issue, here's how I resolved it...

First I ran the coverage tool (from the root of my project directory):

coverage erase
coverage run --branch --source=oppia
coverage xml -i

Then pointed the sonar-scanner to this coverage.xml file:

sonar-scanner \
  -Dsonar.projectKey=django_oppia \
  -Dsonar.organization=alexlittle-github \
  -Dsonar.sources=. \
  -Dsonar.host.url=https://sonarcloud.io \
  -Dsonar.login=xxxxxxxxxx \
  -Dsonar.exclusions=docs/_build/**/*,tests/**/*,oppiamobile/settings_secret.py \
  -Dsonar.python.coverage.reportPath=./coverage.xml

I added some extra exclusions, but what actually made the difference was adding the reportPaths to the coverage.xml

Upvotes: 2

Related Questions