【问题标题】:Python setup.py test dependencies for custom test command自定义测试命令的 Python setup.py 测试依赖项
【发布时间】:2016-03-02 12:45:55
【问题描述】:

为了制作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},
    )

【问题讨论】:

    标签: python testing dependencies setuptools packaging


    【解决方案1】:

    您从错误的类继承。尝试从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},
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 2016-02-06
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多