How to trigger Sonar code coverage for python - SonarCloud

Hello, I am new to SonarCloud.
I use SonarCloud for my azure devops. Everything is fine and clean, however, i do not know how can i run the code coverage test in my project. I have already included the test, install unittest with python and it run on my local computer, but the results do not change when i run it peer SonarCloud.
Can you guy help me, Thanks in advance

Hi,

Does you coverage files are located within the default folder SonarCloud is taking in account : coverage-reports/coverage-.xml ?

Otherwise, you’ll probably need to override the setting “sonar.python.coverage.reportPaths” to specify your own (see https://sonarcloud.io/documentation/analysis/coverage/)

Mickaël

3 Likes

Do you mean that i need to write sth similar to “sonar.python.coverage.reportPath = \aaa\test” in “sonar-project.properties”? Otherwise, i do not know where can i find the coverage-,xml file

Sorry, i forgot to mention, this setting can be put in the “Additionnal properties” field of the “Prepare Analysis” task on Azure DevOps

2 Likes

do i need to run something like “python -m unittest” or “coverage …” inside the yaml file?

i also try to add

sonar.python.coverage.reportPath: \energy_saving\test*

into SonarCloudPrepare in my yaml file, it runs but the covarage is still 0%

Yes, you need at least to run unit tests before the “Run analysis” task to have coverage.
Then, depending on if your provide the setting or not, it means that you either converted on your own the unit tests result to a coverage file or let the scanner doing so for you.

2 Likes

I already added python -m unittest discovery before SonarCloudPrepare and SonarCloudAnalyze task, in the Prepare i have Scannermode as CLI, configMode as manual and sonar.python.coverage.reportPath as [my_test_folder]*
However, it still shows that i have 0% code coverage for my test (the unittest run ok)

Do i need to export the XML output of the unittest/coverage, and then navigate the paths into it? i got the warning “No report was found for sonar.python.coverage.reportPath using pattern”

Hi,

May i ask you to first have a look at the analysis task logs, to see if something has been found by the scanner (or not). You can also add the “sonar.verbose=true” additionnal property to get more logs from that task and help troubleshoot (you can post them here if you want)

Thank you.

1 Like

Hello, i find a way to almost solve my problems: after generate the coverage.xml using coverage for python, i add this into “sonar-project.properties”:

sonar.python.coverage.reportPaths = ./coverage.xml

It can now collect the information from my source code coverage and transfer it into Sonar

However, because coverage.xml only show the file in the source, not in my test folder, the code coverage number for these testé in my sonar is still 0%. Can you help be about that?

Thanks in advance