简体   繁体   中英

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 .pcCoberturaParser - Parsing report '/home/jenkins/jobs/myproject/workspace/trunk/src/python/coverage.xml' 10:04:29.883 INFO p.PhasesTimeProfiler - Sensor PythonCoverageSensor done: 242 ms

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.

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.

"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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM