gpt4 book ai didi

自定义测试命令的 Python setup.py 测试依赖项

转载 作者:太空狗 更新时间:2023-10-29 23:58:17 24 4
gpt4 key购买 nike

为了制作 python setup.py test linting、测试和覆盖命令,我创建了一个自定义命令。但是,它不再安装指定为 tests_require 的依赖项。如何让两者同时工作?

class TestCommand(setuptools.Command):

description = 'run linters, tests and create a coverage report'
user_options = []

def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
self._run(['pep8', 'package', 'test', 'setup.py'])
self._run(['py.test', '--cov=package', 'test'])

def _run(self, command):
try:
subprocess.check_call(command)
except subprocess.CalledProcessError as error:
print('Command failed with exit code', error.returncode)
sys.exit(error.returncode)


def parse_requirements(filename):
with open(filename) as file_:
lines = map(lambda x: x.strip('\n'), file_.readlines())
lines = filter(lambda x: x and not x.startswith('#'), lines)
return list(lines)


if __name__ == '__main__':
setuptools.setup(
# ...
tests_require=parse_requirements('requirements-test.txt'),
cmdclass={'test': TestCommand},
)

最佳答案

您继承自错误的类。尝试从 setuptools.command.test.test 继承,它本身是 setuptools.Command 的子类,但有额外的方法来处理依赖项的安装。然后,您需要覆盖 run_tests() 而不是 run()

所以,一些类似的东西:

from setuptools.command.test import test as TestCommand


class MyTestCommand(TestCommand):

description = 'run linters, tests and create a coverage report'
user_options = []

def run_tests(self):
self._run(['pep8', 'package', 'test', 'setup.py'])
self._run(['py.test', '--cov=package', 'test'])

def _run(self, command):
try:
subprocess.check_call(command)
except subprocess.CalledProcessError as error:
print('Command failed with exit code', error.returncode)
sys.exit(error.returncode)


if __name__ == '__main__':
setuptools.setup(
# ...
tests_require=parse_requirements('requirements-test.txt'),
cmdclass={'test': MyTestCommand},
)

关于自定义测试命令的 Python setup.py 测试依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33972225/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com