How do I force `setup.py test` to install dependencies into my `virtualenv`?

Question:

In a crusade to make my application pip-installable, I’m fighting big fights with setuptools and distribute. I assume my dependencies are correct, i.e. installing with pip install myapp should probably fill the virtual environment correctly. However, I’d like to streamline development while I’m at it, so my goal is to start with an empty virtualenv and make setup.py test (and later setup.py develop, but that’s a whole different fight) fill it with all defined dependencies.

And now to my problem: no matter how hard I try, all I get are dependencies installed as .eggs in my project directory which is sub-optimal at the very least. I tried creating a new setuptools command which would use pip (which seems to work, even though awkwardly) but that can’t seriously be the solution (subclassing and overriding that is).

So how do I make setup.py test fill the virtualevn instead of my working directory?

Answers:

If you are using setuptools, you can specify test dependencies using the tests_require keyword argument for the setup method.

from setuptools import setup

setup(
    name='your-package-name',
    version='1.0.0',
    author='me',
    author_email='[email protected]',
    install_requires=['Pygments>=1.4'],
    tests_require=['nose'],
    packages=[
        'your_package_name',
    ],
)

When you run python setup.py test, this will check for nose and install it to the currently active virtualenv using pip if not already available.

Note that this argument will be ignored if you are using distribute.core.setup (nor will a test command be available).

Answered By: modocache

By design, you can’t make the tests_requires or the setup_requires entries go into the virtual environment. The idea is to separate what is required for performing tests/setup and what is required to actually use the package being installed. For example, I may require that the “coverage” module be needed for running tests on my package, but it isn’t used by any of my code in the package. Therefore, if I didn’t have “coverage” in my environment when I go and run tests, I wouldn’t want “coverage” to get installed into the environment if my package didn’t need it.

Answered By: Ben Root