如果扩展是发行版的一部分,除了运行之外你不需要做任何事情poetry install
——poetry
将就地构建扩展作为项目可编辑安装的一部分。
在其他情况下,您可以在测试中嵌入调用distutils
命令作为套件设置/拆卸的一部分。我不是很熟悉nose
,但这里有一个简单的例子。想象一下我有一个fib.pyx
(这是 Cython 书中的一个例子):
def fib(long n):
'''Returns the nth Fibonacci number.'''
cdef long a=0, b=1, i
for i in range(n):
a, b = a + b, a
return a
test_fib.py
构建fib
库并在测试成功时将其删除的模块:
from distutils.dist import Distribution
from distutils.core import Extension
from pathlib import Path
from Cython.Build import cythonize
fib_source = Path('fib.pyx')
# distutils magic. This is essentially the same as calling
# python setup.py build_ext --inplace
dist = Distribution(attrs={'ext_modules': cythonize(fib_source.name)})
build_ext_cmd = dist.get_command_obj('build_ext')
build_ext_cmd.ensure_finalized()
build_ext_cmd.inplace = 1
build_ext_cmd.run()
fib_obj = Path(build_ext_cmd.get_ext_fullpath(fib_source.stem))
# the lib was built, so the import will succeed now
from fib import fib
def teardown_module():
# remove built library
fib_obj.unlink()
# if you also want to clean the build dir:
from distutils.dir_util import remove_tree
remove_tree(build_ext_cmd.build_lib)
remove_tree(build_ext_cmd.build_temp)
# sample tests
def test_zero():
assert fib(0) == 0
def test_ten():
assert fib(10) == 55
您可能正在自setup_kwargs
定义 custom 中的build.py
. 要重用此代码,请调整dist
初始化,例如:
from build import build
setup_kwargs = {}
build(setup_kwargs)
dist = Distribution(attrs=setup_kwargs)
...
pytest
例子
使用 可以更方便地组织事情pytest
。创建一个以conftest.py
提取到挂钩的安装/拆卸代码命名的文件:
# conftest.py
from distutils.core import Extension
from distutils.dist import Distribution
from distutils.dir_util import remove_tree
from pathlib import Path
from Cython.Build import cythonize
def pytest_sessionstart(session):
fib_source = Path('fib.pyx')
dist = Distribution(attrs={'ext_modules': cythonize(fib_source.name)})
build_ext_cmd = dist.get_command_obj('build_ext')
build_ext_cmd.ensure_finalized()
build_ext_cmd.inplace = 1
build_ext_cmd.run()
session.fib_obj = Path(build_ext_cmd.get_ext_fullpath(fib_source.stem))
def pytest_sessionfinish(session):
session.fib_obj.unlink()
现在测试变得更加清晰,并且设置代码在整个测试会话中运行一次。上面的测试例子,重温:
from fib import fib
def test_zero():
assert fib(0) == 0
def test_ten():
assert fib(10) == 55