目标是将pytest单元测试框架用于使用 Cython 的 Python3 项目。这不是即插即用的事情,因为pytest默认情况下无法导入 Cython 模块。
一种不成功的解决方案是使用该pytest-cython插件,但它对我来说根本不起作用:
> py.test --doctest-cython
usage: py.test [options] [file_or_dir] [file_or_dir] [...]
py.test: error: unrecognized arguments: --doctest-cython
inifile: None
rootdir: /censored/path/to/my/project/dir
Run Code Online (Sandbox Code Playgroud)
要验证我是否已安装该软件包:
> pip freeze | grep pytest-cython
pytest-cython==0.1.0
Run Code Online (Sandbox Code Playgroud)
更新:我正在使用 PyCharm,它似乎没有使用我的 pip 安装的软件包,而是使用我的项目使用的软件包的自定义(?)pycharm 存储库。一旦我添加pytest-cython到该存储库,该命令就会运行,但奇怪的是它无论如何都无法识别 Cython 模块,尽管包/附加组件是专门为此目的而设计的:
> pytest --doctest-cython
Traceback:
tests/test_prism.py:2: in <module>
from cpc_naive.prism import readSequence, processInput
cpc_naive/prism.py:5: in <module>
from calculateScore import calculateScore, filterSortAlphas,
calculateAlphaMatrix_c#, incrementOverlapRanges # cython code
E ImportError: No module …Run Code Online (Sandbox Code Playgroud) 我有一个项目组织如下:
project
??? project
? ??? module1
? ? ??? api.py
? ? ??? _cpython_foo.py
? ? ??? _cython_foo.pyx
? ??? module2
??? setup.py
??? tests
??? module1
??? test_cython_foo.py
??? test_cpython_foo.py
Run Code Online (Sandbox Code Playgroud)
其中api.py进口cythonized扩展:
"""api.py""""
from _cython_foo import cython_fun
Run Code Online (Sandbox Code Playgroud)
我的安装脚本.pyx正确地构建了源代码,我可以cython_fun在已安装的包中使用:
import project.module1.api as module1
module1.cython_fun() # OK
Run Code Online (Sandbox Code Playgroud)
但是,pytest抱怨它无法导入 cython 模块,因为在我调用 setup.js 之前编译的二进制文件没有到位。
project/module1/api.py:2: in <module>
from _cython_foo import cython_fun
E ImportError: No module named _cython_foo
Run Code Online (Sandbox Code Playgroud)
在 pytest 所依赖的项目目录中留下一堆预编译的二进制文件似乎是一种糟糕的风格,那么是否有一种传统的方法可以让 pytest 临时构建 cython 模块,仅用于我的单元测试?