Is there a way to ask tox to run environments matching a name or by group?

Question:

I have a tox.ini with several envs, like this example:

[tox]
envlist =
    py37,
    py38,
    py39,
    lint-{foo,bar,baz}
...

For several of my users, only a subset of these is useful at a time, usually all the py3 ones or all the lint- ones.

Is there a syntax such that instead of tox -e py37,py38,py39, one can say "run all envs (a subset of what tox finds via tox -a) matching py3"?

Alternatively, is there a way to group the envs so that one can say "run the test envs" or "run the cleanup envs" without losing the ability to call tox to run them all?

I can write a separate script to handle this, but I’m searching for a built-in way to match.

Asked By: mech

||

Answers:

The closest to a built-in way to filter tox environments by name is the TOX_SKIP_ENV environment variable, which tox uses as a pattern for re.match (see source) with each environment name, skipping those that match.

So, for example, to only run the lint- prefixed environments, one could:

env TOX_SKIP_ENV='^(?!lint-)' tox

Or, to skip all envs with py or bar anywhere in the name…

env TOX_SKIP_ENV='.*?(py|bar)' tox

There is a (currently unmaintained) tox plugin, tox-tags, aiming to provide roughly the tagging/grouping functionality I was thinking of.

Answered By: mech

Alternatively, is there a way to group the envs so that one can say "run the test envs" or "run the cleanup envs" without losing the ability to call tox to run them all?

You can use labels for this. For example, the following in tox.ini:

[tox]
labels =
     test = py310, py39
     static = flake8, mypy

allows you to run both flake8 and mypy environments with tox run -m static.

Alternatively, you can specify labels in [testenv:...] sections, e.g.:

[testenv:mypy]
labels = static
commands = mypy src/
...

[testenv:flake8]
labels = static
commands = flake8 src/
...

would also result in tox run -m static to run both mypy and flake8 environments.

Answered By: Adrien
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.