Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test/Test Coverage with Python in Sonar not showing up?

I'm running a pretty simple set of python projects through sonar-runner and am having issues getting tests to show up.

I'm running Sonar 3.2.1, with Python Plugin 1.1. The coverage report is generated previously.

I have the following set:

sonar.dynamicAnalysis=reuseReports
sonar.core.codeCoveragePlugin=cobertura
sonar.python.coverage.reportPath=coverage.xml

No matter what I do at this point, the coverage does not show up.

My tests are in the same folder as my sources... could that be the issue? Is there a requirement for how source code is laid out for the coverage report to get analayzed properly by sonar?

Edit: Adding a few more notes...

  • It is a multiproject python instance. I have three projects in there. Everything else seems to show up properly on the sonar report. I'v defined the base and source directories for each and the coverage.xml file has been pre-generated into the base directories of each.
  • The coverage widget shows up but shows:

    Code coverage
    -
    Unit test success
    0 tests
    
  • I'm also seeing when I run sonar-runner:

    10:04:29.641 INFO  p.PhasesTimeProfiler - Sensor PythonCoverageSensor...
    10:04:29.642 INFO  .p.c.CoberturaParser - Parsing report '/home/jenkins/jobs/myproject/workspace/trunk/src/python/coverage.xml'
    10:04:29.883 INFO  p.PhasesTimeProfiler - Sensor PythonCoverageSensor done: 242 ms
    
like image 591
talon8 Avatar asked Oct 11 '12 16:10

talon8


3 Answers

Also had this issue, and pytest was not producing a properly formatted coverage report what sonarqube could utilize. I ran coverage xml -i after pytest produced the coverage report, and this command produces a properly formatted coverage report which sonarqube understands.

like image 75
Will Rubel Avatar answered Oct 18 '22 20:10

Will Rubel


I was running into a similar issue where python test coverage was not reflected in the SonarQube. The issue happens when coverage.xml has the incorrect path to the source files and SonarQube fails to find those source files when parsing the xml report.

There is an open bug for the same issue on coveragepy: https://github.com/nedbat/coveragepy/issues/578 and a workaround https://github.com/LibraryOfCongress/concordia/pull/857 . However, this workaround didn't work for me.

What I ended up doing: after running my test command (python -m pytest -m unit) that generates coverage.xml with wrong path, I ran coverage xml -i . It overwrites the previous coverage.xml and has the correct paths to source files.

Now the SonarQube has the coverage information.

like image 5
Preet Avatar answered Oct 18 '22 18:10

Preet


"sonar.python.coverage.reportPath" must point to the path of the coverage file relative to the "sonar-project.properties" file. Generally, those temporary files are generated in temp folder like "target" or "bin". So your configuration should more look like:

sonar.python.coverage.reportPath=target/coverage.xml
like image 3
Fabrice - SonarSource Team Avatar answered Oct 18 '22 19:10

Fabrice - SonarSource Team