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.
While the pytest discovery mechanism can find tests anywhere, pytests must be placed into separate directories from the product code packages. These directories may either be under the project root or under the Python package.
Use of Setup.py The setup.py file may be the most significant file that should be placed at the root of the Python project directory.
Running pytest without mentioning a filename will run all files of format test_*. py or *_test.py in the current directory and subdirectories. Pytest automatically identifies those files as test files. We can make pytest run other filenames by explicitly mentioning them.
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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With