Test/Test Coverage with Python in Sonar not showing up?
Asked Answered
A

4

18

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
    
Amadoamador answered 11/10, 2012 at 16:40 Comment(3)
Using SonarQube 4.4 and the same issue exists here. sonar-runner is processing the correct file, but coverage report doesn't get displayed on the dashboard. Did you find a resolution?Crocus
Unforutnately, I do not believe so. We've since given up with Sonar with our python code. When I have more time, I'll give it another shot.Amadoamador
How are you generating the coverage report file?Flanigan
A
15

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.

Amphoteric answered 10/5, 2017 at 14:12 Comment(2)
Note that if you install the pytest-cov plugin, than you can get the equivalent by passing --cov-report xml:/path/to/your/coverage.xml to pytest directly.Custodial
Using pytest 4.5 and pytest-cov 2.7.1 and --cov-report xml:coverage.xml still did not produce test coverage lines on sonar scannerConsistent
V
5

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.

Volk answered 6/5, 2020 at 11:47 Comment(0)
T
3

"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
Trencherman answered 12/10, 2012 at 8:43 Comment(2)
Mine are being generated in the root folder of my project. So my path should be correct...Amadoamador
Sorry if post is a little old. I found differences if the working directory change. Coverage its not respect the properties file but respect the working dir.Festive
S
2

Simple answer to this question :-

  1. This issue mostly arises when we generate XML report at another location and runs SonarAnalyze on another location.Due to which Sonaranalyze will not be able to pick those files as mentioned in coverage.xml.

  2. So if you see the source in the xml file then they should contain some location so if the location mentioned in the coverage.xml matches with path of Sonaranalyze then your issue would be resolved. source<>a/b/c/d<>source

  3. Now Challenge would be how to check the folder structure of coverage.xml.To get the correct file location and path.I have used command line to check as I was using windows agent. (Azure devops)

  • task: CmdLine@2

    inputs:

    script: |
    
      echo  '$(System.DefaultWorkingDirectory)'
    
      dir
    
      dir subfoldername
    

where dir will list you all the directories or files

  1. Then after knowing the folder structure I just manipulated the coverage.xml by making changes of the source directory inside coverage.xml. I used powershell to edit the coverage.xml you can use sed as well depends upon your agent.

For task: PowerShell@2

  inputs:

    targetType: 'inline'

    script: |


  
      (Get-Content coverage.xml)| ForEach-Object { $_.replace("a/b/c","$(System.DefaultWorkingDirectory)") } | Set-Content coverage2.xml

      Copy-Item -Path coverage2.xml -Destination coverage.xml -PassThru

      Remove-Item coverage2.xml

For bash Users sed -i 's#a/b/c#D:/AZ#g;s#/x/c#D:/m#g;' coverage.xml

and then I successfully got the coverage in Sonarqube.

Selfreproach answered 1/7, 2021 at 5:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.