简体   繁体   中英

Coverage of Cython module using py.test and coverage.py

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 , ie 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, ie 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

The current answer here has two parts:

  • pytest configuration
  • coverage.py configuration

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM