should pytest et al. go in tests_require[] or extras_require{testing[]}?

Question:

I am writing a python program which uses py.test for testing and now one test also depends on numpy. Where in my setup.py should I add those dependencies?

Currently the relevant part of my setup.py looks something like this:

[...]
'version': '0.0.1',
'install_requires': [],
'tests_require': ['pytest'],
'cmdclass': {'test': PyTest},
'extras_require': {
    'testing': ['pytest'],
},
[...]

Having pytest twice feels already somewhat strange and I’m not sure where to add numpy.

Asked By: Michael Große

||

Answers:

According to the docs

tests_require are additional packages that are obtained when using setuptools‘s test command. They are not installed on the system.

extras_require are optional additional packages grouped by the feature name. The list of packages are installed to use that feature and there are various ways to install them. See Does pip handle extras_requires from setuptools/distribute based sources?

My interpretation

tests_require should be packages that are used in the tests such as numpy and not packages that are used to conduct testing like pytest or nose. tests_require would need to be moved or copied to a “testing” feature in extras_require when testing outside of setuptools.

Use extras_require to make a testing package such as pytest optional. Use setup_requires to require it.

pytest and nose can be integrated with setuptools to take advantage of the convenience of tests_require, however, there may be drawbacks. nose warns that plugins may not be available when run through setuptools.

See Integrating with setuptools / python setup.py test / pytest-runner
and nosetests setuptools command.

For example

Testing with setuptools integration:

setup.py

[...]
'version': '0.0.1',
'install_requires': [],
'tests_require': ['numpy'],
'cmdclass': {'test': PyTest},
'extras_require': {
    'testing': ['pytest'],
},
[...]

sh

(env) > python setup.py develop
(env) > easy_install pytest
(env) > python setup.py test -a "--pdb"

Or, testing without setuptools integration:

setup.py

[...]
'version': '0.0.1',
'install_requires': [],
'extras_require': {
    'testing': ['pytest', 'numpy'],
},
[...]

sh

(env) > pip install -e .[testing]
(env) > pytest.py --pdb
Answered By: None
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.