Discussion:
Python code coverage reporting problem
Ioannis Foukarakis
2012-11-17 16:47:28 UTC
Permalink
Hi,

As this is my first email here, I'd like to grab the opportunity and thank
you for the great work!

I'd like to ask for some help on a problem I have setting up sonar with a
python project. I'm not sure if I'm doing something wrong or if I've hit a
bug. My project's structure is like the following:
[root folder]
- .pylint
- sonar-project.properties
- src (includes sources)
- tests (includes unit tests)
- testdata (some test data files for my tests)
- reports (generated code coverage and nosetests are added here.

My sonar-project.properties is pretty much like the following:

sonar.projectKey=this.is:myproject
sonar.projectName=New python project
sonar.projectVersion=1.0-SNAPSHOT

# Comma-separated paths to directories with sources (required)
sonar.sources=src

# Language
sonar.language=py

# Encoding of the source files
sonar.sourceEncoding=UTF-8

# JDBC for sonar
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

# Code coverage
sonar.dynamicAnalysis=reuseReports
sonar.core.codeCoveragePlugin=cobertura
sonar.python.coverage.reportPath=reports/coverage.xml

# Test results
sonar.python.xunit.reportPath=reports/nosetests.xml
From my understanding, I have to first run nosetests to generate unit tests
and code coverage reports. So I run it like this:
nosetests --with-xunit --with-coverage --cover-xml --cover-branches
--xunit-file=reports/nosetests.xml --cover-xml-file=reports/coverage.xml

Finally, if I execute sonar-runner, I get my project on sonar, and unit
tests are successfully reported. However neither pylint nor code coverage
are reported. I tried adding sonar.python.pylint_config=.pylintrc in
sonar-project.properties, but I got no results from pylint. I also tried
sonar-runner -X and saw some lines like the following (I edit it to remove
paths):
18:30:31.793 INFO .p.c.CoberturaParser - Parsing report '...<project
root>/reports/coverage.xml'
18:30:31.832 DEBUG PythonCoverageSensor - Cannot find the file 'afile.py',
ignoring coverage measures
18:30:31.832 DEBUG PythonCoverageSensor - Cannot find the file
'anotherfile.py', ignoring coverage measures

It looks like PythonCoverageSensor ignores sonar.sources property. I also
tried copying files under root folder and reports folder, and I still got
the same problem. Finally, I updated project's default coverage plugin to
"cobertura" and tried re-running sonar-runner, but I'm still getting the
same results. My configuration is the following:
Sonar version: 3.3.1
Runner version: 2.0
Python version 2.7.3

Thank you in advance for your time!

Kind regards,
Yiannis
Waleri Enns
2012-11-18 08:47:15 UTC
Permalink
Hi Ioannis,

I'll try to give you some hints.
Post by Ioannis Foukarakis
Hi,
As this is my first email here, I'd like to grab the opportunity and
thank you for the great work!
I'd like to ask for some help on a problem I have setting up sonar with
a python project. I'm not sure if I'm doing something wrong or if I've
[root folder]
- .pylint
- sonar-project.properties
- src (includes sources)
- tests (includes unit tests)
- testdata (some test data files for my tests)
- reports (generated code coverage and nosetests are added here.
sonar.projectKey=this.is:myproject
sonar.projectName=New python project
sonar.projectVersion=1.0-SNAPSHOT
# Comma-separated paths to directories with sources (required)
sonar.sources=src
# Language
sonar.language=py
# Encoding of the source files
sonar.sourceEncoding=UTF-8
# JDBC for sonar
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
# Code coverage
sonar.dynamicAnalysis=reuseReports
This one has no effect in the case of python plugin
Post by Ioannis Foukarakis
sonar.core.codeCoveragePlugin=cobertura
This is Java specific, you dont need this.
Post by Ioannis Foukarakis
sonar.python.coverage.reportPath=reports/coverage.xml
# Test results
sonar.python.xunit.reportPath=reports/nosetests.xml
From my understanding, I have to first run nosetests to generate unit
nosetests --with-xunit --with-coverage --cover-xml --cover-branches
--xunit-file=reports/nosetests.xml --cover-xml-file=reports/coverage.xml
I advise you to use the procedure described in the documentation:
calling coverage explicitly and not via nosetest. Thats the only way to
pass the --sources-Parameter to coverage, which is critical for getting
accurate results.
Post by Ioannis Foukarakis
Finally, if I execute sonar-runner, I get my project on sonar, and unit
tests are successfully reported. However neither pylint nor code
coverage are reported.
You are using default profile where pylint rules are deactivated. You
have to use a customized one with pylint rules activated.
Post by Ioannis Foukarakis
I tried adding
sonar.python.pylint_config=.pylintrc in sonar-project.properties, but I
got no results from pylint. I also tried sonar-runner -X and saw some
18:30:31.793 INFO .p.c.CoberturaParser - Parsing report '...<project
root>/reports/coverage.xml'
18:30:31.832 DEBUG PythonCoverageSensor - Cannot find the file
'afile.py', ignoring coverage measures
18:30:31.832 DEBUG PythonCoverageSensor - Cannot find the file
'anotherfile.py', ignoring coverage measures
The plugin cannot associate the coverage measures with source files.
That can be caused by the lacking --sources-Parameter. Try to generate
the reports as documented in the WiKi.

HTH
--
Waleri
Post by Ioannis Foukarakis
It looks like PythonCoverageSensor ignores sonar.sources property. I
also tried copying files under root folder and reports folder, and I
still got the same problem. Finally, I updated project's default
coverage plugin to "cobertura" and tried re-running sonar-runner, but
Sonar version: 3.3.1
Runner version: 2.0
Python version 2.7.3
Thank you in advance for your time!
Kind regards,
Yiannis
Ioannis Foukarakis
2012-11-18 23:17:12 UTC
Permalink
Thank you very much!

I finally managed to have code coverage and unit test reporting using
distutils and a couple of custom commands for running nose and coverage.py.
Post by Waleri Enns
Hi Ioannis,
I'll try to give you some hints.
Post by Ioannis Foukarakis
Hi,
As this is my first email here, I'd like to grab the opportunity and
thank you for the great work!
I'd like to ask for some help on a problem I have setting up sonar with
a python project. I'm not sure if I'm doing something wrong or if I've
[root folder]
- .pylint
- sonar-project.properties
- src (includes sources)
- tests (includes unit tests)
- testdata (some test data files for my tests)
- reports (generated code coverage and nosetests are added here.
sonar.projectKey=this.is:mypro**ject
sonar.projectName=New python project
sonar.projectVersion=1.0-**SNAPSHOT
# Comma-separated paths to directories with sources (required)
sonar.sources=src
# Language
sonar.language=py
# Encoding of the source files
sonar.sourceEncoding=UTF-8
# JDBC for sonar
sonar.jdbc.url=jdbc:mysql://**localhost:3306/sonar?**useUnicode=true&**
characterEncoding=utf8&**rewriteBatchedStatements=true
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
# Code coverage
sonar.dynamicAnalysis=**reuseReports
This one has no effect in the case of python plugin
sonar.core.codeCoveragePlugin=**cobertura
This is Java specific, you dont need this.
sonar.python.coverage.**reportPath=reports/coverage.**xml
Post by Ioannis Foukarakis
# Test results
sonar.python.xunit.reportPath=**reports/nosetests.xml
From my understanding, I have to first run nosetests to generate unit
nosetests --with-xunit --with-coverage --cover-xml --cover-branches
--xunit-file=reports/**nosetests.xml --cover-xml-file=reports/**
coverage.xml
I advise you to use the procedure described in the documentation: calling
coverage explicitly and not via nosetest. Thats the only way to pass the
--sources-Parameter to coverage, which is critical for getting accurate
results.
Post by Ioannis Foukarakis
Finally, if I execute sonar-runner, I get my project on sonar, and unit
tests are successfully reported. However neither pylint nor code
coverage are reported.
You are using default profile where pylint rules are deactivated. You have
to use a customized one with pylint rules activated.
I tried adding
Post by Ioannis Foukarakis
sonar.python.pylint_config=.**pylintrc in sonar-project.properties, but I
got no results from pylint. I also tried sonar-runner -X and saw some
18:30:31.793 INFO .p.c.CoberturaParser - Parsing report '...<project
root>/reports/coverage.xml'
18:30:31.832 DEBUG PythonCoverageSensor - Cannot find the file
'afile.py', ignoring coverage measures
18:30:31.832 DEBUG PythonCoverageSensor - Cannot find the file
'anotherfile.py', ignoring coverage measures
The plugin cannot associate the coverage measures with source files. That
can be caused by the lacking --sources-Parameter. Try to generate the
reports as documented in the WiKi.
HTH
--
Waleri
Post by Ioannis Foukarakis
It looks like PythonCoverageSensor ignores sonar.sources property. I
also tried copying files under root folder and reports folder, and I
still got the same problem. Finally, I updated project's default
coverage plugin to "cobertura" and tried re-running sonar-runner, but
Sonar version: 3.3.1
Runner version: 2.0
Python version 2.7.3
Thank you in advance for your time!
Kind regards,
Yiannis
------------------------------**------------------------------**---------
http://xircles.codehaus.org/**manage_email<http://xircles.codehaus.org/manage_email>
Loading...