2015-11-28 144 views
5

爲了製作python setup.py test linting,測試和覆蓋命令,我創建了一個自定義命令。但是,它不再安裝指定爲tests_require的依賴項。我怎樣才能讓兩者同時工作?Python setup.py自定義測試命令的測試依賴關係

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}, 
    ) 

回答

3

您從錯誤的類繼承。嘗試從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}, 
    ) 
相關問題