I want to use setup.py as the authority on packages to install for testing, done with extra_requires like so:
setup(
# ...
extras_require={
'test': ['pytest', ],
},
)
Tox only appears to be capable of installing from a requirements.txt file which just implies a step of snapshotting requirements before testing (which I'm ignorant how to do automatically) or by duplicating the test dependencies into the tox file, which is all I want to avoid. One mailing list post suggested that tox.ini should be the authority on test dependencies, but I don't wish to straightjacket tox into the project that completely.
Use of Setup.py It primarily serves two purposes: It includes choices and metadata about the program, such as the package name, version, author, license, minimal dependencies, entry points, data files, and so on. Secondly, it serves as the command line interface via which packaging commands may be executed.
tox will automatically try to discover a valid Python interpreter version by using the environment name (e.g. py27 means Python 2.7 and the basepython configuration value) and the current operating system PATH value.
Sometimes the "extras" option can't resolve the problem (e.g. when your extras dependency requires a dependency from the deps
section; for example when you use pytest-django
and your extras dependency doesn't install Django itself).
In that case you can simply install your extras in the deps
section like this:
# tox.ini
[testenv]
deps = .[test]
The period .
represents the current project (current path), followed by the extras in brackets, as usual. This works just like a pip install .[test]
would.
You now have the "extras" option:
# tox.ini
...
[testenv]
...
extras = test
Source: https://tox.readthedocs.io/en/latest/config.html#conf-extras
I've come up with a nasty hack that seems to work
# tox.ini
...
[testenv]
...
install_command = pip install {opts} {packages} {env:PWD}[test]
The defualt install_command
is pip install {opts} {packages}
, unfortunately {packages}
is a required argument. Additionally tox doesn't expose the project directory as a magic variable; but it does expose the env
of the shell that ran it.
This works by assuming you ran tox
from the same directory as your setup.py
and tox.ini
, and assuming your shell exposes PWD
as your current path. Tox appears to use something like shlex
to split your install_command
into a shell-safe set of arguments, so we cant do things like {packages}[test]
. Ultimately this hack will name your package twice, but I think that's okay since {env:PWD}[test]
names your package plus the extra_require
block you want.
I don't know a better way, the PYPA SampleProject seems content with specifying your test dependencies in both setup.py and tox.ini.
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