tox tests, use setup.py extra_require as tox deps source
<p>I want to use setup.py as the authority on packages to install for testing, done with extra_requires like so:</p>

<pre><code>setup(
    # ...
    extras_require={
        'test': ['pytest', ],
    },
)
</code></pre>

<p>Tox only appears to be capable of <a href="https://testrun.org/tox/latest/example/basic.html#depending-on-requirements-txt" rel="nofollow">installing from a requirements.txt</a> file which just implies a step of snapshotting requirements before testing (which I'm ignorant how to do automatically) or by <a href="https://testrun.org/tox/latest/config.html#confval-deps=MULTI-LINE-LIST" rel="nofollow">duplicating the test dependencies</a> into the tox file, which is all I want to avoid. <a href="http://lists.idyll.org/pipermail/testing-in-python/2015-March/006285.html" rel="nofollow">One mailing list post</a> suggested that tox.ini should be the authority on test dependencies, but I don't wish to straightjacket tox into the project that completely.</p>

<p>I've come up with a nasty hack that seems to work</p>

<pre><code># tox.ini
...
[testenv]
...
install_command = pip install {opts} {packages} {env:PWD}[test]
</code></pre>

<p>The defualt <code>install_command</code> is <code>pip install {opts} {packages}</code>, unfortunately <code>{packages}</code> is a required argument. Additionally tox doesn't expose the project directory as a magic variable; but it does expose the <code>env</code> of  the shell that ran it.</p>

<p>This works by assuming you ran <code>tox</code> from the same directory as your <code>setup.py</code> and <code>tox.ini</code>, and assuming your shell exposes <code>PWD</code> as your current path. Tox appears to use something like <code>shlex</code> to split your <code>install_command</code> into a shell-safe set of arguments, so we cant do things like <code>{packages}[test]</code>. Ultimately this hack will name your package twice, but I think that's okay since <code>{env:PWD}[test]</code> names your package plus the <code>extra_require</code> block you want.</p>

<p>I don't know a better way, the <a href="https://github.com/pypa/sampleproject/" rel="nofollow">PYPA SampleProject</a> seems content with specifying your test dependencies in both setup.py and tox.ini.</p>

python testing setup.py tox