2

I have a project that has a model and other components each in a Cython library. I've created unittests and can run those fine with coverage but I only get coverage information for the test code itself (not the libraries the test code is calling). I've tried to use the debug trace operation in coverage but it doesn't show any attempt to read any of the library code (I get no entries for Tracing or Not tracing). I have linetrace, CYTHON_TRACE=1, and the .coveragerc item for the Cython.coverage plugin all turned on. I've tried to copy the model.c code up to \ but that didn't seem to help.

Here is the (partial) project structure:

\model.pyd
\setup.py
\src\models\windows\model.pyx
\src\models\windows\model.c
\src\models\submodel.pxi
\tests\models\test_model.py

The code in windows\model.pyx includes a number of platform independent pieces of code from \src\models like this:

include '..\\submodel.pxi'

setup.py is run from \ with

python setup.py build_ext --inplace

and looks like this:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
from Cython.Compiler.Options import directive_defaults

import numpy

# Needed for line_profiler - disable for production code
directive_defaults['linetrace'] = True
directive_defaults['binding'] = True

setup(
  ext_modules = cythonize([
    Extension("model",
              ["src\models\windows\model.pyx"]
              #,extra_compile_args = ["-O3"]
              # CYTHON_TRACE required for coverage and line_profiler.  Remove for release.
              ,define_macros=[('CYTHON_TRACE', '1')] 
              )
    ] )
)

unittests is run from \tests\models with

coverage run -m unittest discover

Here is the coverage report output (with plugin debug enabled). test_model is running lots of code in model.pyx and submodel.pxi that's not reported here:

Loaded plugin 'Cython.Coverage': <Cython.Coverage.Plugin object at 0x02F2AA30>
Name                     Stmts   Miss  Cover
--------------------------------------------
test_model.py               40      1    98%
--------------------------------------------
TOTAL                       40      1    98%

Each unit test starts with this code snippet to pull the pyd file from the higher directory:

sys.path.append('..\\..\\')
from model import Model
...

I'm sure this has something to do with the project structure and I don't mind copying source files into the test directories in order to load up the test harness but I can't quite figure out the correct combination to make that happen. All help is welcome.

4

1 回答 1

2

当然,在我写完之后,答案是基本的粗心。

修改setup.py不会自动重新编译 C 代码,并且 C 代码中没有CYTHON_TRACE=1更新。一旦我删除了所有 C/pyd 文件并重新运行setup.py它就可以了。

如果调试跟踪显示它不包括该模块,那将会很有帮助,因为它没有能力跟踪到 pyx/C 文件,因为它们没有被检测。

于 2016-04-02T00:33:01.527 回答