10

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.

ento
  • 5,801
  • 6
  • 51
  • 69
ThorSummoner
  • 16,657
  • 15
  • 135
  • 147

3 Answers3

26

You now have the "extras" option:

# tox.ini
...
[testenv]
...
extras = test

Source: https://tox.readthedocs.io/en/latest/config.html#conf-extras

Dustin Ingram
  • 20,502
  • 7
  • 59
  • 82
helderco
  • 1,043
  • 1
  • 11
  • 9
  • "New in version 2.4.", "2.4 tagged on Oct 12, 2016", so its not brand new, but it sounds like exactly what I was after! – ThorSummoner Jul 24 '17 at 16:53
5

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.

Peterino
  • 15,097
  • 3
  • 28
  • 29
1

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.

ThorSummoner
  • 16,657
  • 15
  • 135
  • 147