Python 3.x 如何将Cython与pytest一起使用?

Python 3.x 如何将Cython与pytest一起使用?,python-3.x,unit-testing,pycharm,cython,pytest,Python 3.x,Unit Testing,Pycharm,Cython,Pytest,目标是为使用Cython的Python3项目使用pytest单元测试框架。这不是一个即插即用的东西,因为默认情况下,pytest无法导入Cython模块 一个不成功的解决方案是使用该插件,但它对我根本不起作用: > py.test --doctest-cython usage: py.test [options] [file_or_dir] [file_or_dir] [...] py.test: error: unrecognized arguments: --doctest-cytho

目标是为使用Cython的Python3项目使用
pytest
单元测试框架。这不是一个即插即用的东西,因为默认情况下,
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
要验证是否已安装软件包,请执行以下操作:

> pip freeze | grep pytest-cython
pytest-cython==0.1.0
更新: 我使用的是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 named 'calculateScore'
更新

from distutils.core import setup
from distutils.extension import Extension

from setuptools import find_packages
from Cython.Build import cythonize
import numpy

try:  # try to build the .c file
    from Cython.Distutils import build_ext
except ImportError:  # if the end-user doesn't have Cython that's OK; you should have shipped the .c files anyway.
    use_cython = False
else:
    use_cython = True

cmdclass = {}
ext_modules = []

if use_cython:
    ext_modules += [
        Extension("cpc_naive.calculateScore", ["cpc_naive/calculateScore.pyx"],
                  extra_compile_args=['-g'],   # -g for debugging
                  define_macros=[('CYTHON_TRACE', '1')]),
    ]
    cmdclass.update({'build_ext': build_ext})
else:
    ext_modules += [
        Extension("cpc_naive.calculateScore", ["cpc_naive/calculateScore.c"],
                  define_macros=[('CYTHON_TRACE', '1')]),  # compiled C files are stored in /home/pdiracdelta/.pyxbld/
    ]

setup(
    name='cpc_naive',
    author=censored,
    author_email=censored,
    license=censored,
    packages=find_packages(),
    cmdclass=cmdclass,
    ext_modules=ext_modules,
    install_requires=['Cython', 'numpy'],
    include_dirs=[numpy.get_include()],
    setup_requires=['pytest-runner'],
    tests_require=['pytest']
)
首先,我忘了将
setup\u requires=['pytest-runner',…]
tests\u require=['pytest',…]
添加到安装脚本中。一旦我这么做了,我又犯了一个错误:

> python3 setup.py pytest
Traceback (most recent call last):
  File "setup.py", line 42, in <module>
    tests_require=['pytest']
(...)
AttributeError: type object 'test' has no attribute 'install_dists'
更新3(部分修复)
正如@hoefling建议的那样,我将
pytestrunner
降级为@hoefling建议的版本,应该使用
pytestrunner
version您是在测试只能从
cython
模块(
cdef
)中访问的函数,还是只测试可以从调用Python访问的函数(
cpdef
def
)?如果
pytest
命令未显示,则您可能已使用错误的
pip
安装了
pytest runner
-是否
pip
引用了您的
python3
安装?请检查
pip-V
产生了什么,可能它引用了
python2
。如果是这种情况,请检查是否有
pip3
使用
pip3安装pytest runner
python3
提供并安装
pytest runner
。您的cython代码是否以自动编译为setup.py文件的方式编写?听起来好像您正试图手动构建C扩展。@hpaulj我使用的是
def
,那么您可能会经验。降级
pytest runner
from distutils.core import setup
from distutils.extension import Extension

from setuptools import find_packages
from Cython.Build import cythonize
import numpy

try:  # try to build the .c file
    from Cython.Distutils import build_ext
except ImportError:  # if the end-user doesn't have Cython that's OK; you should have shipped the .c files anyway.
    use_cython = False
else:
    use_cython = True

cmdclass = {}
ext_modules = []

if use_cython:
    ext_modules += [
        Extension("cpc_naive.calculateScore", ["cpc_naive/calculateScore.pyx"],
                  extra_compile_args=['-g'],   # -g for debugging
                  define_macros=[('CYTHON_TRACE', '1')]),
    ]
    cmdclass.update({'build_ext': build_ext})
else:
    ext_modules += [
        Extension("cpc_naive.calculateScore", ["cpc_naive/calculateScore.c"],
                  define_macros=[('CYTHON_TRACE', '1')]),  # compiled C files are stored in /home/pdiracdelta/.pyxbld/
    ]

setup(
    name='cpc_naive',
    author=censored,
    author_email=censored,
    license=censored,
    packages=find_packages(),
    cmdclass=cmdclass,
    ext_modules=ext_modules,
    install_requires=['Cython', 'numpy'],
    include_dirs=[numpy.get_include()],
    setup_requires=['pytest-runner'],
    tests_require=['pytest']
)
E   ImportError: No module named 'calculateScore'
AttributeError: type object 'test' has no attribute 'install_dists'