简体   繁体   中英

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

eg 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?

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

[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 (eg 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 .

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}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM