I want to get coverage information of a Cython module using some (unit) tests written in Python. What I have right now is coverage of the tests themselves, i.e. which lines of the tests are executed by running py.test
. While nice to look at, I would rather get coverage of the .pyx
file, i.e. which lines of the C/Python interface are covered by my tests.
I found some info already but wasn't able to get it running for my project:
http://blog.behnel.de/posts/coverage-analysis-for-cython-modules.html
https://medium.com/@dfdeshom/better-test-coverage-workflow-for-cython-modules-631615eb197a
How to use coverage analysis with Cython
This is the code in question: https://github.com/SCIP-Interfaces/PySCIPOpt/tree/coverage
To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.
2019-04-30. Code coverage is a simple tool for checking which lines of your application code are run by your test suite. 100% coverage is a laudable goal, as it means every line is run at least once. Coverage.py is the Python tool for measuring code coverage.
Increase coverage by adding more tests The code coverage has increased to 78% on adding another test case. It can be increased further to 100% in a similar fashion.
The current answer here has two parts:
Let's start with the pytest configuration. Currently on your coverage
branch there is the .travis.yml
file with the following line: py.test --cov tests
, this tells pytest-cov to show you the coverage for the tests
directory, which you don't want.
You can run py.test tests --cov
instead or change your setup.cfg
and not pass the tests
at all.
You would then have the setup.cfg
file with:
[tool:pytest]
testpaths =
tests
In the .coverage
file you need:
[run]
plugins = Cython.Coverage
source = src/pyscipopt
omit =
tests
*__init__.py
Specifying the paths
separately is obsolete and I have removed it. This way coverage.py
knows that it should only check coverage for the src/pyscipopt
directory.
After these changes the output I got is:
---------- coverage: platform darwin, python 3.6.4-final-0 -----------
Name Stmts Miss Branch BrPart Cover
---------------------------------------------------------------
src/pyscipopt/Multidict.py 17 17 10 0 0%
src/pyscipopt/conshdlr.pxi 301 129 0 0 57%
src/pyscipopt/expr.pxi 168 57 0 0 66%
src/pyscipopt/heuristic.pxi 44 18 0 0 59%
src/pyscipopt/lp.pxi 235 177 0 0 25%
src/pyscipopt/pricer.pxi 52 24 0 0 54%
---------------------------------------------------------------
TOTAL 817 422 10 0 48%
This looks like the proper output you want, but I do see the scip.pyx
file is missing. You can also look at a related issue on github. I am not investigating further because it answers your question and I think you will be able to go from here with whatever is missing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With