Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python setup.py test dependencies for custom test command

To make python setup.py test linting, testing and coverage commands, I created a custom command. However, it doesn't install the dependencies specified as tests_require anymore. How can I make both work at the same time?

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},
    )
like image 451
danijar Avatar asked Nov 28 '15 14:11

danijar


People also ask

Do you need setup py and requirements txt?

The short answer is that requirements. txt is for listing package requirements only. setup.py on the other hand is more like an installation script. If you don't plan on installing the python code, typically you would only need requirements.

What is pip install test?

The pip install <package> command always looks for the latest version of the package and installs it. It also searches for dependencies listed in the package metadata and installs them to ensure that the package has all the requirements that it needs.

Does pip install use setup py?

@joeforker, pip uses setup.py behind the scenes.


1 Answers

You're inheriting from the wrong class. Try inheriting from setuptools.command.test.test which is itself a subclass of setuptools.Command, but has additional methods to handle installation of your dependencies. You'll then want to override run_tests() rather than run().

So, something along the lines of:

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},
    )
like image 96
Dan Avatar answered Oct 06 '22 04:10

Dan