das
das

Reputation: 679

sonarqube for python project not showing any test coverage

I installed sonarqube in my MAC machine using the docker compose given below.

version: "2"

services:
sonarqube:
image: sonarqube
ports:
  - "9000:9000"
networks:
  - sonarnet
environment:
  - SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar
volumes:
  - sonarqube_conf:/opt/sonarqube/conf
  - sonarqube_data:/opt/sonarqube/data
  - sonarqube_extensions:/opt/sonarqube/extensions
  - sonarqube_bundled-plugins:/opt/sonarqube/lib/bundled-plugins

  db:
image: postgres
networks:
  - sonarnet
environment:
  - POSTGRES_USER=sonar
  - POSTGRES_PASSWORD=sonar
volumes:
  - postgresql:/var/lib/postgresql
  # This needs explicit mapping due to https://github.com/docker-library/postgres/blob/4e48e3228a30763913ece952c611e5e9b95c8759/Dockerfile.template#L52
  - postgresql_data:/var/lib/postgresql/data

networks:
sonarnet:
driver: bridge

volumes:
  sonarqube_conf:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_bundled-plugins:
  postgresql:
  postgresql_data:

After which I used the command

sonar-scanner

to analyse the project using sonarqube.

enter image description here

The analysis report is shown above. If you notice, the code coverage part is left blank, even though I have written some python unittest scripts. Please suggest a way so that I can get the code coverage report for my python project in sonarqube. Thanks in advance.

Upvotes: 4

Views: 10632

Answers (2)

yegorski
yegorski

Reputation: 395

You’ll need a code coverage tool to analyze how much of the project’s code is coverage by unit tests.

As mentioned, one of the tools is coverage.

The coverage tool can be used to generate a SonarQube-compatible XML report, which is then uploaded to SonarQube.

Once installed, run coverage xml.

In your sonar-project.properties add:

sonar.python.coverage.reportPath=coverage.xml

Remember to add the auto-generated coverage output files to .gitignore:

.coverage
coverage.xml

Upvotes: 3

agabrys
agabrys

Reputation: 9116

SonarQube doesn't calculate a code coverage. It only displays results provided by other tools.

You have to execute a tool which calculates code coverage (e.g. Coverage.py) and next add analysis parameters:

  • sonar.python.coverage.reportPath - a report path of the unit test results
  • sonar.python.coverage.itReportPath - a report path of the integration test results

You can read everything on SonarQube wiki: https://docs.sonarqube.org/display/PLUG/Python+Coverage+Results+Import

Upvotes: 3

Related Questions