cython icon indicating copy to clipboard operation
cython copied to clipboard

cannot report test coverage

Open johnyf opened this issue 9 years ago • 5 comments

I cannot successfully obtain a report of coverage for the Cython module dd.cudd. I have:

I am using nosetests---I do not know whether nose can cause additional difficulties.

Directory layout under /path roughly:

dd/
tests/

Normally, I run nosetests within tests/, because I want to ensure that nosetests will import dd from site-packages (I have also tried the -w option of nosetests). I have tried running nosetests both inside the tests/ directory, and above it. nose does not report any results for cudd.pyx.

When nose --with-coverage --cover-package=dd runs under tests/, it creates a .coverage that mentions both a /path/tests/dd/cudd.pyx file (nonexistent), as well as the existing /path/dd/dd/cudd.pyx. For the existing file, coverage report complains that

/path/dd/dd/cudd.pyx   NotPython:
Couldn't parse '/path/dd/dd/cudd.pyx' as Python source: 'invalid syntax' at line 20

Also, manually editing the wrong path reported in .coveragerc produces again the above error.

These observations are with:

  • cython == 0.25.1
  • nose == 1.3.7
  • coverage == 4.2

How can I get coverage to be reported for Cython modules? It seems that this may be a problem both with paths that coverage.py reports, and with failure of coverage.py to parse Cython syntax. I am unsure whether this should be addressed by cython (e.g., in the coverage.py plugin Cython.Coverage), or by coverage.py, or both.

May have some relevance to #1461.

johnyf avatar Oct 29 '16 03:10 johnyf

@johnyf I'm not sure that's the reason. I'm having the same issue here. The error says coverage does find the pyx file, it simply fails parsing it. The error when failing to find a pyx file is different. @scoder

pitrou avatar May 11 '18 14:05 pitrou

Actually, looking at Cython.Coverage, it does seem the generated C file (not the pyx file!) is used to find out about the original source code lines. I'm not sure why, since Cython injects __Pyx_TraceCall calls with (almost -- see issue #2264) the right information in the generated C code.

pitrou avatar May 11 '18 14:05 pitrou

It looks like Cython.Coverage has a bug when it tries to match the src file location with the dir layout... https://github.com/cython/cython/issues/3636

webknjaz avatar May 28 '20 20:05 webknjaz

I think that with https://github.com/cython/cython/pull/3831, this is probably fixed. Measuring coverage with a pip installed project is possible if the C-files are generated. Here https://github.com/aio-libs/yarl/blob/a3df0aa/.github/workflows/ci-cd.yml#L250 I wrapped cythonization of PYX files with a script that performs the conversion to C using the same set of options/directives as the PEP 517 build backend. This allowed me to build wheels separately and install them. The project is imported from site-packages/ but the testing is running via python -Im pytest in the project root which makes sure that the locally available package is not imported. But since the Cython.Coverage plugin requires C files, I additionally run the PYX-to-C conversion so that coveragepy can match the measured coverage with the PYX files and put that information into the report (it reports 0 lines and 100% coverage otherwise).

@scoder looks like this issue can be closed now.

webknjaz avatar Dec 08 '23 12:12 webknjaz