I'm trying to run coverage analysis on some Cython code using pytest-cov
and coveralls.io. I've got as far as building the extension modules with tracing enabled, and running the analysis with the help of the links below:
http://docs.cython.org/src/tutorial/profiling_tutorial.html
http://blog.behnel.de/posts/coverage-analysis-for-cython-modules.html
However, I'm getting some results that I can't explain. It seems that many of the def
/cdef
/cpdef
lines in the code are showing as not running, despite code within them being OK. The results aren't even consistent as some lines seem OK.
Example report: https://coveralls.io/files/1871744040
I don't know if I'm calling something wrong, if this is a bug, or if I'm just not interpreting the results correctly.
In the example above, the get_cost
method seems OK, but the __set__
method for the property above is not called, despite the lines within the function having been called.
Update: It seems the issue is with Cython classes. If the class is defined with def
rather than cdef
the problem goes away. I guess there isn't full support for this yet.
If the Cython tracing facility does not seem to work as intended, it should be possible to use gcov
for the coverage analysis of cython code. This way one could verify if some line of the generated C code is executed or not.
With a simple main.pyx
import mymod
def main():
mymod.test()
and mymod.pyx
def test():
return 42
and then
cython --embed main.pyx
cython mymod.pyx
gcc -O1 -fPIC -fprofile-arcs -ftest-coverage -Wall -I/usr/include/python2.7 -c -o main.o main.c
gcc main.o -fprofile-arcs -lpython2.7 -lgcov -o main
gcc -O1 -fPIC -fprofile-arcs -ftest-coverage -Wall -I/usr/include/python2.7 -c -o mymod.o mymod.c
gcc -shared mymod.o -fprofile-arcs -lgcov -lpython2.7 -o mymod.so
an executable was created. After executing ./main
main.gcda and mymod.gcda were created for gcov.
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