如何在Cython中使用覆盖率分析 [英] How to use coverage analysis with Cython
问题描述
我正在尝试使用 pytest-cov
和Coveralls.io对某些Cython代码进行覆盖率分析。我已经尽可能构建了启用了跟踪的扩展模块,并借助以下链接运行了分析:
在上面的示例中, get_cost
方法似乎可以,但是上述属性的 __ set __
方法是
更新:似乎问题出在Cython类上。如果该类是用 def
而不是 cdef
定义的,那么问题就解决了。我猜还没有对此的完全支持。
如果Cython跟踪工具似乎无法正常工作,应该可以使用 gcov
对cython代码进行覆盖率分析。这样可以验证是否执行了生成的C代码的某些行。
使用简单的 main.pyx
import mymod
def main():
mymod.test()
和 mymod.pyx
def test():
返回42
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
创建了一个可执行文件。执行 ./ main
后,为 gcov 创建了 main.gcda 和 mymod.gcda
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.
这篇关于如何在Cython中使用覆盖率分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!