Coverage shows zero for Python using SonarScanner in Bamboo Builds

I am using Bamboo as our CI/CD tool. We have sonar-scanner installed as Bamboo sonar agent. I am able to get sonar results on our Bamboo builds and Bitbucket. However, we get coverage as 0% as part of our python code tests.

We interact with bash script to run the python tests using pytest library. I am also generating coverage xml file after running the tests. (I use pytest, pytest-cov and coverage.py libraries for testing).

#!/bin/bash
pytest --cov=src
coverage xml -i
coverage html

This does create a coverage.xml file in the testing folder.

<?xml version="1.0" ?>
<coverage branch-rate="0" branches-covered="0" branches-valid="0" complexity="0" line-rate="0.543" lines-covered="613" lines-valid="1129" timestamp="1600348721521" version="5.2.1">
	<!-- Generated by coverage.py: https://coverage.readthedocs.io -->
	<!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd -->
	<sources>
		<source></source>
	</sources>
	<packages>
		<package branch-rate="0" complexity="0" line-rate="0.2644" name=".home.user.Repos.project.src.folder">
			<classes>
				<class branch-rate="0" complexity="0" filename="/home/user/Repos/project/src/folder/file.py" line-rate="0.2644" name="file.py">
					<methods/>

However when I set the

sonar.python.coverage.reportPaths=tests/coverage.xml, *coverage.xml, tests/coverage.xml

I can’t find the coverage.xml file. sonar.properties and tests are on the same dir.
– sonar.properties
– tests

I can see the results of coverage.xml. The only issue is sonar-scanner cannot find the location of coverage.xml (my best guess).

17-Sep-2020 17:27:47	INFO: Working dir: /root/bamboo-agent-home/xml-data/build-dir/CBP-CT22-JOB1/.scannerwork
17-Sep-2020 17:27:47	INFO: Source paths: src
17-Sep-2020 17:27:47	INFO: Source encoding: US-ASCII, default locale: en_US
17-Sep-2020 17:27:47	INFO: Index files
17-Sep-2020 17:27:47	INFO: Excluded sources: 
17-Sep-2020 17:27:47	INFO:   **/node_modules/**
17-Sep-2020 17:27:47	INFO: 55 files indexed
17-Sep-2020 17:27:47	INFO: 0 files ignored because of inclusion/exclusion patterns
17-Sep-2020 17:27:47	INFO: Quality profile for py: PythonQuality
17-Sep-2020 17:27:48	WARNING: An illegal reflective access operation has occurred
17-Sep-2020 17:27:48	WARNING: Illegal reflective access by net.sf.cglib.core.ReflectUtils$1 (file:/root/.sonar/cache/2089ae0d5cb0f75cbfb0902f1af146e2/sonar-tsql-plugin-1.3.1.3067.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
17-Sep-2020 17:27:48	WARNING: Please consider reporting this to the maintainers of net.sf.cglib.core.ReflectUtils$1
17-Sep-2020 17:27:48	WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
17-Sep-2020 17:27:48	WARNING: All illegal access operations will be denied in a future release
17-Sep-2020 17:27:49	INFO: Sensor SonarJavaXmlFileSensor [java]
17-Sep-2020 17:27:49	INFO: Sensor SonarJavaXmlFileSensor [java] (done) | time=1ms
17-Sep-2020 17:27:49	INFO: Sensor Python Squid Sensor [python]
17-Sep-2020 17:27:50	INFO: Sensor Python Squid Sensor [python] (done) | time=1146ms
17-Sep-2020 17:27:50	INFO: Sensor Cobertura Sensor for Python coverage [python]
17-Sep-2020 17:27:50	WARN: No report was found for sonar.python.coverage.reportPaths using pattern *coverage*.xml
17-Sep-2020 17:27:50	WARN: No report was found for sonar.python.coverage.reportPaths using pattern coverage.xml
17-Sep-2020 17:27:50	WARN: No report was found for sonar.python.coverage.reportPaths using pattern **coverage.xml
17-Sep-2020 17:27:50	WARN: No report was found for sonar.python.coverage.reportPaths using pattern /project/coverage.xml
17-Sep-2020 17:27:50	INFO: Sensor Cobertura Sensor for Python coverage [python] (done) | time=25ms
17-Sep-2020 17:27:50	INFO: Sensor PythonXUnitSensor [python]
17-Sep-2020 17:27:50	INFO: Sensor PythonXUnitSensor [python] (done) | time=2ms
17-Sep-2020 17:27:50	INFO: Sensor PylintSensor [python]

I have seen options in which others have used sed command to solve it (this link) but I cannot do that as I am running using Bamboo agent.

Can you help how I can configure so Bamboo agent for Sonar-Scanner can find the xml file?

P.S. I can only interact with Bamboo agent through Bamboo. I do not have access to change settings etc of SonarScanner on Bamboo.

sed may not be relevant in your case anyway. In my issue here the problem was not that SonarQube couldn’t find the coverage.xml file. It was that it couldn’t interpret it. This was because the tests were being run against the installed version of my code under site-packages rather than against my actual checked-out source code. So the paths in coverage.xml didn’t match the source code paths. Your problem seems to be it can’t find the coverage.xml file in the first place.

1 Like

Yes, that is my guess also. I thought sonar-scanner takes sonar.properties file level as the main directory and looks for xml file from that point onwards. I have even place the xml file to same level as sonar.properties file and still no luck.