我想使用一些用 Python 编写的(单元)测试来获取 Cython 模块的覆盖率信息。我现在拥有的是测试本身的覆盖范围,即通过运行py.test
来执行哪些测试行。虽然看起来不错,但我更愿意了解 .pyx
文件,即我的测试涵盖了 C/Python 接口(interface)的哪些行。
我已经找到了一些信息,但无法为我的项目运行它:
http://blog.behnel.de/posts/coverage-analysis-for-cython-modules.html
https://medium.com/@dfdeshom/better-test-coverage-workflow-for-cython-modules-631615eb197a
How to use coverage analysis with Cython
这是有问题的代码:https://github.com/SCIP-Interfaces/PySCIPOpt/tree/coverage
最佳答案
这里的当前答案有两部分:
- pytest 配置
- coverage.py 配置
让我们从 pytest 配置开始。当前在您的 coverage
分支上有 .travis.yml
文件,其中包含以下行:py.test --cov 测试
,这告诉 pytest -cov 向您显示您不想要的 tests
目录的覆盖范围。
您可以改为运行 py.test tests --cov
或更改您的 setup.cfg
而根本不通过 tests
。
然后您将拥有 setup.cfg
文件,其中包含:
[tool:pytest]
testpaths =
tests
在你需要的.coverage
文件中:
[run]
plugins = Cython.Coverage
source = src/pyscipopt
omit =
tests
*__init__.py
单独指定 paths
已过时,我已将其删除。这样 coverage.py
知道它应该只检查 src/pyscipopt
目录的覆盖率。
在这些更改之后,我得到的输出是:
---------- coverage: platform darwin, python 3.6.4-final-0 -----------
Name Stmts Miss Branch BrPart Cover
---------------------------------------------------------------
src/pyscipopt/Multidict.py 17 17 10 0 0%
src/pyscipopt/conshdlr.pxi 301 129 0 0 57%
src/pyscipopt/expr.pxi 168 57 0 0 66%
src/pyscipopt/heuristic.pxi 44 18 0 0 59%
src/pyscipopt/lp.pxi 235 177 0 0 25%
src/pyscipopt/pricer.pxi 52 24 0 0 54%
---------------------------------------------------------------
TOTAL 817 422 10 0 48%
这看起来像您想要的正确输出,但我确实看到 scip.pyx
文件丢失了。您也可以查看 related issue on github .我不会进一步调查,因为它回答了你的问题,我认为你可以从这里带着任何缺失的东西离开。
关于python - 使用 py.test 和 coverage.py 覆盖 Cython 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48505458/