How to get tox to test "named" test environments with different python versions?

Question:

e.g. Let’s say I have following in tox.ini

[tox]
envlist = py27, py35, testenv2

[testenv]  
 # settings related to "default" testenv - includes deps, commands

[testenv:testenv2]  
 # settings related to testenv2 - includes deps, commands

Now when I run tox command, it invokes testenv commands with python 2.7 and 3.5 interpreter but testenv2 commands only with base python installed on the machine (in my case 2.7). How to get tox to also test a “named” (non-default) test environment like testenv2 to be tested with multiple python versions?

Asked By: Viral Modi

||

Answers:

List all environments explicitly:

[tox]
envlist = py27, py35, py27-testenv2, py35-testenv2

If that’s not enough refactor tox.ini:

[testenv]  
 # settings related to "default" testenv - includes deps, commands

[testenv2]  
 # settings related to testenv2 - includes deps, commands

[testenv:py27-testenv2]  
deps = {[testenv2]deps}
commands = {[testenv2]commands}

[testenv:py35-testenv2]  
deps = {[testenv2]deps}
commands = {[testenv2]commands}
Answered By: phd

The first answer describes two viable ways. For completeness: you can also generate environments and use conditional settings – e.g.:

[tox]
skipsdist = True
envlist = py{27,35}-{test,lint}
[testenv]
skip_install = True
deps =
    test: pytest
    test: pytest-xprocess
    lint: flake8
    lint: black
commands =
    test: pytest -v
    lint: flake8
    lint: black .

would generate (tox -a):

py27-test
py27-lint
py35-test
py35-lint

You can use any factor (e.g. py27 or test) to conditionally add commands, deps, etc.

See also the docs.

BTW: to see which settings each testenv exactly has you can run tox --showconfig.

Answered By: Oliver Bestwalter

I could get the “named” test environment to be tested with multiple python versions by making multiple “named” test environments, one each for the different python versions I wanted it to be tested with and using the basepython option to specify the python version for the test environment to be tested with. Below is the example that works:

[tox]
envlist = py27, py35, testenv2_py27, testenv2_py35

[testenv]  
 # settings related to "default" testenv - includes deps, commands

[testenv:testenv2_py27]
basepython = python2.7 
 # settings related to testenv2 - includes deps, commands

[testenv:testenv2_py35]
basepython = python3.5  
 # settings related to testenv2 - includes deps, commands
Answered By: Viral Modi

Just in case someone still needs this, I figured out an even more concise solution. It combines all the good suggestions from the other answers:

[tox]
envlist = py{27,35}, testenv2-py{27,35}

[testenv]  
 # settings related to "default" testenv - includes deps, commands

[testenv:testenv2-py{27,35}]
 # settings related to testenv2 - includes deps, commands
Answered By: wonderjaxe
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.