Specify where to install 'tests_require' dependencies of a distribute/setuptools package
Asked Answered
C

3

50

When I run python setup.py test the dependencies listed in tests_require in setup.py are downloaded to the current directory. When I run python setup.py install, the dependencies listed in requires are instead installed to site-packages.

How can I have those tests_require dependencies instead installed in site-packages?

Cyprinid answered 19/1, 2011 at 10:43 Comment(4)
setuptools tests_require is deprecated since 41.5.0 (October 2019). We are now at 50.3.0.Whitening
Hello @MartinThoma is there an alternative to specify requirements needed for dev/test but not plain usage?Dehnel
That would be something like extras_require={'test': ['pytest']} in setup.py I think.Dehnel
@MartinThoma the release notes make no mention of thatFlashover
S
54

You cannot specify where the test requirements are installed. The whole point of the tests_require parameter is to specify dependencies that are not required for the installation of the package but only for running the tests (as you can imagine many consumers might want to install the package but not run the tests). If you want the test requirements to be included during installation, I would include them in the install_requires parameter. For example:

test_requirements = ['pytest>=2.1', 'dingus']
setup(
    # ...
    tests_require = test_requirements,
    install_requires = [
        # ... (your usual install requirements)
    ] + test_requirements,
)

As far as I know, there's no parameter you can pass to force this behavior without changing the setup script.

Selfconsequence answered 12/10, 2011 at 22:1 Comment(2)
I found this article which describes using 'extras' for a similar purpose.Selfconsequence
I no longer recommend this technique. If tests are required for installation (whether or not they're required for tests), just put them in install_requires, and don't bother adding them to tests_require.Selfconsequence
B
29

You can use a virtualenv to avoid this and install the extra packages to their default locations, inside lib/pythonX/site-packages. First, you should define your testing requirements as extras, in setup.py:

setup(
    # ...
    install_requires=[
        # ... (your usual install requirements)
    ],
    extras_require={
        'testing': [
            # ... (your test requirements)
        ]
    },
)

Then install your package with test requirements like this:

pip install -e ".[testing]"
Barth answered 3/12, 2014 at 12:7 Comment(1)
@Flimm It does not. But you could pull the test packages into a list and reference the same list from extras_require['test'] and tests_require like this answer does. https://mcmap.net/q/355541/-pip-install-test-dependencies-for-tox-from-setup-pyFlynt
I
18

I am using pip to achieve something like that. Instead of adding tests_requires or extras to my setup.py I have created a pip requirements file.

Example my dev_requirements.txt file:

pytest
webtest

Then to install it run:

$ pip install -r dev_requirements.txt
Isleana answered 17/1, 2012 at 18:22 Comment(2)
I believe requirements-dev.txt and requirements-test.txt are the more commonly used names for these files.Glob
@phoenix the same can be said about requirements/test.txt and requirements/prod.txtChart

© 2022 - 2024 — McMap. All rights reserved.