java.lang.IllegalStateException when parsing coverage xml from pytest-cov

I’m using an azure devops build agent to generate a coverage xml:
python -m pytest --cov=<REDACTED> <REDACTED>/tests -cov-report=xml

My sonar-project.properties file contains the line:
sonar.python.coverage.reportPaths = **/*coverage

Please provide the expected pytest-cov command for providing a valid coverage xml format as well as the expected sonar-project.properties entry if the report is called ‘coverage.xml’ and found in the project root directory.

Thanks!

INFO: 28/36 files analyzed, current file: <REDACTED>
INFO: 36/36 source files have been analyzed
INFO: Sensor Python Sensor [python] (done) | time=14442ms
INFO: Sensor Cobertura Sensor for Python coverage [python]
##[error]java.lang.IllegalStateException: Error parsing the report 'D:\a\1\s\.coverage'
java.lang.IllegalStateException: Error parsing the report 'D:\a\1\s\.coverage'
##[error]at org.sonar.plugins.python.coverage.PythonCoverageSensor.parseReport(PythonCoverageSensor.java:124)
	at org.sonar.plugins.python.coverage.PythonCoverageSensor.execute(PythonCoverageSensor.java:81)
	at org.sonar.plugins.python.coverage.PythonCoverageSensor.parseReport(PythonCoverageSensor.java:124)
	at org.sonar.plugins.python.coverage.PythonCoverageSensor.execute(PythonCoverageSensor.java:81)
##[error]at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:62)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:75)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:48)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:66)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:48)
	at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:64)
	at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:62)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:75)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:48)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:66)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:48)

##[error]at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:123)

(The stack goes on like this for quite a while. Let me know if you need more).

Hey there.

You can find documentation on Python Test Coverage here: Python Test Coverage | SonarCloud Docs

If your coverage report is called coverage.xml and located in the root directory (the same directory you’re executing analysis from)… you should just be able to use sonar.python.coverage.reportPaths=coverage.xml.

My properties file contains:
sonar.python.coverage.reportPaths=.coverage

I’ve verified that the coverage file exists. Here’s the dir output prior to the SonarCloud Prepare step (the file generated from pytest-cov is called .coverage):

 Directory of D:\a\1\s

08/31/2022  07:19 PM    <DIR>          .
08/31/2022  07:16 PM    <DIR>          ..
08/31/2022  07:19 PM            53,248 .coverage
08/31/2022  07:16 PM               522 .gitattributes
08/31/2022  07:16 PM             4,943 .gitignore
08/31/2022  07:16 PM             2,470 azure-pipelines.yml
08/31/2022  07:19 PM    <DIR>          <REDACTED>
08/31/2022  07:16 PM               552 setup.py
08/31/2022  07:16 PM               443 sonar-project.properties

When I run the Sonar Cloud analyze step, it fails when trying to parse the pytest-cov xml report:

##[error]ERROR: Error during SonarScanner execution
ERROR: Error during SonarScanner execution
##[error]java.lang.IllegalStateException: Error parsing the report 'D:\a\1\s\.coverage'
java.lang.IllegalStateException: Error parsing the report 'D:\a\1\s\.coverage'
##[error]at org.sonar.plugins.python.coverage.PythonCoverageSensor.parseReport(PythonCoverageSensor.java:124)
	at org.sonar.plugins.python.coverage.PythonCoverageSensor.execute(PythonCoverageSensor.java:81)
	at org.sonar.plugins.python.coverage.PythonCoverageSensor.parseReport(PythonCoverageSensor.java:124)
	at org.sonar.plugins.python.coverage.PythonCoverageSensor.execute(PythonCoverageSensor.java:81)

...

Any ideas on how to get past the java.lang.IllegalStateException error?

If you cat .coverage – is it an XML file?

1 Like

I’m on windows, not unix so I don’t have cat.

But windows explorer seems to think it’s an xml

Apparently the .coverage file was not the expected xml file.

I was missing a dash in my pytest argument. I needed to add:
--cov-report=xml

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.