2015-11-28 9 views
5

के लिए पायथन setup.py परीक्षण निर्भरता python setup.py test बनाने, परीक्षण और कवरेज कमांड बनाने के लिए, मैंने एक कस्टम कमांड बनाया है। हालांकि, यह 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}, 
    ) 

उत्तर

3

आप गलत वर्ग से विरासत में हैं। setuptools.command.test.test से विरासत प्राप्त करने का प्रयास करें जो स्वयं setuptools.Command का उप-वर्ग है, लेकिन आपकी निर्भरताओं की स्थापना को संभालने के लिए अतिरिक्त विधियां हैं। फिर आप run() के बजाय run_tests() को ओवरराइड करना चाहते हैं।

तो, की तर्ज पर कुछ:

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}, 
    ) 
संबंधित मुद्दे