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

时间:2015-11-28 14:07:05

标签: python testing dependencies setuptools packaging

要制作python setup.py test linting,testing和coverage命令,我创建了一个自定义命令。但是,它不再安装指定为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},
    )

1 个答案:

答案 0 :(得分:6)

你是从错误的班级继承的。尝试从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}, )

所以,有些东西:

{{1}}