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

dan*_*jar 6 python testing dependencies packaging setuptools

为了制作python setup.py testlinting,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},
    )
Run Code Online (Sandbox Code Playgroud)

Dan*_*Dan 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},
    )
Run Code Online (Sandbox Code Playgroud)