Giordano
Giordano

Reputation: 5570

SonarQube does not correctly import coverage.xml file for Python project

I'm working on a Python project on Linux.

I'm trying to link the coverage.xml report to SonarQube analysis.

The project structure is the following:

- root
    - folder1
      ...
    - folder2
      ...
    - ...
    - coverage.xml

I run the SonarScanner analysis with the following command:

sonar-scanner \
 -Dsonar.projectKey=test \
 -Dsonar.sources=. \
 -Dsonar.host.url=http://localhost:9000 \
 -Dsonar.login=xxxxxxxx \
 -Dsonar.python.coverage.reportPaths=coverage.xml

Everything works fine except the coverage that it's always not considered.

Here the console output:

INFO: Python test coverage
INFO: Parsing report '/my/path/coverage.xml'
INFO: Sensor Cobertura Sensor for Python coverage [python] (done) | time=73ms
INFO: Sensor PythonXUnitSensor [python]
INFO: Sensor PythonXUnitSensor [python] (done) | time=13ms
INFO: Sensor SonarCSS Rules [cssfamily]
INFO: No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
INFO: Sensor SonarCSS Rules [cssfamily] (done) | time=2ms
INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: 
target/site/jacoco/jacoco.xml,target/site/jacoco- 
it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
INFO: No report imported, no coverage information will be imported by JaCoCo XML Report Importer
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=9ms
INFO: Sensor JavaXmlSensor [java]
INFO: 1 source files to be analyzed
INFO: Sensor JavaXmlSensor [java] (done) | time=306ms
INFO: 1/1 source files have been analyzed
INFO: Sensor HTML [web]
INFO: Sensor HTML [web] (done) | time=5ms
INFO: Sensor XML Sensor [xml]
INFO: 1 source files to be analyzed
INFO: Sensor XML Sensor [xml] (done) | time=257ms
INFO: 1/1 source files have been analyzed
INFO: ------------- Run sensors on project
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=12ms
INFO: CPD Executor 21 files had no CPD blocks
INFO: CPD Executor Calculating CPD for 21 files
INFO: CPD Executor CPD calculation finished (done) | time=31ms
INFO: Analysis report generated in 86ms, dir size=397 KB
INFO: Analysis report compressed in 140ms, zip size=133 KB
INFO: Analysis report uploaded in 18ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard?id=test

What am I doing wrong?

Upvotes: 3

Views: 5163

Answers (1)

Aniket Kulkarni
Aniket Kulkarni

Reputation: 13

I faced the same issue. The problem is with coverage.xml file. The xml file generated by pytest was incorrect with respect to source path. Because of which the SonarQube was not able to reach the source files and bring the coverage data. To solve the problem you can once check the source path in xml file and manually update it. There is another solution to use coverage library which will fix the xml for us.

But It did not worked for me. Still I will add it here.

Install coverage

pip3 install coverage

Run

python3 -m coverage xml -i

in the directory where xml is kept. It should Fix the source path. But In my case It was deleting the xml file itself. So, I updated source manually and SonarQube was able to show the coverage. But It is not possible to change source manually.

To automate it We can use

python3 -m coverage xml -i

Hopefully It will help.

Upvotes: 1

Related Questions