I am trying to generate code coverage report for a cython module, an d facing issues.
I have a simple c++ code : apple.h and apple.cpp files. The cpp file is simple as :
using namespace std;
namespace mango {
apple::apple(int key) {
_key = key;
};
int apple::execute()
{
return _key*_key;
};
}
I have written a basic cython code over this in "cyApple.pyx" :
# cython: linetrace=True
from libcpp.list cimport list as clist
from libcpp.string cimport string
from libc.stdlib cimport malloc
cdef extern from "apple.h" namespace "mango" :
cdef cppclass apple:
apple(int)
int execute()
cdef class pyApple:
cdef apple* aa
def __init__(self, number):
self.aa = new apple(number)
def getSquare(self):
return self.aa.execute()
My setup.py file :
from distutils.core import setup, Extension
from Cython.Build import cythonize
compiler_directives = {}
define_macros = []
compiler_directives['profile'] = True
compiler_directives['linetrace'] = True
define_macros.append(('CYTHON_TRACE', '1'))
setup(ext_modules = cythonize(Extension(
"cyApple",
sources=["cyApple.pyx", "apple.cpp"],
define_macros=define_macros,
language="c++",
), compiler_directives=compiler_directives))
This generates a proper library cyApple.so. I have also written a simple appletest.py file to run test cases :
import cyApple, unittest
class APPLETests(unittest.TestCase):
def test1(self):
temp = 5
apple1 = cyApple.pyApple(temp)
self.assertEqual(25, apple1.getSquare())
suite = unittest.TestLoader().loadTestsFromTestCase(APPLETests)
unittest.TextTestRunner(verbosity=3).run(suite)
The test works fine. The problem is I need to get code coverage for my cyApple.pyx file
When i run "coverage report -m" I get the error and coverage for only my test file not pyx file.
cyApple.pyx NotPython: Couldn't parse '/home/final/cyApple.pyx' as Python source: 'invalid syntax' at line 2
Name Stmts Miss Cover Missing
--------------------------------------------
appletest.py 8 1 88% 9
I tried to look online and get some help , so i added
.coveragerc file with contents as :
[run]
plugins = Cython.Coverage
On running "coverage run appletest.py" i get errors :
...
...
...
ImportError: No module named Coverage
I want to generate simple code coverage report for my pyx file. How i can do it in a simple way ?
I reinstalled Cython-0.28.3. Now on running "coverage run appletest.py" I am getting error :
test1 (__main__.APPLETests) ... Segmentation fault (core dumped)
This is my apple.h file :
#include<iostream>
namespace mango {
class apple {
public:
apple(int key);
int execute();
private:
int _key;
};
}
You must update Cython. The documentation states:
Since Cython 0.23, line tracing (see above) also enables support for coverage reporting with the coverage.py tool.